validates_by_schema 0.5.0 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a44382b4c751d51c6056b9eae1daed6f8eedfe8040383fe38ce717beafc2c84d
4
- data.tar.gz: 553ffe11b1a3c83f6c2f1126954d691553465492f7f3820f6530b14a48675971
3
+ metadata.gz: a0460d1427aec1f4b27e66917d3b708c33f277e799ffb32150f8a34e2317795a
4
+ data.tar.gz: e1dd3c76c989ed56df9dfb3d40a912206fcf75ed05d7843ce73c3d3914f99656
5
5
  SHA512:
6
- metadata.gz: b404ea525a570681cc76d936755f6f6fbc89a310e8409d0480161a2a9aad11e1c803445c30b0cf03ce951d02ae63f614715e48c3a9bfde80455e5cec4b293c71
7
- data.tar.gz: a7b24d0fe512674063b0c055f155b5cd608661fcf284634399354c6d63a3acfbf7fba6e9de335ea5e55e25765cedc8bda60505879aa7d9a09f96a3e828ebadba
6
+ metadata.gz: 0aa543eb8486c4df7e2be25863a079f81a377da4f0f06a25f6d8878dfc8d075eb78f10b0aeed0caecc9d5fbff653b1c985e1d5cc5abf12e65322c32aacb40a17
7
+ data.tar.gz: 30d69fea7343a822b90008d8ba24c8bc46bf4f3f552f00b9c9a6c8db5a9782728a6f6ce4e81dcf16be3cd0150891176571ec99597aa3f0999e057fdd285366ac
data/README.md CHANGED
@@ -1,9 +1,9 @@
1
1
  # Validates By Schema (validates_by_schema)
2
2
 
3
- [![Gem Version](http://img.shields.io/gem/v/validates_by_schema.svg?style=flat)](https://rubygems.org/gems/validates_by_schema)
4
- [![Build Status](http://img.shields.io/github/workflow/status/joshwlewis/validates_by_schema/Build?style=flat)](https://github.com/joshwlewis/validates_by_schema/actions/workflows/build.yml)
5
- [![Coverage Status](http://img.shields.io/coveralls/joshwlewis/validates_by_schema.svg?style=flat)](https://coveralls.io/r/joshwlewis/validates_by_schema)
6
- [![Code Climate](http://img.shields.io/codeclimate/maintainability/joshwlewis/validates_by_schema.svg?style=flat)](https://codeclimate.com/github/joshwlewis/validates_by_schema)
3
+ [![Gem Version](https://img.shields.io/gem/v/validates_by_schema.svg?style=flat)](https://rubygems.org/gems/validates_by_schema)
4
+ [![Build Status](https://img.shields.io/github/actions/workflow/status/joshwlewis/validates_by_schema/build.yml?style=flat)](https://github.com/joshwlewis/validates_by_schema/actions/workflows/build.yml)
5
+ [![Coverage Status](https://img.shields.io/coveralls/joshwlewis/validates_by_schema.svg?style=flat)](https://coveralls.io/r/joshwlewis/validates_by_schema)
6
+ [![Code Climate](https://img.shields.io/codeclimate/maintainability/joshwlewis/validates_by_schema.svg?style=flat)](https://codeclimate.com/github/joshwlewis/validates_by_schema)
7
7
 
8
8
  Automatic validation based on your database schema column types and limits. Keep your code DRY by inferring column validations from table properties!
9
9
 
@@ -1,11 +1,12 @@
1
1
  class ValidatesBySchema::ValidationOption
2
2
  # column here must be an ActiveRecord column
3
3
  # i.e. MyARModel.columns.first
4
- attr_accessor :klass, :column
4
+ attr_accessor :klass, :column, :unique_indexes
5
5
 
6
- def initialize(klass, column)
6
+ def initialize(klass, column, unique_indexes = [])
7
7
  @klass = klass
8
8
  @column = column
9
+ @unique_indexes = unique_indexes.select { |index| index.columns.first == column.name }
9
10
  end
10
11
 
11
12
  def define!
@@ -16,13 +17,15 @@ class ValidatesBySchema::ValidationOption
16
17
  else
17
18
  define_validations(to_hash)
18
19
  end
19
- define_uniqueness_validations if ValidatesBySchema.validate_uniqueness
20
+ define_uniqueness_validations
20
21
  end
21
22
 
22
23
  private
23
24
 
24
25
  def define_belongs_to_presence_validation
25
- klass.validates association.name, presence: true if !ActiveRecord::Base.belongs_to_required_by_default && presence
26
+ if !ActiveRecord::Base.belongs_to_required_by_default && presence
27
+ klass.validates association.name, presence: true
28
+ end
26
29
  end
27
30
 
28
31
  def define_uniqueness_validations
@@ -79,13 +82,6 @@ class ValidatesBySchema::ValidationOption
79
82
  end
80
83
  end
81
84
 
82
- def unique_indexes
83
- klass
84
- .connection
85
- .indexes(klass.table_name)
86
- .select { |index| index.unique && index.columns.first == column.name }
87
- end
88
-
89
85
  def case_sensitive?
90
86
  !klass.connection.respond_to?(:collation) ||
91
87
  !klass.connection.collation.end_with?('_ci')
@@ -1,3 +1,3 @@
1
1
  module ValidatesBySchema
2
- VERSION = '0.5.0'
2
+ VERSION = '0.5.1'
3
3
  end
@@ -28,13 +28,23 @@ module ValidatesBySchema
28
28
  def define_schema_validations
29
29
  return unless @validates_by_schema_options
30
30
 
31
+ unique_indexes = ValidatesBySchema.validate_uniqueness ? fetch_unique_indexes : []
31
32
  customized_schema_validatable_columns.each do |c|
32
- ValidationOption.new(self, c).define!
33
+ ValidationOption.new(self, c, unique_indexes).define!
33
34
  end
34
35
 
35
36
  @validates_by_schema_options = nil
36
37
  end
37
38
 
39
+ def fetch_unique_indexes
40
+ if connection.schema_cache.respond_to?(:indexes)
41
+ connection.schema_cache.indexes(table_name).select(&:unique)
42
+ else
43
+ # Rails < 6.0
44
+ connection.indexes(table_name).select(&:unique)
45
+ end
46
+ end
47
+
38
48
  def customized_schema_validatable_columns
39
49
  # Allow user to specify :only or :except options
40
50
  schema_validatable_columns.tap do |columns|
@@ -54,7 +64,7 @@ module ValidatesBySchema
54
64
  end
55
65
 
56
66
  def ignored_columns_for_validates_by_schema
57
- [primary_key.to_s, 'created_at', 'updated_at', 'deleted_at']
67
+ ['id', 'created_at', 'updated_at', 'deleted_at']
58
68
  end
59
69
 
60
70
  end
data/spec/db/test.sqlite3 CHANGED
Binary file
data/spec/spec_helper.rb CHANGED
@@ -22,7 +22,15 @@ end
22
22
  require 'validates_by_schema'
23
23
 
24
24
  # Setup the database
25
- conf = YAML.load(ERB.new(File.read(File.join(File.dirname(__FILE__), 'config', 'database.yml'))).result)
25
+ conf = nil
26
+ begin
27
+ # load with psych 4 (>= ruby 3.1)
28
+ conf = YAML.load(ERB.new(File.read(File.join(File.dirname(__FILE__), 'config', 'database.yml'))).result, aliases: true)
29
+ rescue ArgumentError
30
+ # fallback to psych 3 syntax
31
+ conf = YAML.load(ERB.new(File.read(File.join(File.dirname(__FILE__), 'config', 'database.yml'))).result)
32
+ end
33
+
26
34
  ActiveRecord::Base.establish_connection(conf['test'])
27
35
 
28
36
  ActiveRecord::Base.belongs_to_required_by_default = false
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: validates_by_schema
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Josh Lewis
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2021-11-25 00:00:00.000000000 Z
12
+ date: 2023-02-28 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
@@ -161,22 +161,22 @@ required_rubygems_version: !ruby/object:Gem::Requirement
161
161
  - !ruby/object:Gem::Version
162
162
  version: '0'
163
163
  requirements: []
164
- rubygems_version: 3.1.4
164
+ rubygems_version: 3.3.26
165
165
  signing_key:
166
166
  specification_version: 4
167
167
  summary: Automatic validation based on your database schema column types and limits.
168
168
  test_files:
169
- - spec/spec_helper.rb
169
+ - spec/ci/rails50.gemfile
170
+ - spec/ci/rails50.gemfile.lock
171
+ - spec/ci/rails52.gemfile
172
+ - spec/ci/rails60.gemfile
173
+ - spec/config/database.yml
174
+ - spec/config/schema.rb
170
175
  - spec/db/test.sqlite3
171
- - spec/validations_spec.rb
172
- - spec/support/models/widget.rb
176
+ - spec/spec_helper.rb
173
177
  - spec/support/models/contraption.rb
174
- - spec/support/models/sub_contraption.rb
175
178
  - spec/support/models/gadget.rb
176
179
  - spec/support/models/gizmo.rb
177
- - spec/config/schema.rb
178
- - spec/config/database.yml
179
- - spec/ci/rails52.gemfile
180
- - spec/ci/rails50.gemfile
181
- - spec/ci/rails60.gemfile
182
- - spec/ci/rails50.gemfile.lock
180
+ - spec/support/models/sub_contraption.rb
181
+ - spec/support/models/widget.rb
182
+ - spec/validations_spec.rb