validator_attachment 1.1.1 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/CHANGELOG +2 -0
- data/Manifest +4 -3
- data/README.rdoc +11 -11
- data/Rakefile +0 -13
- data/lib/validator_attachment.rb +4 -4
- data/{test → spec}/dummy_validator.rb +0 -0
- data/spec/spec_helper.rb +6 -0
- data/spec/validator_attachment_spec.rb +160 -0
- data/validator_attachment.gemspec +12 -19
- metadata +43 -20
- data/test/test_helper.rb +0 -9
- data/test/validator_attachment_test.rb +0 -155
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
ODIwYTJhNDE1YmMzZDcyMzRlYzBjOTNlNWYzODdmMjY3YTViYTc4OA==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
NTdmNzViMzZjN2JlMGU1MzE1N2Y1NTQwMWI3MjI2OTkzZWY1MjI4YQ==
|
7
|
+
SHA512:
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
ZjY2ZjE0ZWE4MzlkYjUxY2I0NDNlZjc2NTU5OGY5MzAzMzc2OTZiZTgyYTZk
|
10
|
+
YzEwYTVjYWE2NTdhMjJjYzdhNTUxYmVlMWVkYWU4ZjMzZjZkODVlYTRlZmRm
|
11
|
+
NGVlYTRlYTkxZmQyYTRmODcwN2UyMzcwN2EwZTQ1YjMwZDQ5ODg=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
ZDMxOGZjNjUzMWMxYTFiMDU4NmEyYTI4Mzg2MjAxODRiYmQ5MWNhNTg3MDZk
|
14
|
+
MTlhZWNmMGJiYzQ2YzU3ODEyOGM5YmNmMjBiMzVjOWNlNjQxNDViZDU1Y2Zh
|
15
|
+
ZmYxODhhM2VjMmM2OWI0OTg3NDRjN2RiNThhODE2ZTE2NjQ2OWE=
|
data/CHANGELOG
CHANGED
data/Manifest
CHANGED
@@ -3,8 +3,9 @@ MIT-LICENSE
|
|
3
3
|
README.rdoc
|
4
4
|
Rakefile
|
5
5
|
lib/validator_attachment.rb
|
6
|
-
|
7
|
-
|
8
|
-
|
6
|
+
lib/validator_attachment/version.rb
|
7
|
+
spec/dummy_validator.rb
|
8
|
+
spec/spec_helper.rb
|
9
|
+
spec/validator_attachment_spec.rb
|
9
10
|
validator_attachment.gemspec
|
10
11
|
Manifest
|
data/README.rdoc
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
= Validator Attachment
|
2
2
|
|
3
|
-
It works with Rails >=3.0. Helps me test my validations on Models with less code. Assuming that Rails validations are
|
3
|
+
It works with Rails >=4.0. (For Rails >=3.0 use version 1.x) Helps me test my validations on Models with less code. Assuming that Rails validations are
|
4
4
|
already tested, by Rails, then when I have a validation on an attribute of a Model, I just want to test whether the
|
5
5
|
validation is there. Nothing more.
|
6
6
|
|
@@ -14,8 +14,8 @@ Assuming that you have a Model like the following:
|
|
14
14
|
|
15
15
|
In your test:
|
16
16
|
|
17
|
-
|
18
|
-
|
17
|
+
it "last name has to be present" do
|
18
|
+
expect(ActiveModel::Validations::PresenceValidator.is_attached?(MyAwesomeModel, :last_name)).to be true
|
19
19
|
end
|
20
20
|
|
21
21
|
will test that your +last_name+ attribute on the model +MyAwesomeModel+ has a +:presence+ validation attached. Note
|
@@ -31,17 +31,17 @@ Optionally, you can also check whether the correct options are attached too. So,
|
|
31
31
|
|
32
32
|
In your test:
|
33
33
|
|
34
|
-
|
34
|
+
it "last name should not be of correct length" do
|
35
35
|
# you do not have to check for all options
|
36
|
-
|
37
|
-
|
36
|
+
it { expect(ActiveModel::Validations::LengthValidator.is_attached?(MyAwesomeModel, :last_name, { :minimum => 3 })).to be true }
|
37
|
+
it { expect(ActiveModel::Validations::LengthValidator.is_attached?(MyAwesomeModel, :last_name, { :maximum => 60 })).to be true }
|
38
38
|
|
39
39
|
# or you can check for all
|
40
|
-
|
40
|
+
it { expect(ActiveModel::Validations::LengthValidator.is_attached?(MyAwesomeModel, :last_name, { :minimum => 3, :maximum => 60 })).to be true }
|
41
41
|
|
42
42
|
# or you can ask for those and only those, i.e. exact content match
|
43
|
-
|
44
|
-
|
43
|
+
it { expect(!ActiveModel::Validations::LengthValidator.is_attached?(MyAwesomeModel, :last_name, { :maximum => 60 }, true)).to be true }
|
44
|
+
it { expect(ActiveModel::Validations::LengthValidator.is_attached?(MyAwesomeModel, :last_name, { :minimum > 60, :maximum => 60 }, true)).to be true }
|
45
45
|
end
|
46
46
|
|
47
47
|
Another example with +with_options+:
|
@@ -56,8 +56,8 @@ Another example with +with_options+:
|
|
56
56
|
You can test this as follows:
|
57
57
|
|
58
58
|
test "user admin validators" do
|
59
|
-
|
60
|
-
|
59
|
+
it { expect(ActiveModel::Validations::LengthValidator.is_attached?(User, :password, { :minimum => 10, :if => :is_admin? })).to be true }
|
60
|
+
it { exepct(ActiveModel::Validations::PresenceValidator.is_attached?(User, :username, { :if => :is_admin? })).to be true }
|
61
61
|
end
|
62
62
|
|
63
63
|
In the file {validator_attachment_test.rb}[https://github.com/pmatsinopoulos/validator_attachment/blob/master/test/validator_attachment_test.rb]
|
data/Rakefile
CHANGED
@@ -1,19 +1,6 @@
|
|
1
1
|
#!/usr/bin/env rake
|
2
2
|
require 'rubygems'
|
3
3
|
require 'rake'
|
4
|
-
require 'echoe'
|
5
|
-
|
6
|
-
Echoe.new('validator_attachment') do |p|
|
7
|
-
p.summary = "Is this ActiveModel Validator Used?"
|
8
|
-
p.description = "Checks whether an ActiveModel Validator is attached to an attribute of a Model and with which options"
|
9
|
-
p.url = "http://github.com/pmatsinopoulos/validator_attachment"
|
10
|
-
p.author = "Panayotis Matsinopoulos"
|
11
|
-
p.email = "panayotis@matsinopoulos.gr"
|
12
|
-
p.ignore_pattern = ["tmp/*", "script/*", "rdoc_output/*", "rdoc_output/js/*", "rdoc_output/images/*", "rdoc_output/ActiveModel/*", "rdoc_output/lib/*",
|
13
|
-
"yard_output/*", "yard_output/js/*", "yard_output/css/*", "Gemfile", "Gemfile.lock", "*.iml"]
|
14
|
-
p.runtime_dependencies = ["rails ~>3.0"]
|
15
|
-
p.development_dependencies = ["rails ~>3.0"]
|
16
|
-
end
|
17
4
|
|
18
5
|
Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each{ |ext| load ext }
|
19
6
|
|
data/lib/validator_attachment.rb
CHANGED
@@ -1,11 +1,11 @@
|
|
1
|
-
require
|
1
|
+
require "rails/all"
|
2
2
|
|
3
3
|
module ValidatorAttachment
|
4
4
|
|
5
5
|
# Given a +klass+ checks whether the target Validator is attached to
|
6
6
|
# the given +attribute+ of the model with class +klass+. It can also check
|
7
7
|
# whether validator is attached together with some +options+ and if these
|
8
|
-
# +options+ are the only ones used (given +
|
8
|
+
# +options+ are the only ones used (given +exact_match+ +true+)
|
9
9
|
#
|
10
10
|
# @param [Class] klass the Model class that uses the Validator
|
11
11
|
# @param [Symbol] attribute the attribute of the Model class that we want
|
@@ -18,7 +18,7 @@ module ValidatorAttachment
|
|
18
18
|
# should not have more or less options.
|
19
19
|
# @return [boolean] whether Validator is attached or not
|
20
20
|
#
|
21
|
-
# @see http://github.com/pmatsinopoulos/validator_attachment
|
21
|
+
# @see http://github.com/pmatsinopoulos/validator_attachment project home page for more details and examples of usage.
|
22
22
|
#
|
23
23
|
def is_attached?(klass, attribute, options=nil, exact_match=false)
|
24
24
|
validators = klass._validate_callbacks.map{|vc| vc.raw_filter}
|
@@ -39,7 +39,7 @@ module ValidatorAttachment
|
|
39
39
|
# The logic of comparison:
|
40
40
|
# I find the difference of +val.options+ to +options+ and then I remove from +val.options+ this difference.
|
41
41
|
# The remaining keys of +val.options+ should be hash equal to +options+
|
42
|
-
validators = validators.find_all{|val| val.options.present? && val.options.
|
42
|
+
validators = validators.find_all{|val| val.options.present? && val.options.merge(options) == val.options}
|
43
43
|
end
|
44
44
|
end
|
45
45
|
|
File without changes
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,160 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ValidatorAttachment do
|
4
|
+
module ValidatorAttachmentTest
|
5
|
+
class SampleModel
|
6
|
+
include ActiveModel::Validations
|
7
|
+
include ActiveRecord::Validations
|
8
|
+
|
9
|
+
validates :acceptance_attr_1, :acceptance => true
|
10
|
+
validates :acceptance_attr_2, :acceptance => { :allow_nil => true }
|
11
|
+
validates :acceptance_attr_3, :acceptance => { :allow_nil => false }
|
12
|
+
validates :acceptance_attr_4, :acceptance => { :accept => 'sure' }
|
13
|
+
validates :acceptance_attr_5, :acceptance => { :allow_nil => true, :accept => 'sure' }
|
14
|
+
|
15
|
+
validates :email_1, :confirmation => true
|
16
|
+
validates :email_2, :confirmation => true, :presence => true
|
17
|
+
|
18
|
+
validates :exclusion_1, :exclusion => { :in => %w(ab cd ef), :message => 'not allowed' }
|
19
|
+
|
20
|
+
validates :legacy_code, :format => { :with => /\Aa.+\d\z/, :message => 'wrong format' }
|
21
|
+
|
22
|
+
validates :size, :inclusion => { :in => %w(small medium large),
|
23
|
+
:message => "%{value} is not a valid size" }
|
24
|
+
|
25
|
+
validates :name, :length => { :minimum => 2 }
|
26
|
+
validates :bio, :length => { :maximum => 500 }
|
27
|
+
validates :password, :length => { :in => 6..20 }
|
28
|
+
validates :registration_number, :length => { :is => 6 }
|
29
|
+
validates :account_number, :length => { :minimum => 2, :maximum => 6, :wrong_length => 'wrong length, please try again' }
|
30
|
+
validates :student_id, :length => { :maximum => 10, :too_long => 'sorry, too long', :too_short => 'sorry, too short' }
|
31
|
+
validates :mama_telephone, :length => { :is => 8 }
|
32
|
+
|
33
|
+
validates :points, :numericality => true
|
34
|
+
validates :games_played, :numericality => { :only_integer => true }
|
35
|
+
validates :num_1, :numericality => { :greater_than => 1 }
|
36
|
+
validates :num_2, :numericality => { :greater_than_or_equal_to => 2 }
|
37
|
+
validates :num_3, :numericality => { :equal_to => 3 }
|
38
|
+
validates :num_4, :numericality => { :less_than => 4 }
|
39
|
+
validates :num_5, :numericality => { :less_than_or_equal_to => 5 }
|
40
|
+
validates :num_6, :numericality => { :odd => true }
|
41
|
+
validates :num_7, :numericality => { :even => true }
|
42
|
+
|
43
|
+
validates :identity_number, :uniqueness => true
|
44
|
+
validates :identity_number_1, :uniqueness => { :scope => :num_7 }
|
45
|
+
validates :identity_number_2, :uniqueness => { :case_sensitive => true}
|
46
|
+
|
47
|
+
validates :num_8, :presence => true, :if => :paid_with_card?
|
48
|
+
|
49
|
+
with_options :if => :is_admin? do |admin|
|
50
|
+
admin.validates :num_9, :length => { :minimum => 10 }
|
51
|
+
admin.validates :num_10, :presence => true
|
52
|
+
end
|
53
|
+
|
54
|
+
validates :my_facebook_email, :presence => true, :dummy => true
|
55
|
+
|
56
|
+
def persisted?
|
57
|
+
false
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
context 'ActiveModel::EachValidator' do
|
63
|
+
it 'responds to is_attached?' do
|
64
|
+
expect(ActiveModel::EachValidator).to respond_to(:is_attached?)
|
65
|
+
end
|
66
|
+
|
67
|
+
it 'responds to attached?' do
|
68
|
+
expect(ActiveModel::EachValidator).to respond_to(:attached?)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
context 'acceptance validator' do
|
73
|
+
it { expect(ActiveModel::Validations::AcceptanceValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :acceptance_attr_1)).to be true }
|
74
|
+
it { expect(ActiveModel::Validations::AcceptanceValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :acceptance_attr_2, {:allow_nil => true})).to be true }
|
75
|
+
it { expect(ActiveModel::Validations::AcceptanceValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :acceptance_attr_3, {:allow_nil => false})).to be true }
|
76
|
+
it { expect(ActiveModel::Validations::AcceptanceValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :acceptance_attr_4, {:accept => 'sure'})).to be true }
|
77
|
+
it { expect(ActiveModel::Validations::AcceptanceValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :acceptance_attr_5, {:accept => 'sure'})).to be true }
|
78
|
+
it { expect(!ActiveModel::Validations::AcceptanceValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :acceptance_attr_5, {:accept => 'sure'}, true)).to be true }
|
79
|
+
it { expect(ActiveModel::Validations::AcceptanceValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :acceptance_attr_5, {:accept => 'sure', :allow_nil => true}, true)).to be true }
|
80
|
+
end
|
81
|
+
|
82
|
+
context 'confirmation validator' do
|
83
|
+
it { expect(ActiveModel::Validations::ConfirmationValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :email_1)).to be true }
|
84
|
+
it { expect(!ActiveModel::Validations::ConfirmationValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :acceptance_attr_1)).to be true }
|
85
|
+
it { expect(ActiveModel::Validations::ConfirmationValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :email_2)).to be true }
|
86
|
+
end
|
87
|
+
|
88
|
+
context 'presence validator' do
|
89
|
+
it { expect(ActiveModel::Validations::PresenceValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :email_2)).to be true }
|
90
|
+
it { expect(ActiveModel::Validations::PresenceValidator.attached?(ValidatorAttachmentTest::SampleModel, :email_2)).to be true }
|
91
|
+
end
|
92
|
+
|
93
|
+
context 'exclusion validator' do
|
94
|
+
it { expect(!ActiveModel::Validations::ExclusionValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :email_2)).to be true }
|
95
|
+
it { expect(ActiveModel::Validations::ExclusionValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :exclusion_1, {:in => %w(ab cd ef)})).to be true }
|
96
|
+
it { expect(!ActiveModel::Validations::ExclusionValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :exclusion_1, {:in => %w(ab cd ef gh)})).to be true }
|
97
|
+
end
|
98
|
+
|
99
|
+
context 'format validator' do
|
100
|
+
it { expect(ActiveModel::Validations::FormatValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :legacy_code)).to be true }
|
101
|
+
it { expect(ActiveModel::Validations::FormatValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :legacy_code, {:message => 'wrong format'})).to be true }
|
102
|
+
it { expect(ActiveModel::Validations::FormatValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :legacy_code, {:with => /\Aa.+\d\z/})).to be true }
|
103
|
+
it { expect(ActiveModel::Validations::FormatValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :legacy_code, {:message => 'wrong format', :with => /\Aa.+\d\z/})).to be true }
|
104
|
+
it { expect(!ActiveModel::Validations::FormatValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :legacy_code, {:with => /\Aa.+\z/})).to be true }
|
105
|
+
end
|
106
|
+
|
107
|
+
context 'inclusion validator' do
|
108
|
+
it { expect(ActiveModel::Validations::InclusionValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :size, {:in => %w(small medium large)})).to be true }
|
109
|
+
it { expect(ActiveModel::Validations::InclusionValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :size, {:message => "%{value} is not a valid size"})).to be true }
|
110
|
+
end
|
111
|
+
|
112
|
+
context 'length validator' do
|
113
|
+
it { expect(ActiveModel::Validations::LengthValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :name)).to be true }
|
114
|
+
it { expect(ActiveModel::Validations::LengthValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :name, {:minimum => 2})).to be true }
|
115
|
+
it { expect(!ActiveModel::Validations::LengthValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :name, {:maximum => 2})).to be true }
|
116
|
+
it { expect(ActiveModel::Validations::LengthValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :bio, {:maximum => 500})).to be true }
|
117
|
+
# watch out how :in has been interpreted to :minimum .. :maximum
|
118
|
+
it { expect(ActiveModel::Validations::LengthValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :password, {:minimum => 6})).to be true }
|
119
|
+
it { expect(ActiveModel::Validations::LengthValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :password, {:maximum => 20})).to be true }
|
120
|
+
it { expect(!ActiveModel::Validations::LengthValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :registration_number, {:is => 2})).to be true }
|
121
|
+
it { expect(ActiveModel::Validations::LengthValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :registration_number, {:is => 6})).to be true }
|
122
|
+
it { expect(ActiveModel::Validations::LengthValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :account_number, {:wrong_length => 'wrong length, please try again', :minimum => 2})).to be true }
|
123
|
+
it { expect(ActiveModel::Validations::LengthValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :student_id, {:too_long => 'sorry, too long', :maximum => 10})).to be true }
|
124
|
+
it { expect(ActiveModel::Validations::LengthValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :mama_telephone, {:is => 8})).to be true }
|
125
|
+
end
|
126
|
+
|
127
|
+
context 'numericality' do
|
128
|
+
it { expect(ActiveModel::Validations::NumericalityValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :points)).to be true }
|
129
|
+
it { expect(!ActiveModel::Validations::NumericalityValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :points, { :only_integer => true })).to be true }
|
130
|
+
it { expect(ActiveModel::Validations::NumericalityValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :games_played, { :only_integer => true })).to be true }
|
131
|
+
|
132
|
+
it { expect(ActiveModel::Validations::NumericalityValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :num_1, { :greater_than => 1 })).to be true }
|
133
|
+
it { expect(ActiveModel::Validations::NumericalityValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :num_2, { :greater_than_or_equal_to => 2 })).to be true }
|
134
|
+
it { expect(ActiveModel::Validations::NumericalityValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :num_3, { :equal_to => 3 })).to be true }
|
135
|
+
it { expect(ActiveModel::Validations::NumericalityValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :num_4, { :less_than => 4 })).to be true }
|
136
|
+
it { expect(ActiveModel::Validations::NumericalityValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :num_5, { :less_than_or_equal_to => 5 })).to be true }
|
137
|
+
it { expect(ActiveModel::Validations::NumericalityValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :num_6, { :odd => true })).to be true }
|
138
|
+
it { expect(ActiveModel::Validations::NumericalityValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :num_7, { :even => true })).to be true }
|
139
|
+
end
|
140
|
+
|
141
|
+
context 'uniqueness' do
|
142
|
+
it { expect(ActiveRecord::Validations::UniquenessValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :identity_number)).to be true }
|
143
|
+
it { expect(ActiveRecord::Validations::UniquenessValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :identity_number_1)).to be true }
|
144
|
+
it { expect(!ActiveRecord::Validations::UniquenessValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :identity_number_1, { :scope => :num_1 })).to be true }
|
145
|
+
it { expect(ActiveRecord::Validations::UniquenessValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :identity_number_1, { :scope => :num_7 })).to be true }
|
146
|
+
it { expect(ActiveRecord::Validations::UniquenessValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :identity_number_2)).to be true }
|
147
|
+
it { expect(ActiveRecord::Validations::UniquenessValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :identity_number_2, { :case_sensitive => true})).to be true }
|
148
|
+
end
|
149
|
+
|
150
|
+
context 'conditional validation' do
|
151
|
+
it { expect(ActiveModel::Validations::PresenceValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :num_8, { :if => :paid_with_card? })).to be true }
|
152
|
+
it { expect(ActiveModel::Validations::LengthValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :num_9, { :minimum => 10, :if => :is_admin? })).to be true }
|
153
|
+
it { expect(ActiveModel::Validations::PresenceValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :num_10, { :if => :is_admin? })).to be true }
|
154
|
+
end
|
155
|
+
|
156
|
+
context 'custom validator' do
|
157
|
+
it { expect(DummyValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :my_facebook_email)).to be true }
|
158
|
+
it { expect(ActiveModel::Validations::PresenceValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :my_facebook_email)).to be true }
|
159
|
+
end
|
160
|
+
end
|
@@ -1,36 +1,29 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'validator_attachment/version'
|
2
5
|
|
3
6
|
Gem::Specification.new do |s|
|
4
7
|
s.name = "validator_attachment"
|
5
|
-
s.version =
|
8
|
+
s.version = ValidatorAttachment::VERSION
|
6
9
|
|
7
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
11
|
s.authors = ["Panayotis Matsinopoulos"]
|
9
|
-
s.date = "
|
12
|
+
s.date = "2015-07-12"
|
10
13
|
s.description = "Checks whether an ActiveModel Validator is attached to an attribute of a Model and with which options"
|
11
14
|
s.email = "panayotis@matsinopoulos.gr"
|
12
15
|
s.extra_rdoc_files = ["CHANGELOG", "README.rdoc", "lib/validator_attachment.rb"]
|
13
|
-
s.files = ["CHANGELOG", "MIT-LICENSE", "README.rdoc", "Rakefile", "lib/validator_attachment.rb", "
|
16
|
+
s.files = ["CHANGELOG", "MIT-LICENSE", "README.rdoc", "Rakefile", "lib/validator_attachment.rb", "spec/dummy_validator.rb", "spec/spec_helper.rb", "spec/validator_attachment_spec.rb", "validator_attachment.gemspec", "Manifest"]
|
14
17
|
s.homepage = "http://github.com/pmatsinopoulos/validator_attachment"
|
15
18
|
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Validator_attachment", "--main", "README.rdoc"]
|
16
19
|
s.require_paths = ["lib"]
|
17
20
|
s.rubyforge_project = "validator_attachment"
|
18
|
-
s.rubygems_version = "
|
21
|
+
s.rubygems_version = "2.2.2"
|
19
22
|
s.summary = "Is this ActiveModel Validator Used?"
|
20
|
-
s.test_files = ["
|
23
|
+
s.test_files = ["spec/validator_attachment_spec.rb", "spec/spec_helper.rb", "spec/dummy_validator.rb"]
|
21
24
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
s.add_runtime_dependency(%q<rails>, ["~> 3.0"])
|
27
|
-
s.add_development_dependency(%q<rails>, ["~> 3.0"])
|
28
|
-
else
|
29
|
-
s.add_dependency(%q<rails>, ["~> 3.0"])
|
30
|
-
s.add_dependency(%q<rails>, ["~> 3.0"])
|
31
|
-
end
|
32
|
-
else
|
33
|
-
s.add_dependency(%q<rails>, ["~> 3.0"])
|
34
|
-
s.add_dependency(%q<rails>, ["~> 3.0"])
|
35
|
-
end
|
25
|
+
s.add_runtime_dependency('rails', "~>4.0", "~>4.0")
|
26
|
+
s.add_development_dependency('bundler', '~>1.0')
|
27
|
+
s.add_development_dependency('rake', '~>10.0')
|
28
|
+
s.add_development_dependency('rspec', '~>3.0')
|
36
29
|
end
|
metadata
CHANGED
@@ -1,36 +1,60 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: validator_attachment
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
5
|
-
prerelease:
|
4
|
+
version: 2.0.0
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Panayotis Matsinopoulos
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2015-07-12 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: rails
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
16
|
requirements:
|
19
17
|
- - ~>
|
20
18
|
- !ruby/object:Gem::Version
|
21
|
-
version: '
|
19
|
+
version: '4.0'
|
22
20
|
type: :runtime
|
23
21
|
prerelease: false
|
24
22
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
23
|
requirements:
|
27
24
|
- - ~>
|
28
25
|
- !ruby/object:Gem::Version
|
29
|
-
version: '
|
26
|
+
version: '4.0'
|
30
27
|
- !ruby/object:Gem::Dependency
|
31
|
-
name:
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
32
57
|
requirement: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
58
|
requirements:
|
35
59
|
- - ~>
|
36
60
|
- !ruby/object:Gem::Version
|
@@ -38,7 +62,6 @@ dependencies:
|
|
38
62
|
type: :development
|
39
63
|
prerelease: false
|
40
64
|
version_requirements: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
65
|
requirements:
|
43
66
|
- - ~>
|
44
67
|
- !ruby/object:Gem::Version
|
@@ -55,16 +78,17 @@ extra_rdoc_files:
|
|
55
78
|
files:
|
56
79
|
- CHANGELOG
|
57
80
|
- MIT-LICENSE
|
81
|
+
- Manifest
|
58
82
|
- README.rdoc
|
59
83
|
- Rakefile
|
60
84
|
- lib/validator_attachment.rb
|
61
|
-
-
|
62
|
-
-
|
63
|
-
-
|
85
|
+
- spec/dummy_validator.rb
|
86
|
+
- spec/spec_helper.rb
|
87
|
+
- spec/validator_attachment_spec.rb
|
64
88
|
- validator_attachment.gemspec
|
65
|
-
- Manifest
|
66
89
|
homepage: http://github.com/pmatsinopoulos/validator_attachment
|
67
90
|
licenses: []
|
91
|
+
metadata: {}
|
68
92
|
post_install_message:
|
69
93
|
rdoc_options:
|
70
94
|
- --line-numbers
|
@@ -76,24 +100,23 @@ rdoc_options:
|
|
76
100
|
require_paths:
|
77
101
|
- lib
|
78
102
|
required_ruby_version: !ruby/object:Gem::Requirement
|
79
|
-
none: false
|
80
103
|
requirements:
|
81
104
|
- - ! '>='
|
82
105
|
- !ruby/object:Gem::Version
|
83
106
|
version: '0'
|
84
107
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
85
|
-
none: false
|
86
108
|
requirements:
|
87
109
|
- - ! '>='
|
88
110
|
- !ruby/object:Gem::Version
|
89
111
|
version: '1.2'
|
90
112
|
requirements: []
|
91
113
|
rubyforge_project: validator_attachment
|
92
|
-
rubygems_version:
|
114
|
+
rubygems_version: 2.2.2
|
93
115
|
signing_key:
|
94
|
-
specification_version:
|
116
|
+
specification_version: 4
|
95
117
|
summary: Is this ActiveModel Validator Used?
|
96
118
|
test_files:
|
97
|
-
-
|
98
|
-
-
|
119
|
+
- spec/validator_attachment_spec.rb
|
120
|
+
- spec/spec_helper.rb
|
121
|
+
- spec/dummy_validator.rb
|
99
122
|
has_rdoc:
|
data/test/test_helper.rb
DELETED
@@ -1,9 +0,0 @@
|
|
1
|
-
require 'rubygems'
|
2
|
-
require 'test/unit'
|
3
|
-
require 'active_support'
|
4
|
-
require 'active_model'
|
5
|
-
require 'active_record'
|
6
|
-
require 'active_support/core_ext/hash/diff'
|
7
|
-
require 'active_support/core_ext/object/with_options'
|
8
|
-
require File.expand_path('../../lib/validator_attachment', __FILE__)
|
9
|
-
require 'dummy_validator'
|
@@ -1,155 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class ValidatorAttachmentTest < ActiveSupport::TestCase
|
4
|
-
|
5
|
-
class SampleModel
|
6
|
-
include ActiveModel::Validations
|
7
|
-
include ActiveRecord::Validations
|
8
|
-
|
9
|
-
validates :acceptance_attr_1, :acceptance => true
|
10
|
-
validates :acceptance_attr_2, :acceptance => { :allow_nil => true }
|
11
|
-
validates :acceptance_attr_3, :acceptance => { :allow_nil => false }
|
12
|
-
validates :acceptance_attr_4, :acceptance => { :accept => 'sure' }
|
13
|
-
validates :acceptance_attr_5, :acceptance => { :allow_nil => true, :accept => 'sure' }
|
14
|
-
|
15
|
-
validates :email_1, :confirmation => true
|
16
|
-
validates :email_2, :confirmation => true, :presence => true
|
17
|
-
|
18
|
-
validates :exclusion_1, :exclusion => { :in => %w(ab cd ef), :message => 'not allowed' }
|
19
|
-
|
20
|
-
validates :legacy_code, :format => { :with => /^a.+\d$/, :message => 'wrong format' }
|
21
|
-
|
22
|
-
validates :size, :inclusion => { :in => %w(small medium large),
|
23
|
-
:message => "%{value} is not a valid size" }
|
24
|
-
|
25
|
-
validates :name, :length => { :minimum => 2 }
|
26
|
-
validates :bio, :length => { :maximum => 500 }
|
27
|
-
validates :password, :length => { :in => 6..20 }
|
28
|
-
validates :registration_number, :length => { :is => 6 }
|
29
|
-
validates :account_number, :length => { :minimum => 2, :maximum => 6, :wrong_length => 'wrong length, please try again' }
|
30
|
-
validates :student_id, :length => { :maximum => 10, :too_long => 'sorry, too long', :too_short => 'sorry, too short' }
|
31
|
-
validates :mama_telephone, :length => { :is => 8 }
|
32
|
-
|
33
|
-
validates :points, :numericality => true
|
34
|
-
validates :games_played, :numericality => { :only_integer => true }
|
35
|
-
validates :num_1, :numericality => { :greater_than => 1 }
|
36
|
-
validates :num_2, :numericality => { :greater_than_or_equal_to => 2 }
|
37
|
-
validates :num_3, :numericality => { :equal_to => 3 }
|
38
|
-
validates :num_4, :numericality => { :less_than => 4 }
|
39
|
-
validates :num_5, :numericality => { :less_than_or_equal_to => 5 }
|
40
|
-
validates :num_6, :numericality => { :odd => true }
|
41
|
-
validates :num_7, :numericality => { :even => true }
|
42
|
-
|
43
|
-
validates :identity_number, :uniqueness => true
|
44
|
-
validates :identity_number_1, :uniqueness => { :scope => :num_7 }
|
45
|
-
validates :identity_number_2, :uniqueness => { :case_sensitive => true}
|
46
|
-
|
47
|
-
validates :num_8, :presence => true, :if => :paid_with_card?
|
48
|
-
|
49
|
-
with_options :if => :is_admin? do |admin|
|
50
|
-
admin.validates :num_9, :length => { :minimum => 10 }
|
51
|
-
admin.validates :num_10, :presence => true
|
52
|
-
end
|
53
|
-
|
54
|
-
validates :my_facebook_email, :presence => true, :dummy => true
|
55
|
-
|
56
|
-
def persisted?
|
57
|
-
false
|
58
|
-
end
|
59
|
-
|
60
|
-
end
|
61
|
-
|
62
|
-
test "validator attachment" do
|
63
|
-
assert ActiveModel::EachValidator.respond_to?(:is_attached?)
|
64
|
-
assert ActiveModel::EachValidator.respond_to?(:attached?)
|
65
|
-
end
|
66
|
-
|
67
|
-
test "acceptance validator" do
|
68
|
-
assert ActiveModel::Validations::AcceptanceValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :acceptance_attr_1)
|
69
|
-
assert ActiveModel::Validations::AcceptanceValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :acceptance_attr_2, {:allow_nil => true})
|
70
|
-
assert ActiveModel::Validations::AcceptanceValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :acceptance_attr_3, {:allow_nil => false})
|
71
|
-
assert ActiveModel::Validations::AcceptanceValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :acceptance_attr_4, {:accept => 'sure'})
|
72
|
-
assert ActiveModel::Validations::AcceptanceValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :acceptance_attr_5, {:accept => 'sure'})
|
73
|
-
assert !ActiveModel::Validations::AcceptanceValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :acceptance_attr_5, {:accept => 'sure'}, true)
|
74
|
-
assert ActiveModel::Validations::AcceptanceValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :acceptance_attr_5, {:accept => 'sure', :allow_nil => true}, true)
|
75
|
-
end
|
76
|
-
|
77
|
-
test "confirmation validator" do
|
78
|
-
assert ActiveModel::Validations::ConfirmationValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :email_1)
|
79
|
-
assert !ActiveModel::Validations::ConfirmationValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :acceptance_attr_1)
|
80
|
-
assert ActiveModel::Validations::ConfirmationValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :email_2)
|
81
|
-
end
|
82
|
-
|
83
|
-
test "presence validator" do
|
84
|
-
assert ActiveModel::Validations::PresenceValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :email_2)
|
85
|
-
assert ActiveModel::Validations::PresenceValidator.attached?(ValidatorAttachmentTest::SampleModel, :email_2)
|
86
|
-
end
|
87
|
-
|
88
|
-
test "exclusion validator" do
|
89
|
-
assert !ActiveModel::Validations::ExclusionValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :email_2)
|
90
|
-
assert ActiveModel::Validations::ExclusionValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :exclusion_1, {:in => %w(ab cd ef)})
|
91
|
-
assert !ActiveModel::Validations::ExclusionValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :exclusion_1, {:in => %w(ab cd ef gh)})
|
92
|
-
end
|
93
|
-
|
94
|
-
test "format validator" do
|
95
|
-
assert ActiveModel::Validations::FormatValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :legacy_code)
|
96
|
-
assert ActiveModel::Validations::FormatValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :legacy_code, {:message => 'wrong format'})
|
97
|
-
assert ActiveModel::Validations::FormatValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :legacy_code, {:with => /^a.+\d$/})
|
98
|
-
assert ActiveModel::Validations::FormatValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :legacy_code, {:message => 'wrong format', :with => /^a.+\d$/})
|
99
|
-
assert !ActiveModel::Validations::FormatValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :legacy_code, {:with => /^a.+$/})
|
100
|
-
end
|
101
|
-
|
102
|
-
test "inclusion validator" do
|
103
|
-
assert ActiveModel::Validations::InclusionValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :size, {:in => %w(small medium large)})
|
104
|
-
assert ActiveModel::Validations::InclusionValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :size, {:message => "%{value} is not a valid size"})
|
105
|
-
end
|
106
|
-
|
107
|
-
test "length validator" do
|
108
|
-
assert ActiveModel::Validations::LengthValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :name)
|
109
|
-
assert ActiveModel::Validations::LengthValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :name, {:minimum => 2})
|
110
|
-
assert !ActiveModel::Validations::LengthValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :name, {:maximum => 2})
|
111
|
-
assert ActiveModel::Validations::LengthValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :bio, {:maximum => 500})
|
112
|
-
# watch out how :in has been interpreted to :minimum .. :maximum
|
113
|
-
assert ActiveModel::Validations::LengthValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :password, {:minimum => 6})
|
114
|
-
assert ActiveModel::Validations::LengthValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :password, {:maximum => 20})
|
115
|
-
assert !ActiveModel::Validations::LengthValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :registration_number, {:is => 2})
|
116
|
-
assert ActiveModel::Validations::LengthValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :registration_number, {:is => 6})
|
117
|
-
assert ActiveModel::Validations::LengthValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :account_number, {:wrong_length => 'wrong length, please try again', :minimum => 2})
|
118
|
-
assert ActiveModel::Validations::LengthValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :student_id, {:too_long => 'sorry, too long', :maximum => 10})
|
119
|
-
assert ActiveModel::Validations::LengthValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :mama_telephone, {:is => 8})
|
120
|
-
end
|
121
|
-
|
122
|
-
test "numericality" do
|
123
|
-
assert ActiveModel::Validations::NumericalityValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :points)
|
124
|
-
assert !ActiveModel::Validations::NumericalityValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :points, { :only_integer => true })
|
125
|
-
assert ActiveModel::Validations::NumericalityValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :games_played, { :only_integer => true })
|
126
|
-
|
127
|
-
assert ActiveModel::Validations::NumericalityValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :num_1, { :greater_than => 1 })
|
128
|
-
assert ActiveModel::Validations::NumericalityValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :num_2, { :greater_than_or_equal_to => 2 })
|
129
|
-
assert ActiveModel::Validations::NumericalityValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :num_3, { :equal_to => 3 })
|
130
|
-
assert ActiveModel::Validations::NumericalityValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :num_4, { :less_than => 4 })
|
131
|
-
assert ActiveModel::Validations::NumericalityValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :num_5, { :less_than_or_equal_to => 5 })
|
132
|
-
assert ActiveModel::Validations::NumericalityValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :num_6, { :odd => true })
|
133
|
-
assert ActiveModel::Validations::NumericalityValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :num_7, { :even => true })
|
134
|
-
end
|
135
|
-
|
136
|
-
test "uniqueness" do
|
137
|
-
assert ActiveRecord::Validations::UniquenessValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :identity_number)
|
138
|
-
assert ActiveRecord::Validations::UniquenessValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :identity_number_1)
|
139
|
-
assert !ActiveRecord::Validations::UniquenessValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :identity_number_1, { :scope => :num_1 })
|
140
|
-
assert ActiveRecord::Validations::UniquenessValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :identity_number_1, { :scope => :num_7 })
|
141
|
-
assert ActiveRecord::Validations::UniquenessValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :identity_number_2)
|
142
|
-
assert ActiveRecord::Validations::UniquenessValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :identity_number_2, { :case_sensitive => true})
|
143
|
-
end
|
144
|
-
|
145
|
-
test "conditional validation" do
|
146
|
-
assert ActiveModel::Validations::PresenceValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :num_8, { :if => :paid_with_card? })
|
147
|
-
assert ActiveModel::Validations::LengthValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :num_9, { :minimum => 10, :if => :is_admin? })
|
148
|
-
assert ActiveModel::Validations::PresenceValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :num_10, { :if => :is_admin? })
|
149
|
-
end
|
150
|
-
|
151
|
-
test "custom validator" do
|
152
|
-
assert DummyValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :my_facebook_email)
|
153
|
-
assert ActiveModel::Validations::PresenceValidator.is_attached?(ValidatorAttachmentTest::SampleModel, :my_facebook_email)
|
154
|
-
end
|
155
|
-
end
|