database_validations 0.3.3 → 0.3.4
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:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5be0a1cf5c74a0aa6744a86a14aa6a81707dc767dac4009706d882a6bbd43f86
|
4
|
+
data.tar.gz: 1e278b7a7399acc9305465129c68f7e8970d4dc283d5586a8531cb8fd0103369
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5080c42455f88a031b2cbefd620637c3312b576d7ba1af781c1d179b362d93194cd3f892693a07cfbc666c6bf98911ab0f302a6b1ef8bacd9d1b6ea88159717c
|
7
|
+
data.tar.gz: 7f83a786582d700407fe95a092a3675e9444b7a25265379befd16972462d528c7f9784ea992480124c8d889dc3e9492520f44c9b0ca07fad08451e9766aafc02
|
@@ -7,7 +7,9 @@ module DatabaseValidations
|
|
7
7
|
|
8
8
|
def initialize(columns)
|
9
9
|
@columns = columns.map(&:to_s)
|
10
|
-
super "No unique index found with columns: #{self.columns}"
|
10
|
+
super "No unique index found with columns: #{self.columns}. "\
|
11
|
+
"Use ENV['SKIP_DB_UNIQUENESS_VALIDATOR_INDEX_CHECK']=true in case you want to skip the check. "\
|
12
|
+
"For example, when you run migrations."
|
11
13
|
end
|
12
14
|
end
|
13
15
|
|
@@ -18,12 +18,23 @@ module DatabaseValidations
|
|
18
18
|
.map!(&:to_s)
|
19
19
|
.sort!
|
20
20
|
|
21
|
-
options = instance.class
|
21
|
+
options = uniqueness_validators_options(instance.class)[columns]
|
22
22
|
|
23
23
|
error_options = options.except(:case_sensitive, :scope, :conditions, :attributes)
|
24
24
|
error_options[:value] = instance.public_send(options[:attributes])
|
25
25
|
|
26
26
|
instance.errors.add(options[:attributes], :taken, error_options)
|
27
27
|
end
|
28
|
+
|
29
|
+
def uniqueness_validators_options(klass)
|
30
|
+
validators_options = klass.instance_variable_get(:'@validates_db_uniqueness_opts') || {}
|
31
|
+
|
32
|
+
while klass.superclass.respond_to?(:validates_db_uniqueness_of)
|
33
|
+
validators_options.reverse_merge!(klass.superclass.instance_variable_get(:'@validates_db_uniqueness_opts') || {})
|
34
|
+
klass = klass.superclass
|
35
|
+
end
|
36
|
+
|
37
|
+
validators_options
|
38
|
+
end
|
28
39
|
end
|
29
40
|
end
|
@@ -8,7 +8,8 @@ module DatabaseValidations
|
|
8
8
|
def valid?(context = nil)
|
9
9
|
output = super(context)
|
10
10
|
|
11
|
-
|
11
|
+
|
12
|
+
DatabaseValidations::Helpers.uniqueness_validators_options(self.class).each_value do |opts|
|
12
13
|
validates_with(ActiveRecord::Validations::UniquenessValidator, opts.merge(allow_nil: true))
|
13
14
|
end
|
14
15
|
|
@@ -41,24 +42,19 @@ module DatabaseValidations
|
|
41
42
|
|
42
43
|
module ClassMethods
|
43
44
|
def validates_db_uniqueness_of(*attributes)
|
44
|
-
@
|
45
|
+
@validates_db_uniqueness_opts ||= {}
|
45
46
|
|
46
47
|
options = attributes.extract_options!
|
47
48
|
|
48
49
|
attributes.each do |attribute|
|
49
50
|
columns = [attribute, Array.wrap(options[:scope])].flatten!.map!(&:to_s).sort!
|
50
51
|
|
51
|
-
DatabaseValidations::Helpers.raise_if_index_missed!(self, columns)
|
52
|
+
DatabaseValidations::Helpers.raise_if_index_missed!(self, columns) unless ENV['SKIP_DB_UNIQUENESS_VALIDATOR_INDEX_CHECK']
|
52
53
|
|
53
|
-
@
|
54
|
+
@validates_db_uniqueness_opts[columns] = options.merge(attributes: attribute)
|
54
55
|
end
|
55
56
|
|
56
57
|
include(DatabaseUniquenessValidator)
|
57
58
|
end
|
58
|
-
|
59
|
-
def validates_db_uniqueness
|
60
|
-
derived = superclass.respond_to?(:validates_db_uniqueness) ? superclass.validates_db_uniqueness : {}
|
61
|
-
derived.merge(@validates_db_uniqueness || {})
|
62
|
-
end
|
63
59
|
end
|
64
60
|
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.3.
|
4
|
+
version: 0.3.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Evgeniy Demin
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-09-
|
11
|
+
date: 2018-09-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|