unique_nested_validator 1.0.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.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +52 -0
- data/Rakefile +5 -0
- data/lib/unique_nested_validator/version.rb +3 -0
- data/lib/unique_nested_validator.rb +10 -0
- data/spec/spec_helper.rb +2 -0
- data/spec/unique_nested_validator_spec.rb +59 -0
- data/tasks/rspec.rake +3 -0
- data/unique_nested_validator.gemspec +27 -0
- metadata +146 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Estevan Vedovelli
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
# UniqueNestedValidator
|
2
|
+
|
3
|
+
This validator allows you to verify the uniqueness of attributes from nested models.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'unique_nested_validator'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install unique_nested_validator
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
|
22
|
+
Let's say you have a model for `Runner`:
|
23
|
+
|
24
|
+
class Runner < ActiveRecord::Base
|
25
|
+
attr_accessible :email
|
26
|
+
|
27
|
+
Then you create a `Race` and fill in runners using nested forms. In your `Race` model you will accept nested attributes for the nested model `Runner`:
|
28
|
+
|
29
|
+
class Race < ActiveRecord::Base
|
30
|
+
has_many :runners, :dependent => :destroy
|
31
|
+
accepts_nested_attributes_for :products
|
32
|
+
|
33
|
+
If we want to warrant that there aren't two runners with the same id, so we add the following validation to `Race`:
|
34
|
+
|
35
|
+
validates :products, :unique_nested => true
|
36
|
+
|
37
|
+
Or, if we want to warrant there aren't repeated emails for runners we add the following validation:
|
38
|
+
|
39
|
+
validates :products, :unique_nested => { :with => :email }
|
40
|
+
|
41
|
+
And it is possible to set a custom error message:
|
42
|
+
|
43
|
+
validates :products, :unique_nested => { :with => :email, :message => "Two runners cannot have the same email" }
|
44
|
+
|
45
|
+
|
46
|
+
## Contributing
|
47
|
+
|
48
|
+
1. Fork it
|
49
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
50
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
51
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
52
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
class UniqueNestedValidator < ActiveModel::EachValidator
|
2
|
+
def validate_each(record, attribute, value)
|
3
|
+
if not attribute.empty?
|
4
|
+
with = options[:with] || :id
|
5
|
+
if value.collect{|c| c[with]}.uniq.length != value.length
|
6
|
+
record.errors.add(attribute, options[:message] || "#{I18n.t('unique_nested_validator.errors.cannot_repeat', default: 'cannot repeat')} #{I18n.t(with.to_s.pluralize, scope: [:unique_nested_validator, :keys], default: with.to_s.pluralize)}")
|
7
|
+
end
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class TestModel
|
4
|
+
include ActiveModel::Validations
|
5
|
+
def initialize(attributes = {})
|
6
|
+
@attributes = attributes
|
7
|
+
end
|
8
|
+
def read_attribute_for_validation(key)
|
9
|
+
@attributes[key]
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
class TV < TestModel
|
14
|
+
validates :channels, :unique_nested => { :with => :number }
|
15
|
+
end
|
16
|
+
|
17
|
+
class Race < TestModel
|
18
|
+
validates :runners, :unique_nested => true
|
19
|
+
end
|
20
|
+
|
21
|
+
class TVWithErrorMessage < TestModel
|
22
|
+
validates :channels, :unique_nested => { :with => :number, :message => "must be different" }
|
23
|
+
end
|
24
|
+
|
25
|
+
|
26
|
+
describe UniqueNestedValidator do
|
27
|
+
|
28
|
+
describe 'validation of uniqueness of nested attributes' do
|
29
|
+
it 'should fails when attributes are the same' do
|
30
|
+
expect(TV.new(:channels => [{number: "nine"}, {number: "nine"}])).not_to be_valid
|
31
|
+
end
|
32
|
+
it 'should not fail when attributes are different' do
|
33
|
+
expect(TV.new(:channels => [{number: "eight"}, {number: "seven"}])).to be_valid
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
describe 'validation of uniqueness of nested ids' do
|
38
|
+
it 'should fails when nested ids are the same' do
|
39
|
+
expect(Race.new(:runners => [{id: 4554}, {id: 4554}])).not_to be_valid
|
40
|
+
end
|
41
|
+
it 'should not fail when nested ids are different' do
|
42
|
+
expect(Race.new(:runners => [{id: 6332}, {id: 990}])).to be_valid
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
describe 'error messages' do
|
47
|
+
it 'should be default when the message is not defined' do
|
48
|
+
tv = TV.new(:channels => [{number: "ten"}, {number: "ten"}])
|
49
|
+
tv.valid?
|
50
|
+
expect(tv.errors.messages).to match({:channels => ["cannot repeat numbers"]})
|
51
|
+
end
|
52
|
+
it 'should be the provided message when it is defined' do
|
53
|
+
tv = TVWithErrorMessage.new(:channels => [{number: "ten"}, {number: "ten"}])
|
54
|
+
tv.valid?
|
55
|
+
expect(tv.errors.messages).to match({:channels => ["must be different"]})
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
data/tasks/rspec.rake
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'unique_nested_validator/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "unique_nested_validator"
|
8
|
+
spec.version = UniqueNested::VERSION
|
9
|
+
spec.authors = ["Estevan Vedovelli"]
|
10
|
+
spec.email = ["evedovelli@gmail.com"]
|
11
|
+
spec.description = %q{This validator allows you to verify nested models created with nested forms to validate the uniqueness of an attribute, More details in http://github.com/balexand/email_validator}
|
12
|
+
spec.summary = %q{A validator for warranting the uniqueness of nested model attributes}
|
13
|
+
spec.homepage = "http://github.com/balexand/email_validator"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^spec/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency "activemodel", ">= 0"
|
22
|
+
spec.add_dependency "i18n"
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
25
|
+
spec.add_development_dependency "rake"
|
26
|
+
spec.add_development_dependency "rspec"
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,146 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: unique_nested_validator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Estevan Vedovelli
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2015-02-27 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activemodel
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: i18n
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: bundler
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ~>
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '1.3'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.3'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: rake
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: rspec
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
description: This validator allows you to verify nested models created with nested
|
95
|
+
forms to validate the uniqueness of an attribute, More details in http://github.com/balexand/email_validator
|
96
|
+
email:
|
97
|
+
- evedovelli@gmail.com
|
98
|
+
executables: []
|
99
|
+
extensions: []
|
100
|
+
extra_rdoc_files: []
|
101
|
+
files:
|
102
|
+
- .gitignore
|
103
|
+
- Gemfile
|
104
|
+
- LICENSE.txt
|
105
|
+
- README.md
|
106
|
+
- Rakefile
|
107
|
+
- lib/unique_nested_validator.rb
|
108
|
+
- lib/unique_nested_validator/version.rb
|
109
|
+
- spec/spec_helper.rb
|
110
|
+
- spec/unique_nested_validator_spec.rb
|
111
|
+
- tasks/rspec.rake
|
112
|
+
- unique_nested_validator.gemspec
|
113
|
+
homepage: http://github.com/balexand/email_validator
|
114
|
+
licenses:
|
115
|
+
- MIT
|
116
|
+
post_install_message:
|
117
|
+
rdoc_options: []
|
118
|
+
require_paths:
|
119
|
+
- lib
|
120
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
121
|
+
none: false
|
122
|
+
requirements:
|
123
|
+
- - ! '>='
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: '0'
|
126
|
+
segments:
|
127
|
+
- 0
|
128
|
+
hash: -633055405
|
129
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
130
|
+
none: false
|
131
|
+
requirements:
|
132
|
+
- - ! '>='
|
133
|
+
- !ruby/object:Gem::Version
|
134
|
+
version: '0'
|
135
|
+
segments:
|
136
|
+
- 0
|
137
|
+
hash: -633055405
|
138
|
+
requirements: []
|
139
|
+
rubyforge_project:
|
140
|
+
rubygems_version: 1.8.24
|
141
|
+
signing_key:
|
142
|
+
specification_version: 3
|
143
|
+
summary: A validator for warranting the uniqueness of nested model attributes
|
144
|
+
test_files:
|
145
|
+
- spec/spec_helper.rb
|
146
|
+
- spec/unique_nested_validator_spec.rb
|