mobility_uniqueness 0.1.2 → 0.1.3

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: 710c0ea5d6e02ee1cda673303e2b30d61993321a3fc3655c84b4a231b9267c36
4
- data.tar.gz: '029a9ebe04b453422eb6018d32421ae9ba390cc8ecad29f2e22eb7591f2568b0'
3
+ metadata.gz: 9de231d19a846a5f9894f62d1a66308022d943ff1ec9a4f43b636a3ad8313a14
4
+ data.tar.gz: edf9256ed4c4f2e621b7b363ce7adbda7918a4cc94c48de5939065abdacdc2e5
5
5
  SHA512:
6
- metadata.gz: 1f53e82506ceada51bd9db3bad9ee12d45621d2ee045103b8bc9d82a7a7a9de497367f165a4eab3cfda81cf453fa071cb86ac2d9385bf2885ff28e07fad6180b
7
- data.tar.gz: 0a382359b1bb564e4462f88fbd576ef51c6317bf9e7bc6c28f6221f872060c104c2c10b7d44c06c1d05ae4a1210c7bc2cdd8917f6fcfaaae170fd51f481d56c5
6
+ metadata.gz: cd0b4ce43780debe0ccb801c2b28a10ca1612e3e77fd48de0a76cb5f19e66a809f22f2844f5a609e0e4277123a7f0be6144910972ddb566d62601ed793f5ff60
7
+ data.tar.gz: 8493568a0641f00ae39d3e0b97a5f306d1f11bf3a5df1d54d2fb350e8f8fd89cb8584f62c95e46b7619f730befbc38b51b27dac1f02e89afde116a0943f2b801
data/README.md CHANGED
@@ -1,9 +1,9 @@
1
1
  # Mobility Uniqueness
2
2
 
3
- Mobility Uniqueness is a Ruby gem that extends the [Mobility](https://github.com/shioyama/mobility) gem by enabling uniqueness validation on translated attributes across multiple locales. With this gem, you can validate the uniqueness of translations on attributes in ActiveRecord models, ensuring consistency and preventing duplicate entries in different languages. It's designed to work seamlessly with **Mobility's default :key_value backend**, integrating directly into your model validations.
3
+ Mobility Uniqueness is a Ruby gem that extends the [Mobility](https://github.com/shioyama/mobility) gem by enabling uniqueness validation on translated string attributes across multiple locales. With this gem, you can validate the uniqueness of translations on attributes in ActiveRecord models, ensuring consistency and preventing duplicate entries in different languages. It's designed to work seamlessly with **Mobility's default :key_value backend**, integrating directly into your model validations.
4
4
 
5
5
  ## Methods
6
- * `validates_uniqueness_of_translated(*args, message: 'custom message')` - Validates that the specified attributes are unique across all locales
6
+ * `validates_uniqueness_of_translated(*args, message: 'custom message')` - Validates that the specified string attributes are unique across all locales
7
7
 
8
8
  Default error message is _'violates uniqueness constraint'_ if the error message is not specified.
9
9
 
@@ -21,39 +21,22 @@ bundle add mobility_uniqueness
21
21
  class Book < ApplicationRecord
22
22
  extend Mobility
23
23
 
24
- translates :name, type: :string
25
- translates :description, type: :text
24
+ translates :title, type: :string
26
25
 
27
- validates_uniqueness_of_translated :name, :description
26
+ validates_uniqueness_of_translated :title
28
27
  end
29
28
  ```
30
29
 
31
30
  ### Custom Error Messages
32
31
 
33
- You can customize the error message for all:
32
+ You can customize the error message:
34
33
  ```rb
35
34
  class Book < ApplicationRecord
36
35
  extend Mobility
37
36
 
38
- translates :name, type: :string
39
- translates :description, type: :text
37
+ translates :title, type: :string
40
38
 
41
- validates_uniqueness_of_translated :name, :description, message: 'custom message'
42
- end
43
- ```
44
-
45
- ### Attribute-Specific Error Messages
46
-
47
- Specify different error messages per attribute for more granular feedback:
48
- ```rb
49
- class Book < ApplicationRecord
50
- extend Mobility
51
-
52
- translates :name, type: :string
53
- translates :description, type: :text
54
-
55
- validates_uniqueness_of_translated :name, message: 'name is not unique'
56
- validates_uniqueness_of_translated :description, message: 'description should be unique'
39
+ validates_uniqueness_of_translated :title, message: 'custom message'
57
40
  end
58
41
  ```
59
42
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MobilityUniqueness
4
- VERSION = '0.1.2'
4
+ VERSION = '0.1.3'
5
5
  end
@@ -5,26 +5,33 @@ module MobilityUniqueness
5
5
  def validates_uniqueness_of_translated(*args)
6
6
  return unless defined?(Mobility)
7
7
 
8
- opitons = args.extract_options!
8
+ options = args.extract_options!
9
+ message = options[:message] || 'violates uniqueness constraint'
9
10
 
10
11
  before_validation do
11
- message = opitons[:message] || 'violates uniqueness constraint'
12
-
13
12
  query_classes = args.group_by { |attr| mobility_query_class(attr) }
14
13
  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?
14
+ next unless query_class
15
+
16
+ Mobility.available_locales.each do |locale|
17
+ # Retrieve values for the current locale and attribute
18
+ values = attributes.map { |attr| send(:"#{attr}_#{locale}") }.compact
19
+
20
+ next if values.empty?
21
+
22
+ # Checking for conflicts in the database
23
+ conflicts = query_class
24
+ .where(locale: locale) # Only compare values in the same locale
25
+ .where(translatable_type: self.class.to_s)
26
+ .where(key: attributes, value: values)
27
+ .where.not(translatable_id: id)
28
+ .group(:key, :locale, :value)
29
+ .count
30
+
31
+ # If there are conflicts, add a validation error for the key
32
+ conflicts.each do |(key, locale, _value), count|
33
+ errors.add(:"#{key}_#{locale}", message) unless count.zero?
34
+ end
28
35
  end
29
36
  end
30
37
  end
@@ -32,18 +39,16 @@ module MobilityUniqueness
32
39
  end
33
40
 
34
41
  def mobility_query_class(attr)
35
- attr_type = self.class.attribute_types[attr&.to_s].type
36
-
42
+ # Dynamically retrieve the appropriate query class based on attribute type
43
+ attr_type = self.class.attribute_types[attr.to_s]&.type
37
44
  translations = {
38
- string: Mobility::Backends::ActiveRecord::KeyValue::StringTranslation,
39
- text: Mobility::Backends::ActiveRecord::KeyValue::TextTranslation
45
+ string: Mobility::Backends::ActiveRecord::KeyValue::StringTranslation
40
46
  }
41
47
 
42
- translations[attr_type]
48
+ translations[attr_type] || raise(ArgumentError, "Unsupported attribute type for #{attr}")
43
49
  end
44
50
  end
45
51
 
46
- # Automatically include the in all ActiveRecord models
47
52
  ActiveSupport.on_load(:active_record) do
48
53
  include MobilityUniqueness
49
54
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mobility_uniqueness
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - egemen öztürk
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-11-17 00:00:00.000000000 Z
11
+ date: 2024-12-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord