mobility_uniqueness 0.1.0 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rspec +1 -0
- data/README.md +6 -0
- data/Rakefile +1 -1
- data/lib/mobility_uniqueness/version.rb +1 -1
- data/lib/mobility_uniqueness.rb +16 -34
- metadata +66 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 710c0ea5d6e02ee1cda673303e2b30d61993321a3fc3655c84b4a231b9267c36
|
4
|
+
data.tar.gz: '029a9ebe04b453422eb6018d32421ae9ba390cc8ecad29f2e22eb7591f2568b0'
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1f53e82506ceada51bd9db3bad9ee12d45621d2ee045103b8bc9d82a7a7a9de497367f165a4eab3cfda81cf453fa071cb86ac2d9385bf2885ff28e07fad6180b
|
7
|
+
data.tar.gz: 0a382359b1bb564e4462f88fbd576ef51c6317bf9e7bc6c28f6221f872060c104c2c10b7d44c06c1d05ae4a1210c7bc2cdd8917f6fcfaaae170fd51f481d56c5
|
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--require spec_helper
|
data/README.md
CHANGED
data/Rakefile
CHANGED
data/lib/mobility_uniqueness.rb
CHANGED
@@ -10,45 +10,27 @@ module MobilityUniqueness
|
|
10
10
|
before_validation do
|
11
11
|
message = opitons[:message] || 'violates uniqueness constraint'
|
12
12
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
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
|
39
31
|
end
|
40
32
|
end
|
41
33
|
|
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
34
|
def mobility_query_class(attr)
|
53
35
|
attr_type = self.class.attribute_types[attr&.to_s].type
|
54
36
|
|
@@ -56,7 +38,7 @@ module MobilityUniqueness
|
|
56
38
|
string: Mobility::Backends::ActiveRecord::KeyValue::StringTranslation,
|
57
39
|
text: Mobility::Backends::ActiveRecord::KeyValue::TextTranslation
|
58
40
|
}
|
59
|
-
|
41
|
+
|
60
42
|
translations[attr_type]
|
61
43
|
end
|
62
44
|
end
|
metadata
CHANGED
@@ -1,15 +1,77 @@
|
|
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.2
|
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-
|
12
|
-
dependencies:
|
11
|
+
date: 2024-11-17 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activerecord
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '7.2'
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 7.2.2
|
23
|
+
type: :development
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - "~>"
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '7.2'
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 7.2.2
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: mobility
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
type: :development
|
41
|
+
prerelease: false
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: rspec
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0'
|
61
|
+
- !ruby/object:Gem::Dependency
|
62
|
+
name: sqlite3
|
63
|
+
requirement: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '0'
|
68
|
+
type: :development
|
69
|
+
prerelease: false
|
70
|
+
version_requirements: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
13
75
|
description: " MobilityUniqueness extends the Mobility gem with locale-specific
|
14
76
|
uniqueness validation \n for translated attributes in ActiveRecord models. It
|
15
77
|
allows you to ensure that a given attribute, \n such as `name` in different locales,
|
@@ -23,6 +85,7 @@ executables: []
|
|
23
85
|
extensions: []
|
24
86
|
extra_rdoc_files: []
|
25
87
|
files:
|
88
|
+
- ".rspec"
|
26
89
|
- LICENSE.txt
|
27
90
|
- README.md
|
28
91
|
- Rakefile
|