Skip to content

Commit 59cae8f

Browse files
committed
Handle :on as same as :if and :unless
1 parent 9b64047 commit 59cae8f

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

lib/html5_validators/active_model/helper_methods.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,31 @@ module Validations
33
module HelperMethods
44
def attribute_required?(attribute)
55
self.validators.grep(PresenceValidator).any? do |v|
6-
v.attributes.include?(attribute.to_sym) && (v.options.keys & [:if, :unless]).empty?
6+
v.attributes.include?(attribute.to_sym) && (v.options.keys & [:if, :unless, :on]).empty?
77
end
88
end
99

1010
def attribute_maxlength(attribute)
1111
self.validators.grep(LengthValidator).select {|v|
12-
v.attributes.include?(attribute.to_sym) && (v.options.keys & [:maximum, :is]).any? && (v.options.keys & [:if, :unless, :allow_nil, :allow_blank, :tokenizer]).empty?
12+
v.attributes.include?(attribute.to_sym) && (v.options.keys & [:maximum, :is]).any? && (v.options.keys & [:if, :unless, :on, :allow_nil, :allow_blank, :tokenizer]).empty?
1313
}.map {|v| v.options.slice(:maximum, :is)}.map(&:values).flatten.max
1414
end
1515

1616
def attribute_minlength(attribute)
1717
self.validators.grep(LengthValidator).select {|v|
18-
v.attributes.include?(attribute.to_sym) && (v.options.keys & [:minimum, :is]).any? && (v.options.keys & [:if, :unless, :allow_nil, :allow_blank, :tokenizer]).empty?
18+
v.attributes.include?(attribute.to_sym) && (v.options.keys & [:minimum, :is]).any? && (v.options.keys & [:if, :unless, :on, :allow_nil, :allow_blank, :tokenizer]).empty?
1919
}.map {|v| v.options.slice(:minimum, :is)}.map(&:values).flatten.min
2020
end
2121

2222
def attribute_max(attribute)
2323
self.validators.grep(NumericalityValidator).select {|v|
24-
v.attributes.include?(attribute.to_sym) && (v.options.keys & [:less_than, :less_than_or_equal_to]).any? && (v.options.keys & [:if, :unless, :allow_nil, :allow_blank]).empty?
24+
v.attributes.include?(attribute.to_sym) && (v.options.keys & [:less_than, :less_than_or_equal_to]).any? && (v.options.keys & [:if, :unless, :on, :allow_nil, :allow_blank]).empty?
2525
}.map {|v| v.options.slice(:less_than, :less_than_or_equal_to)}.map(&:values).flatten.max
2626
end
2727

2828
def attribute_min(attribute)
2929
self.validators.grep(NumericalityValidator).select {|v|
30-
v.attributes.include?(attribute.to_sym) && (v.options.keys & [:greater_than, :greater_than_or_equal_to]).any? && (v.options.keys & [:if, :unless, :allow_nil, :allow_blank]).empty?
30+
v.attributes.include?(attribute.to_sym) && (v.options.keys & [:greater_than, :greater_than_or_equal_to]).any? && (v.options.keys & [:if, :unless, :on, :allow_nil, :allow_blank]).empty?
3131
}.map {|v| v.options.slice(:greater_than, :greater_than_or_equal_to)}.map(&:values).flatten.min
3232
end
3333
end

spec/features/validation_spec.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,21 @@
100100
find('textarea#person_bio')[:minlength].should == '10'
101101
end
102102
end
103+
104+
context 'with validation context' do
105+
background do
106+
Person.validates_presence_of :name, :bio, on: :create
107+
end
108+
after do
109+
Person._validators.clear
110+
end
111+
scenario 'new form' do
112+
visit '/people/new'
113+
114+
find('input#person_name')[:required].should be_nil
115+
find('textarea#person_bio')[:required].should be_nil
116+
end
117+
end
103118
end
104119

105120
feature 'item#new' do
@@ -202,4 +217,19 @@
202217
find('textarea#item_description')[:minlength].should == '10'
203218
end
204219
end
220+
221+
context 'with validation context' do
222+
background do
223+
Item.validates_presence_of :name, :description, on: :create
224+
end
225+
after do
226+
Item._validators.clear
227+
end
228+
scenario 'new form' do
229+
visit '/items/new'
230+
231+
find('input#item_name')[:required].should be_nil
232+
find('textarea#item_description')[:required].should be_nil
233+
end
234+
end
205235
end

0 commit comments

Comments
 (0)