database_validations 0.8.9 → 0.8.10

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9c5d3a56f3d69b34fdc1da4e3b74b9ff3797d0ca93772df93746e709f9b9fd64
4
- data.tar.gz: db772a3a726ccce4ce21bfceb0ddac72da07d9da3650f89d1845e92e580519b2
3
+ metadata.gz: c934b2c71117e26cc8493d7eafb35a681cb54f77bb6a1ad7d9ba0ec309664817
4
+ data.tar.gz: 7b7e34001d44eac5b818d756288ecb5e5d7f33147d5f8bbc07a3e54b0de6287c
5
5
  SHA512:
6
- metadata.gz: da0b57c3e7203be84466c31f72a517852d1b4484a306607bf48e6d6f6d683e43dc1ebdcd678bc258075c5ce1f9c52af756805b749ab5562cc84ac83a6ac48ee4
7
- data.tar.gz: 1580a4718c53aa42267a2d7efd04148df0de38ddba3ce7c4c5f6c4c226736e4f568919cbe600b79d269566f081c7c9537b09fca0f5b0d65cdd73378e81b9ae71
6
+ metadata.gz: 544cee2e2edf40aec5a25bfdc789df6324d07120997a1a68b366eac75152eeb095ca9cfb3758c32842e189da2ad5306e4530faaa24fa2ecbf44c93c44995e2c9
7
+ data.tar.gz: 3e006dfe1110c3758a3a971a2db45660a73427e6b9007d140f1e338d8dd02bd099c5a9cd29f78b8018c1f541c8c67f4bd064a845bbbf5bb6f1a18ff21d6d679c
@@ -1,11 +1,20 @@
1
1
  module DatabaseValidations
2
2
  module ClassMethods
3
+ # rubocop: disable Metrics/AbcSize
3
4
  def db_belongs_to(name, scope = nil, **options)
4
5
  Helpers.cache_valid_method!(self)
5
6
 
6
7
  @database_validations_storage ||= DatabaseValidations::OptionsStorage.new(self)
7
8
 
8
- belongs_to(name, scope, options.merge(optional: true))
9
+ belongs_to_options =
10
+ if ActiveRecord::VERSION::MAJOR < 5
11
+ options.delete(:optional)
12
+ options.merge(required: false)
13
+ else
14
+ options.merge(optional: true)
15
+ end
16
+
17
+ belongs_to(name, scope, belongs_to_options)
9
18
 
10
19
  foreign_key = reflections[name.to_s].foreign_key
11
20
 
@@ -17,4 +26,5 @@ module DatabaseValidations
17
26
  include(DatabaseValidations::Rescuer)
18
27
  end
19
28
  end
29
+ # rubocop: enable Metrics/AbcSize
20
30
  end
@@ -1,7 +1,14 @@
1
1
  module DatabaseValidations
2
2
  class BelongsToOptions
3
+ VALIDATOR_MESSAGE =
4
+ if ActiveRecord::VERSION::MAJOR < 5
5
+ :blank
6
+ else
7
+ :required
8
+ end
9
+
3
10
  def self.validator_options(association, foreign_key)
4
- { attributes: association, foreign_key: foreign_key, message: :required }
11
+ { attributes: association, foreign_key: foreign_key, message: VALIDATOR_MESSAGE }
5
12
  end
6
13
 
7
14
  attr_reader :column, :adapter, :relation
@@ -23,7 +30,7 @@ module DatabaseValidations
23
30
  def handle_foreign_key_error(instance)
24
31
  # Hack to not query the database because we know the result already
25
32
  instance.send("#{relation}=", nil)
26
- instance.errors.add(relation, :blank, message: :required)
33
+ instance.errors.add(relation, :blank, message: VALIDATOR_MESSAGE)
27
34
  end
28
35
 
29
36
  private
@@ -23,7 +23,7 @@ module DatabaseValidations
23
23
  else
24
24
  return unless record.public_send(foreign_key).blank? && record.public_send(association).blank?
25
25
 
26
- record.errors.add(association, :blank, message: :required)
26
+ record.errors.add(association, :blank, message: BelongsToOptions::VALIDATOR_MESSAGE)
27
27
  end
28
28
  end
29
29
  end
@@ -5,7 +5,7 @@ module DatabaseValidations
5
5
  def cache_valid_method!(klass)
6
6
  return if klass.method_defined?(:valid_without_database_validations?)
7
7
 
8
- klass.alias_method(:valid_without_database_validations?, :valid?)
8
+ klass.__send__(:alias_method, :valid_without_database_validations?, :valid?)
9
9
  end
10
10
 
11
11
  def handle_error!(instance, error)
@@ -91,8 +91,18 @@ module DatabaseValidations
91
91
  index_name ? adapter.find_index_by_name(index_name.to_s) : adapter.find_index(columns, where_clause)
92
92
  end
93
93
 
94
+ def index_columns_size(columns)
95
+ columns.is_a?(Array) ? columns.size : (columns.count(',') + 1)
96
+ end
97
+
98
+ def check_index_options?(index)
99
+ (columns.size == index_columns_size(index.columns)) && (where_clause.nil? == index.where.nil?)
100
+ end
101
+
94
102
  def raise_if_index_missed!(index)
95
- raise Errors::IndexNotFound.new(columns, where_clause, index_name, adapter.indexes, adapter.table_name) unless index
103
+ return if index && check_index_options?(index)
104
+
105
+ raise Errors::IndexNotFound.new(columns, where_clause, index_name, adapter.indexes, adapter.table_name)
96
106
  end
97
107
  end
98
108
  end
@@ -1,3 +1,3 @@
1
1
  module DatabaseValidations
2
- VERSION = '0.8.9'.freeze
2
+ VERSION = '0.8.10'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: database_validations
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.9
4
+ version: 0.8.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Evgeniy Demin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-02-13 00:00:00.000000000 Z
11
+ date: 2019-02-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -16,20 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '3.2'
20
- - - "<"
21
- - !ruby/object:Gem::Version
22
- version: '6'
19
+ version: 4.2.0
23
20
  type: :runtime
24
21
  prerelease: false
25
22
  version_requirements: !ruby/object:Gem::Requirement
26
23
  requirements:
27
24
  - - ">="
28
25
  - !ruby/object:Gem::Version
29
- version: '3.2'
30
- - - "<"
31
- - !ruby/object:Gem::Version
32
- version: '6'
26
+ version: 4.2.0
33
27
  - !ruby/object:Gem::Dependency
34
28
  name: benchmark-ips
35
29
  requirement: !ruby/object:Gem::Requirement
@@ -62,30 +56,30 @@ dependencies:
62
56
  name: mysql2
63
57
  requirement: !ruby/object:Gem::Requirement
64
58
  requirements:
65
- - - "~>"
59
+ - - ">="
66
60
  - !ruby/object:Gem::Version
67
- version: '0.5'
61
+ version: '0'
68
62
  type: :development
69
63
  prerelease: false
70
64
  version_requirements: !ruby/object:Gem::Requirement
71
65
  requirements:
72
- - - "~>"
66
+ - - ">="
73
67
  - !ruby/object:Gem::Version
74
- version: '0.5'
68
+ version: '0'
75
69
  - !ruby/object:Gem::Dependency
76
70
  name: pg
77
71
  requirement: !ruby/object:Gem::Requirement
78
72
  requirements:
79
- - - "~>"
73
+ - - ">="
80
74
  - !ruby/object:Gem::Version
81
- version: '1.1'
75
+ version: '0'
82
76
  type: :development
83
77
  prerelease: false
84
78
  version_requirements: !ruby/object:Gem::Requirement
85
79
  requirements:
86
- - - "~>"
80
+ - - ">="
87
81
  - !ruby/object:Gem::Version
88
- version: '1.1'
82
+ version: '0'
89
83
  - !ruby/object:Gem::Dependency
90
84
  name: rake
91
85
  requirement: !ruby/object:Gem::Requirement
@@ -148,14 +142,14 @@ dependencies:
148
142
  requirements:
149
143
  - - "~>"
150
144
  - !ruby/object:Gem::Version
151
- version: '1.3'
145
+ version: 1.3.0
152
146
  type: :development
153
147
  prerelease: false
154
148
  version_requirements: !ruby/object:Gem::Requirement
155
149
  requirements:
156
150
  - - "~>"
157
151
  - !ruby/object:Gem::Version
158
- version: '1.3'
152
+ version: 1.3.0
159
153
  description: |-
160
154
  ActiveRecord provides validations on app level but it won't guarantee the
161
155
  consistent. In some cases, like `validates_uniqueness_of` it executes