mobility_uniqueness 0.1.1 → 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: cf962cf510a2222aeed27108d93fbbfd097a51e3f077c83e2d5763b392741c98
4
- data.tar.gz: 77132ee3687ab614fabc2c8a962ab0e10677cec70dbb0720b47685f7d5e1797a
3
+ metadata.gz: 9de231d19a846a5f9894f62d1a66308022d943ff1ec9a4f43b636a3ad8313a14
4
+ data.tar.gz: edf9256ed4c4f2e621b7b363ce7adbda7918a4cc94c48de5939065abdacdc2e5
5
5
  SHA512:
6
- metadata.gz: 4da1178c863c73fb38a99a56e41ef8b522cf88f6c08f5457ca74c6d5e43d78074250294c6fa9cda7bdaf62af8e6f06f540a90118b105fe42aa12c31a1eaff1bc
7
- data.tar.gz: 78fa453e34a958965c43006ab2da241245015dbf7ae5ffba4c3e9a6b399502bad2c96f575c6a79abf7942aa8a23bbce536070ce0d68c64db7023d56bbb4cc6bd
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.1'
4
+ VERSION = '0.1.3'
5
5
  end
@@ -5,34 +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
+ query_classes = args.group_by { |attr| mobility_query_class(attr) }
13
+ query_classes.each do |query_class, attributes|
14
+ next unless query_class
12
15
 
13
- Mobility.available_locales.each do |locale|
14
- args.each do |attr|
15
- passed = false
16
- query_class = self.mobility_query_class(attr)
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
17
19
 
18
- next unless query_class
20
+ next if values.empty?
19
21
 
20
- records = query_class
21
- .where(locale: locale)
22
+ # Checking for conflicts in the database
23
+ conflicts = query_class
24
+ .where(locale: locale) # Only compare values in the same locale
22
25
  .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
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
36
35
  end
37
36
  end
38
37
  end
@@ -40,18 +39,16 @@ module MobilityUniqueness
40
39
  end
41
40
 
42
41
  def mobility_query_class(attr)
43
- attr_type = self.class.attribute_types[attr&.to_s].type
44
-
42
+ # Dynamically retrieve the appropriate query class based on attribute type
43
+ attr_type = self.class.attribute_types[attr.to_s]&.type
45
44
  translations = {
46
- string: Mobility::Backends::ActiveRecord::KeyValue::StringTranslation,
47
- text: Mobility::Backends::ActiveRecord::KeyValue::TextTranslation
45
+ string: Mobility::Backends::ActiveRecord::KeyValue::StringTranslation
48
46
  }
49
-
50
- translations[attr_type]
47
+
48
+ translations[attr_type] || raise(ArgumentError, "Unsupported attribute type for #{attr}")
51
49
  end
52
50
  end
53
51
 
54
- # Automatically include the in all ActiveRecord models
55
52
  ActiveSupport.on_load(:active_record) do
56
53
  include MobilityUniqueness
57
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.1
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