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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cf962cf510a2222aeed27108d93fbbfd097a51e3f077c83e2d5763b392741c98
4
- data.tar.gz: 77132ee3687ab614fabc2c8a962ab0e10677cec70dbb0720b47685f7d5e1797a
3
+ metadata.gz: 710c0ea5d6e02ee1cda673303e2b30d61993321a3fc3655c84b4a231b9267c36
4
+ data.tar.gz: '029a9ebe04b453422eb6018d32421ae9ba390cc8ecad29f2e22eb7591f2568b0'
5
5
  SHA512:
6
- metadata.gz: 4da1178c863c73fb38a99a56e41ef8b522cf88f6c08f5457ca74c6d5e43d78074250294c6fa9cda7bdaf62af8e6f06f540a90118b105fe42aa12c31a1eaff1bc
7
- data.tar.gz: 78fa453e34a958965c43006ab2da241245015dbf7ae5ffba4c3e9a6b399502bad2c96f575c6a79abf7942aa8a23bbce536070ce0d68c64db7023d56bbb4cc6bd
6
+ metadata.gz: 1f53e82506ceada51bd9db3bad9ee12d45621d2ee045103b8bc9d82a7a7a9de497367f165a4eab3cfda81cf453fa071cb86ac2d9385bf2885ff28e07fad6180b
7
+ data.tar.gz: 0a382359b1bb564e4462f88fbd576ef51c6317bf9e7bc6c28f6221f872060c104c2c10b7d44c06c1d05ae4a1210c7bc2cdd8917f6fcfaaae170fd51f481d56c5
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MobilityUniqueness
4
- VERSION = '0.1.1'
4
+ VERSION = '0.1.2'
5
5
  end
@@ -10,29 +10,21 @@ module MobilityUniqueness
10
10
  before_validation do
11
11
  message = opitons[:message] || 'violates uniqueness constraint'
12
12
 
13
- Mobility.available_locales.each do |locale|
14
- args.each do |attr|
15
- passed = false
16
- query_class = self.mobility_query_class(attr)
17
-
18
- next unless query_class
19
-
20
- records = query_class
21
- .where(locale: locale)
22
- .where(translatable_type: self.class.to_s)
23
- .where(key: attr, value: send(:"#{attr}_#{locale}"))
24
-
25
- count = records.count
26
-
27
- passed = if self.persisted?
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mobility_uniqueness
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - egemen öztürk