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 +4 -4
- data/README.md +7 -24
- data/lib/mobility_uniqueness/version.rb +1 -1
- data/lib/mobility_uniqueness.rb +27 -22
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9de231d19a846a5f9894f62d1a66308022d943ff1ec9a4f43b636a3ad8313a14
|
4
|
+
data.tar.gz: edf9256ed4c4f2e621b7b363ce7adbda7918a4cc94c48de5939065abdacdc2e5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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 :
|
25
|
-
translates :description, type: :text
|
24
|
+
translates :title, type: :string
|
26
25
|
|
27
|
-
validates_uniqueness_of_translated :
|
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
|
32
|
+
You can customize the error message:
|
34
33
|
```rb
|
35
34
|
class Book < ApplicationRecord
|
36
35
|
extend Mobility
|
37
36
|
|
38
|
-
translates :
|
39
|
-
translates :description, type: :text
|
37
|
+
translates :title, type: :string
|
40
38
|
|
41
|
-
validates_uniqueness_of_translated :
|
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
|
|
data/lib/mobility_uniqueness.rb
CHANGED
@@ -5,26 +5,33 @@ module MobilityUniqueness
|
|
5
5
|
def validates_uniqueness_of_translated(*args)
|
6
6
|
return unless defined?(Mobility)
|
7
7
|
|
8
|
-
|
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
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
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
|
-
|
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.
|
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
|
+
date: 2024-12-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|