mobility_uniqueness 0.1.1 → 0.1.2
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 +4 -4
- data/lib/mobility_uniqueness/version.rb +1 -1
- data/lib/mobility_uniqueness.rb +16 -24
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 710c0ea5d6e02ee1cda673303e2b30d61993321a3fc3655c84b4a231b9267c36
|
4
|
+
data.tar.gz: '029a9ebe04b453422eb6018d32421ae9ba390cc8ecad29f2e22eb7591f2568b0'
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1f53e82506ceada51bd9db3bad9ee12d45621d2ee045103b8bc9d82a7a7a9de497367f165a4eab3cfda81cf453fa071cb86ac2d9385bf2885ff28e07fad6180b
|
7
|
+
data.tar.gz: 0a382359b1bb564e4462f88fbd576ef51c6317bf9e7bc6c28f6221f872060c104c2c10b7d44c06c1d05ae4a1210c7bc2cdd8917f6fcfaaae170fd51f481d56c5
|
data/lib/mobility_uniqueness.rb
CHANGED
@@ -10,29 +10,21 @@ module MobilityUniqueness
|
|
10
10
|
before_validation do
|
11
11
|
message = opitons[:message] || 'violates uniqueness constraint'
|
12
12
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
only_one_record = count <= 1
|
29
|
-
belongs_to_self = count.zero? || id == records.first.translatable_id
|
30
|
-
only_one_record && belongs_to_self
|
31
|
-
else
|
32
|
-
count.zero?
|
33
|
-
end
|
34
|
-
|
35
|
-
self.errors.add(:"#{attr}_#{locale}", message) unless passed
|
13
|
+
query_classes = args.group_by { |attr| mobility_query_class(attr) }
|
14
|
+
query_classes.each do |query_class, attributes|
|
15
|
+
values = Mobility.available_locales.map do |locale|
|
16
|
+
attributes.map { |attr| send(:"#{attr}_#{locale}") }
|
17
|
+
end.flatten
|
18
|
+
|
19
|
+
count_by_group = query_class.where(locale: Mobility.available_locales)
|
20
|
+
.where(translatable_type: self.class.to_s)
|
21
|
+
.where(key: attributes, value: values)
|
22
|
+
.where.not(translatable_id: id)
|
23
|
+
.group(:key, :locale, :value)
|
24
|
+
.count
|
25
|
+
|
26
|
+
count_by_group.each do |(key, locale, _value), count|
|
27
|
+
errors.add(:"#{key}_#{locale}", message) unless count.zero?
|
36
28
|
end
|
37
29
|
end
|
38
30
|
end
|
@@ -46,7 +38,7 @@ module MobilityUniqueness
|
|
46
38
|
string: Mobility::Backends::ActiveRecord::KeyValue::StringTranslation,
|
47
39
|
text: Mobility::Backends::ActiveRecord::KeyValue::TextTranslation
|
48
40
|
}
|
49
|
-
|
41
|
+
|
50
42
|
translations[attr_type]
|
51
43
|
end
|
52
44
|
end
|