validates_unique_values_on_association 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: cd3cffe7f2c414c0cc097646d1f9ec4724a7a9af93fdf12ff5f164922a9f7463
4
+ data.tar.gz: '099f61a57450a877b0156badd395b5f1adbb542497c345ff7b33e21b84b4c97f'
5
+ SHA512:
6
+ metadata.gz: 3654edfb5f0eaf74d9c114cd2ba24ac36068296d581fe6cf5fe9d2ba7b9e10a279b9876295dc00380b95b89811d64e1be8c64795e9a484db773ccb0fb6aa513f
7
+ data.tar.gz: 1f5a02a4b35ed0e92e0cd4ea26942639da8874bd772f2b4ac5a49ff0fc7f9240064d24de0073f3dfd1e78ed0d2e486f267f076eb450d84d4e01a30229f80b260
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2021 kaspernj
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,28 @@
1
+ # ValidatesUniqueValuesOnAssociation
2
+ Short description and motivation.
3
+
4
+ ## Usage
5
+ How to use my plugin.
6
+
7
+ ## Installation
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'validates_unique_values_on_association'
12
+ ```
13
+
14
+ And then execute:
15
+ ```bash
16
+ $ bundle
17
+ ```
18
+
19
+ Or install it yourself as:
20
+ ```bash
21
+ $ gem install validates_unique_values_on_association
22
+ ```
23
+
24
+ ## Contributing
25
+ Contribution directions go here.
26
+
27
+ ## License
28
+ 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,13 @@
1
+ require "bundler/setup"
2
+
3
+ require "bundler/gem_tasks"
4
+
5
+ require "rake/testtask"
6
+
7
+ Rake::TestTask.new(:test) do |t|
8
+ t.libs << "test"
9
+ t.pattern = "test/**/*_test.rb"
10
+ t.verbose = false
11
+ end
12
+
13
+ task default: :test
@@ -0,0 +1,5 @@
1
+ da:
2
+ activerecord:
3
+ errors:
4
+ messages:
5
+ hadnt_unique_values: "havde ikke unikke værdier: %{joined_duplicates}"
@@ -0,0 +1,5 @@
1
+ en:
2
+ activerecord:
3
+ errors:
4
+ messages:
5
+ hadnt_unique_values: "hadn't unique values: %{joined_duplicates}"
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :validates_unique_values_on_association do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,27 @@
1
+ class UniqueValuesOnAssociationValidator < ActiveModel::EachValidator
2
+ def validate_each(object, attribute, _value) # rubocop:disable Metrics/AbcSize
3
+ association = object.association(attribute)
4
+ target = association.target
5
+ child_attribute_name = options.fetch(:attribute)
6
+
7
+ return if target.blank?
8
+
9
+ values = []
10
+ duplicates = []
11
+
12
+ target.each do |child|
13
+ child_attribute_value = child[child_attribute_name]
14
+
15
+ if values.include?(child_attribute_value)
16
+ duplicates << child_attribute_value
17
+ child.errors.add(child_attribute_name, options[:message] || :taken)
18
+ end
19
+
20
+ values << child_attribute_value
21
+ end
22
+
23
+ if duplicates.any?
24
+ object.errors.add(attribute, :hadnt_unique_values, joined_duplicates: duplicates.join(", "), **options)
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,25 @@
1
+ require_relative "validates_unique_values_on_association/version"
2
+ require_relative "validates_unique_values_on_association/railtie"
3
+ require_relative "unique_values_on_association_validator"
4
+
5
+ module ValidatesUniqueValuesOnAssociation
6
+ def self.locale_files # rubocop:disable Metrics/AbcSize
7
+ files = []
8
+
9
+ available_locales = []
10
+ available_locales += I18n.available_locales if I18n.available_locales
11
+
12
+ if Rails.application.config.i18n.available_locales
13
+ available_locales += Rails.application.config.i18n.available_locales
14
+ end
15
+
16
+ available_locales.uniq!
17
+
18
+ available_locales.each do |locale|
19
+ path = "#{File.realpath("#{__dir__}/../config/locales")}/#{locale}.yml"
20
+ files << File.realpath(path) if File.exist?(path)
21
+ end
22
+
23
+ files
24
+ end
25
+ end
@@ -0,0 +1,4 @@
1
+ module ValidatesUniqueValuesOnAssociation; end
2
+
3
+ class ValidatesUniqueValuesOnAssociation::Railtie # rubocop:disable Lint/EmptyClass
4
+ end
@@ -0,0 +1,3 @@
1
+ module ValidatesUniqueValuesOnAssociation
2
+ VERSION = "0.1.0".freeze
3
+ end
metadata ADDED
@@ -0,0 +1,155 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: validates_unique_values_on_association
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - kaspernj
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-05-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 6.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 6.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: factory_bot_rails
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec-rails
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop-performance
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop-rails
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubocop-rspec
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: Validate if an association has unique values on a specific attribute.
112
+ email:
113
+ - k@spernj.org
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - MIT-LICENSE
119
+ - README.md
120
+ - Rakefile
121
+ - config/locales/da.yml
122
+ - config/locales/en.yml
123
+ - lib/tasks/unique_values_validator_tasks.rake
124
+ - lib/unique_values_on_association_validator.rb
125
+ - lib/validates_unique_values_on_association.rb
126
+ - lib/validates_unique_values_on_association/railtie.rb
127
+ - lib/validates_unique_values_on_association/version.rb
128
+ homepage: https://github.com/kaspernj/validates_unique_values_on_association
129
+ licenses:
130
+ - MIT
131
+ metadata:
132
+ allowed_push_host: https://rubygems.org
133
+ homepage_uri: https://github.com/kaspernj/validates_unique_values_on_association
134
+ source_code_uri: https://github.com/kaspernj/validates_unique_values_on_association
135
+ changelog_uri: https://github.com/kaspernj/validates_unique_values_on_association
136
+ post_install_message:
137
+ rdoc_options: []
138
+ require_paths:
139
+ - lib
140
+ required_ruby_version: !ruby/object:Gem::Requirement
141
+ requirements:
142
+ - - ">="
143
+ - !ruby/object:Gem::Version
144
+ version: '2.5'
145
+ required_rubygems_version: !ruby/object:Gem::Requirement
146
+ requirements:
147
+ - - ">="
148
+ - !ruby/object:Gem::Version
149
+ version: '0'
150
+ requirements: []
151
+ rubygems_version: 3.1.6
152
+ signing_key:
153
+ specification_version: 4
154
+ summary: Validate if an association has unique values on a specific attribute.
155
+ test_files: []