mobility_uniqueness 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE.txt +21 -0
- data/README.md +75 -0
- data/Rakefile +4 -0
- data/lib/mobility_uniqueness/version.rb +5 -0
- data/lib/mobility_uniqueness.rb +67 -0
- data/sig/mobility_uniqueness.rbs +4 -0
- metadata +59 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 3dd8ef47b233d926aeeb738ea25c04dca1cb224447ba5345eab060bc28b2ed10
|
4
|
+
data.tar.gz: 03741c9831894edd706b383e706696d5fa070464d8c29c99aeecc1f4dd64c521
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: fc4928e9848303209517c4228e9f52dc46946bbce771aa1c975e548abf6b0404b0d58835346c83fffdd86dfde6b7b30ff8482f9099c0afa3d1c3b7e7e7571cc4
|
7
|
+
data.tar.gz: 86ee2f4453aad4980f65988e2f0e168b99a5976d3dad05f1f328122cdd580c2a452e190da82e1a2231bea17d41eed890e1b1b1e28ee1e885eb67ae7d1190738d
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2024 “egemen_ozturk”
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
# Mobility Uniqueness
|
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.
|
4
|
+
|
5
|
+
## Methods
|
6
|
+
* `validates_uniqueness_of_translated(*args, message: 'custom message')` - Validates that the specified attributes are unique across all locales
|
7
|
+
|
8
|
+
Default error message is _'violates uniqueness constraint'_ if the error message is not specified.
|
9
|
+
|
10
|
+
## Usage
|
11
|
+
|
12
|
+
### Basic Usage
|
13
|
+
|
14
|
+
```rb
|
15
|
+
class Book < ApplicationRecord
|
16
|
+
extend Mobility
|
17
|
+
|
18
|
+
translates :name, type: :string
|
19
|
+
translates :description, type: :text
|
20
|
+
|
21
|
+
validates_uniqueness_of_translated :name, :description
|
22
|
+
end
|
23
|
+
```
|
24
|
+
|
25
|
+
### Custom Error Messages
|
26
|
+
|
27
|
+
You can customize the error message for all:
|
28
|
+
```rb
|
29
|
+
class Book < ApplicationRecord
|
30
|
+
extend Mobility
|
31
|
+
|
32
|
+
translates :name, type: :string
|
33
|
+
translates :description, type: :text
|
34
|
+
|
35
|
+
validates_uniqueness_of_translated :name, :description, message: 'custom message'
|
36
|
+
end
|
37
|
+
```
|
38
|
+
|
39
|
+
### Attribute-Specific Error Messages
|
40
|
+
|
41
|
+
Specify different error messages per attribute for more granular feedback:
|
42
|
+
```rb
|
43
|
+
class Book < ApplicationRecord
|
44
|
+
extend Mobility
|
45
|
+
|
46
|
+
translates :name, type: :string
|
47
|
+
translates :description, type: :text
|
48
|
+
|
49
|
+
validates_uniqueness_of_translated :name, message: 'name is not unique'
|
50
|
+
validates_uniqueness_of_translated :description, message: 'description should be unique'
|
51
|
+
end
|
52
|
+
```
|
53
|
+
|
54
|
+
## Purpose
|
55
|
+
This gem was initially developed as a Rails concern but was later extracted into a standalone gem to support reuse across projects. Its purpose is to address the common requirement of enforcing uniqueness for multilingual content, especially when data is stored in different locales. By encapsulating this functionality in a gem, developers can easily enforce unique translations without having to implement custom validation logic repeatedly.
|
56
|
+
|
57
|
+
As the Mobility gem maintainer stated in the issue comments that the gem has no support of validating uniqueness, so I went ahead and created this gem for all the developers who use mobility gem in their project.
|
58
|
+
|
59
|
+
Below mobility issues were my main motivation to create this gem.
|
60
|
+
|
61
|
+
* [Unique Validation didn't work with locale_accessors on every locales](https://github.com/shioyama/mobility/issues/603)
|
62
|
+
|
63
|
+
* [Uniqueness validation does not work](https://github.com/shioyama/mobility/issues/20)
|
64
|
+
|
65
|
+
# Summary
|
66
|
+
|
67
|
+
This gem provides an easy way to add uniqueness validation to translated fields in ActiveRecord models that use the Mobility gem. It ensures that translations are unique across locales, improving data integrity in multilingual applications.
|
68
|
+
|
69
|
+
## Contributing
|
70
|
+
|
71
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/egemen-dev/mobility_uniqueness.
|
72
|
+
|
73
|
+
## License
|
74
|
+
|
75
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
module MobilityUniqueness
|
2
|
+
extend ActiveSupport::Concern
|
3
|
+
|
4
|
+
class_methods do
|
5
|
+
def validates_uniqueness_of_translated(*args)
|
6
|
+
return unless defined?(Mobility)
|
7
|
+
|
8
|
+
opitons = args.extract_options!
|
9
|
+
|
10
|
+
before_validation do
|
11
|
+
message = opitons[:message] || 'violates uniqueness constraint'
|
12
|
+
|
13
|
+
self.current_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
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def current_locales
|
43
|
+
locales = []
|
44
|
+
|
45
|
+
Mobility.available_locales.each do |locale|
|
46
|
+
locales << locale if self.send("language_code_#{locale}")&.present?
|
47
|
+
end
|
48
|
+
|
49
|
+
locales
|
50
|
+
end
|
51
|
+
|
52
|
+
def mobility_query_class(attr)
|
53
|
+
attr_type = self.class.attribute_types[attr&.to_s].type
|
54
|
+
|
55
|
+
translations = {
|
56
|
+
string: Mobility::Backends::ActiveRecord::KeyValue::StringTranslation,
|
57
|
+
text: Mobility::Backends::ActiveRecord::KeyValue::TextTranslation
|
58
|
+
}
|
59
|
+
|
60
|
+
translations[attr_type]
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
# Automatically include the in all ActiveRecord models
|
65
|
+
ActiveSupport.on_load(:active_record) do
|
66
|
+
include MobilityUniqueness
|
67
|
+
end
|
metadata
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mobility_uniqueness
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- egemen öztürk
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2024-11-09 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: " MobilityUniqueness extends the Mobility gem with locale-specific
|
14
|
+
uniqueness validation \n for translated attributes in ActiveRecord models. It
|
15
|
+
allows you to ensure that a given attribute, \n such as `name` in different locales,
|
16
|
+
is unique within the specified locale. \n\n By using this validator, you can
|
17
|
+
enforce uniqueness for translations without needing to write \n complex custom
|
18
|
+
validation code in your Rails models. The gem works seamlessly with Mobility’s \n
|
19
|
+
\ `KeyValue` backend to query translations and check for duplicates.\n"
|
20
|
+
email:
|
21
|
+
- egemenwasd@gmail.com
|
22
|
+
executables: []
|
23
|
+
extensions: []
|
24
|
+
extra_rdoc_files: []
|
25
|
+
files:
|
26
|
+
- LICENSE.txt
|
27
|
+
- README.md
|
28
|
+
- Rakefile
|
29
|
+
- lib/mobility_uniqueness.rb
|
30
|
+
- lib/mobility_uniqueness/version.rb
|
31
|
+
- sig/mobility_uniqueness.rbs
|
32
|
+
homepage: https://github.com/egemen-dev/mobility_uniqueness
|
33
|
+
licenses:
|
34
|
+
- MIT
|
35
|
+
metadata:
|
36
|
+
allowed_push_host: https://rubygems.org
|
37
|
+
homepage_uri: https://github.com/egemen-dev/mobility_uniqueness
|
38
|
+
source_code_uri: https://github.com/egemen-dev/mobility_uniqueness
|
39
|
+
changelog_uri: https://github.com/egemen-dev/mobility_uniqueness
|
40
|
+
post_install_message:
|
41
|
+
rdoc_options: []
|
42
|
+
require_paths:
|
43
|
+
- lib
|
44
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: 3.0.0
|
49
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
requirements: []
|
55
|
+
rubygems_version: 3.5.3
|
56
|
+
signing_key:
|
57
|
+
specification_version: 4
|
58
|
+
summary: Adds locale-specific uniqueness validation for translated attributes in Mobility.
|
59
|
+
test_files: []
|