From 2adb4922710cc04a3af1ac273762145ffa65f03f Mon Sep 17 00:00:00 2001 From: ta1kt0me Date: Fri, 4 Dec 2015 01:13:53 +0900 Subject: [PATCH 1/2] Handle :on as same as :if and :unless --- .../active_model/helper_methods.rb | 10 +++---- spec/features/validation_spec.rb | 30 +++++++++++++++++++ 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/lib/html5_validators/active_model/helper_methods.rb b/lib/html5_validators/active_model/helper_methods.rb index 3891772..381fa00 100644 --- a/lib/html5_validators/active_model/helper_methods.rb +++ b/lib/html5_validators/active_model/helper_methods.rb @@ -3,31 +3,31 @@ module Validations module HelperMethods def attribute_required?(attribute) self.validators.grep(PresenceValidator).any? do |v| - v.attributes.include?(attribute.to_sym) && (v.options.keys & [:if, :unless]).empty? + v.attributes.include?(attribute.to_sym) && (v.options.keys & [:if, :unless, :on]).empty? end end def attribute_maxlength(attribute) self.validators.grep(LengthValidator).select {|v| - v.attributes.include?(attribute.to_sym) && (v.options.keys & [:maximum, :is]).any? && (v.options.keys & [:if, :unless, :tokenizer]).empty? + v.attributes.include?(attribute.to_sym) && (v.options.keys & [:maximum, :is]).any? && (v.options.keys & [:if, :unless, :on, :tokenizer]).empty? }.map {|v| v.options.slice(:maximum, :is)}.map(&:values).flatten.max end def attribute_minlength(attribute) self.validators.grep(LengthValidator).select {|v| - v.attributes.include?(attribute.to_sym) && (v.options.keys & [:minimum, :is]).any? && (v.options.keys & [:if, :unless, :allow_nil, :allow_blank, :tokenizer]).empty? + v.attributes.include?(attribute.to_sym) && (v.options.keys & [:minimum, :is]).any? && (v.options.keys & [:if, :unless, :on, :allow_nil, :allow_blank, :tokenizer]).empty? }.map {|v| v.options.slice(:minimum, :is)}.map(&:values).flatten.min end def attribute_max(attribute) self.validators.grep(NumericalityValidator).select {|v| - 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? + 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? }.map {|v| v.options.slice(:less_than, :less_than_or_equal_to)}.map(&:values).flatten.max end def attribute_min(attribute) self.validators.grep(NumericalityValidator).select {|v| - 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? + 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? }.map {|v| v.options.slice(:greater_than, :greater_than_or_equal_to)}.map(&:values).flatten.min end end diff --git a/spec/features/validation_spec.rb b/spec/features/validation_spec.rb index 9bed690..7590c53 100644 --- a/spec/features/validation_spec.rb +++ b/spec/features/validation_spec.rb @@ -100,6 +100,21 @@ find('textarea#person_bio')[:minlength].should == '10' end end + + context 'with validation context' do + background do + Person.validates_presence_of :name, :bio, on: :create + end + after do + Person._validators.clear + end + scenario 'new form' do + visit '/people/new' + + find('input#person_name')[:required].should be_nil + find('textarea#person_bio')[:required].should be_nil + end + end end feature 'item#new' do @@ -202,4 +217,19 @@ find('textarea#item_description')[:minlength].should == '10' end end + + context 'with validation context' do + background do + Item.validates_presence_of :name, :description, on: :create + end + after do + Item._validators.clear + end + scenario 'new form' do + visit '/items/new' + + find('input#item_name')[:required].should be_nil + find('textarea#item_description')[:required].should be_nil + end + end end From 7e5bbacc195bcb94a01e8cb0604cfec1caaad1ab Mon Sep 17 00:00:00 2001 From: ta1kt0me Date: Sat, 5 Dec 2015 18:59:40 +0900 Subject: [PATCH 2/2] Compatibility --- spec/features/validation_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/features/validation_spec.rb b/spec/features/validation_spec.rb index 7590c53..67b6491 100644 --- a/spec/features/validation_spec.rb +++ b/spec/features/validation_spec.rb @@ -103,7 +103,7 @@ context 'with validation context' do background do - Person.validates_presence_of :name, :bio, on: :create + Person.validates_presence_of :name, :bio, {:on => :create} end after do Person._validators.clear @@ -220,7 +220,7 @@ context 'with validation context' do background do - Item.validates_presence_of :name, :description, on: :create + Item.validates_presence_of :name, :description, {:on => :create} end after do Item._validators.clear