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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3dd8ef47b233d926aeeb738ea25c04dca1cb224447ba5345eab060bc28b2ed10
4
- data.tar.gz: 03741c9831894edd706b383e706696d5fa070464d8c29c99aeecc1f4dd64c521
3
+ metadata.gz: 710c0ea5d6e02ee1cda673303e2b30d61993321a3fc3655c84b4a231b9267c36
4
+ data.tar.gz: '029a9ebe04b453422eb6018d32421ae9ba390cc8ecad29f2e22eb7591f2568b0'
5
5
  SHA512:
6
- metadata.gz: fc4928e9848303209517c4228e9f52dc46946bbce771aa1c975e548abf6b0404b0d58835346c83fffdd86dfde6b7b30ff8482f9099c0afa3d1c3b7e7e7571cc4
7
- data.tar.gz: 86ee2f4453aad4980f65988e2f0e168b99a5976d3dad05f1f328122cdd580c2a452e190da82e1a2231bea17d41eed890e1b1b1e28ee1e885eb67ae7d1190738d
6
+ metadata.gz: 1f53e82506ceada51bd9db3bad9ee12d45621d2ee045103b8bc9d82a7a7a9de497367f165a4eab3cfda81cf453fa071cb86ac2d9385bf2885ff28e07fad6180b
7
+ data.tar.gz: 0a382359b1bb564e4462f88fbd576ef51c6317bf9e7bc6c28f6221f872060c104c2c10b7d44c06c1d05ae4a1210c7bc2cdd8917f6fcfaaae170fd51f481d56c5
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --require spec_helper
data/README.md CHANGED
@@ -9,6 +9,12 @@ Default error message is _'violates uniqueness constraint'_ if the error message
9
9
 
10
10
  ## Usage
11
11
 
12
+ Add it to your rails application like so;
13
+
14
+ ```
15
+ bundle add mobility_uniqueness
16
+ ```
17
+
12
18
  ### Basic Usage
13
19
 
14
20
  ```rb
data/Rakefile CHANGED
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "bundler/gem_tasks"
3
+ require 'bundler/gem_tasks'
4
4
  task default: %i[]
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MobilityUniqueness
4
- VERSION = "0.1.0"
4
+ VERSION = '0.1.2'
5
5
  end
@@ -10,45 +10,27 @@ module MobilityUniqueness
10
10
  before_validation do
11
11
  message = opitons[:message] || 'violates uniqueness constraint'
12
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
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.0
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-09 00:00:00.000000000 Z
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