From f3c119e1916a2bfa2f54859c838e5010b6a3b0bf Mon Sep 17 00:00:00 2001 From: Adam Meehan Date: Fri, 11 Dec 2009 15:20:34 +1100 Subject: [PATCH] change equal_to to is_at to fix overlap with default rails message --- README.rdoc | 3 +- lib/validates_timeliness/locale/en.yml | 2 +- .../rails/matchers/validate_timeliness.rb | 38 +++---- lib/validates_timeliness/validator.rb | 2 +- spec/active_record/attribute_methods_spec.rb | 28 ++--- .../matchers/validate_timeliness_spec.rb | 104 +++++++++--------- spec/validator_spec.rb | 40 +++---- 7 files changed, 109 insertions(+), 108 deletions(-) diff --git a/README.rdoc b/README.rdoc index ff2d5e2..76b5d4f 100644 --- a/README.rdoc +++ b/README.rdoc @@ -317,6 +317,7 @@ For Rails 2.0/2.1: :invalid_date => "is not a valid date", :invalid_time => "is not a valid time", :invalid_datetime => "is not a valid datetime", + :is_at => "must be at %s", :before => "must be before %s", :on_or_before => "must be on or before %s", :after => "must be after %s", @@ -335,7 +336,7 @@ Rails 2.2+ using the I18n system to define new defaults: invalid_date: "is not a valid date" invalid_time: "is not a valid time" invalid_datetime: "is not a valid datetime" - equal_to: "must be equal to {{restriction}}" + is_at: "must be at {{restriction}}" before: "must be before {{restriction}}" on_or_before: "must be on or before {{restriction}}" after: "must be after {{restriction}}" diff --git a/lib/validates_timeliness/locale/en.yml b/lib/validates_timeliness/locale/en.yml index 3b249a6..01d580f 100644 --- a/lib/validates_timeliness/locale/en.yml +++ b/lib/validates_timeliness/locale/en.yml @@ -5,7 +5,7 @@ en: invalid_date: "is not a valid date" invalid_time: "is not a valid time" invalid_datetime: "is not a valid datetime" - equal_to: "must be equal to {{restriction}}" + is_at: "must be at {{restriction}}" before: "must be before {{restriction}}" on_or_before: "must be on or before {{restriction}}" after: "must be after {{restriction}}" diff --git a/lib/validates_timeliness/spec/rails/matchers/validate_timeliness.rb b/lib/validates_timeliness/spec/rails/matchers/validate_timeliness.rb index 5e5ac8b..5d22180 100644 --- a/lib/validates_timeliness/spec/rails/matchers/validate_timeliness.rb +++ b/lib/validates_timeliness/spec/rails/matchers/validate_timeliness.rb @@ -2,7 +2,7 @@ module Spec module Rails module Matchers class ValidateTimeliness - + VALIDITY_TEST_VALUES = { :date => {:pass => '2000-01-01', :fail => '2000-01-32'}, :time => {:pass => '12:00', :fail => '25:00'}, @@ -10,7 +10,7 @@ module Spec } OPTION_TEST_SETTINGS = { - :equal_to => { :method => :+, :modify_on => :invalid }, + :is_at => { :method => :+, :modify_on => :invalid }, :before => { :method => :-, :modify_on => :valid }, :after => { :method => :+, :modify_on => :valid }, :on_or_before => { :method => :+, :modify_on => :invalid }, @@ -25,10 +25,10 @@ module Spec def matches?(record) @record = record @type = @options[:type] - + valid = test_validity - valid = test_option(:equal_to) if valid && @options[:equal_to] + valid = test_option(:is_at) if valid && @options[:is_at] valid = test_option(:before) if valid && @options[:before] valid = test_option(:after) if valid && @options[:after] valid = test_option(:on_or_before) if valid && @options[:on_or_before] @@ -37,21 +37,21 @@ module Spec return valid end - + def failure_message "expected model to validate #{@type} attribute #{@expected.inspect} with #{@last_failure}" end - + def negative_failure_message "expected not to validate #{@type} attribute #{@expected.inspect}" end - + def description "have validated #{@type} attribute #{@expected.inspect}" end - + private - + def test_validity invalid_value = VALIDITY_TEST_VALUES[@type][:fail] valid_value = parse_and_cast(VALIDITY_TEST_VALUES[@type][:pass]) @@ -62,7 +62,7 @@ module Spec def test_option(option) settings = OPTION_TEST_SETTINGS[option] boundary = parse_and_cast(@options[option]) - + method = settings[:method] valid_value, invalid_value = if settings[:modify_on] == :valid @@ -70,27 +70,27 @@ module Spec else [ boundary, boundary.send(method, 1) ] end - - error_matching(invalid_value, option) && + + error_matching(invalid_value, option) && no_error_matching(valid_value, option) end def test_before before = parse_and_cast(@options[:before]) - error_matching(before - 1, :before) && + error_matching(before - 1, :before) && no_error_matching(before, :before) end def test_between - between = parse_and_cast(@options[:between]) - - error_matching(between.first - 1, :between) && - error_matching(between.last + 1, :between) && + between = parse_and_cast(@options[:between]) + + error_matching(between.first - 1, :between) && + error_matching(between.last + 1, :between) && no_error_matching(between.first, :between) && no_error_matching(between.last, :between) end - + def parse_and_cast(value) value = @validator.class.send(:evaluate_option_value, value, @type, @record) @validator.class.send(:type_cast_value, value, @type) @@ -105,7 +105,7 @@ module Spec @last_failure = "error matching '#{match}' when value is #{format_value(value)}" unless pass pass end - + def no_error_matching(value, option) pass = !error_matching(value, option) unless pass diff --git a/lib/validates_timeliness/validator.rb b/lib/validates_timeliness/validator.rb index e17ee6b..791e3fc 100644 --- a/lib/validates_timeliness/validator.rb +++ b/lib/validates_timeliness/validator.rb @@ -6,7 +6,7 @@ module ValidatesTimeliness self.ignore_restriction_errors = false RESTRICTION_METHODS = { - :equal_to => :==, + :is_at => :==, :before => :<, :after => :>, :on_or_before => :<=, diff --git a/spec/active_record/attribute_methods_spec.rb b/spec/active_record/attribute_methods_spec.rb index 63e054b..0d9bfc0 100644 --- a/spec/active_record/attribute_methods_spec.rb +++ b/spec/active_record/attribute_methods_spec.rb @@ -40,7 +40,7 @@ describe ValidatesTimeliness::ActiveRecord::AttributeMethods do @person.birth_date_and_time = time_string @person.birth_date_and_time_before_type_cast.should == time_string end - + it "should return Time object for attribute_before_type_cast when written as Time" do @person.birth_date_and_time = Time.mktime(2000, 1, 1, 2, 3, 4) @person.birth_date_and_time_before_type_cast.should be_kind_of(Time) @@ -59,24 +59,24 @@ describe ValidatesTimeliness::ActiveRecord::AttributeMethods do it "should return Time object for datetime attribute read method when assigned string" do @person.birth_date_and_time = "2000-01-01 02:03:04" @person.birth_date_and_time.should be_kind_of(Time) - end - + end + it "should return Date object for date attribute read method when assigned Date object" do @person.birth_date = Date.today @person.birth_date.should be_kind_of(Date) - end - + end + it "should return Date object for date attribute read method when assigned string" do @person.birth_date = '2000-01-01' @person.birth_date.should be_kind_of(Date) - end - + end + it "should return nil when time is invalid" do @person.birth_date_and_time = "2000-01-32 02:03:04" @person.birth_date_and_time.should be_nil end - it "should not save invalid date value to database" do + it "should not save invalid date value to database" do time_string = "2000-01-32 02:03:04" @person = Person.new @person.birth_date_and_time = time_string @@ -84,7 +84,7 @@ describe ValidatesTimeliness::ActiveRecord::AttributeMethods do @person.reload @person.birth_date_and_time_before_type_cast.should be_nil end - + if RAILS_VER < '2.1' it "should return time object from database in default timezone" do @@ -106,7 +106,7 @@ describe ValidatesTimeliness::ActiveRecord::AttributeMethods do @person.birth_date_and_time.strftime('%Y-%m-%d %H:%M:%S %Z %z').should == time_string + ' EST +1000' end - it "should return time object from database in correct timezone" do + it "should return time object from database in correct timezone" do Time.zone = 'Melbourne' time_string = "2000-06-01 09:00:00" @person = Person.new @@ -115,19 +115,19 @@ describe ValidatesTimeliness::ActiveRecord::AttributeMethods do @person.reload @person.birth_date_and_time.strftime('%Y-%m-%d %H:%M:%S %Z %z').should == time_string + ' EST +1000' end - + end - + it "should return correct date value after new value assigned" do today = Date.today - tomorrow = Date.today + 1.day + tomorrow = Date.today + 1.day @person = Person.new @person.birth_date = today @person.birth_date.should == today @person.birth_date = tomorrow @person.birth_date.should == tomorrow end - + it "should update date attribute on existing object" do today = Date.today tomorrow = Date.today + 1.day diff --git a/spec/spec/rails/matchers/validate_timeliness_spec.rb b/spec/spec/rails/matchers/validate_timeliness_spec.rb index bd07f23..137604b 100644 --- a/spec/spec/rails/matchers/validate_timeliness_spec.rb +++ b/spec/spec/rails/matchers/validate_timeliness_spec.rb @@ -5,24 +5,24 @@ class NoValidation < Person end class WithValidation < Person - validates_date :birth_date, - :equal_to => '2000-01-01', + validates_date :birth_date, + :is_at => '2000-01-01', :before => '2000-01-10', :after => '2000-01-01', :on_or_before => '2000-01-09', :on_or_after => '2000-01-02', :between => ['2000-01-01', '2000-01-03'] - validates_time :birth_time, - :equal_to => '09:00', + validates_time :birth_time, + :is_at => '09:00', :before => '23:00', :after => '09:00', :on_or_before => '22:00', :on_or_after => '10:00', :between => ['09:00', '17:00'] - validates_datetime :birth_date_and_time, - :equal_to => '2000-01-01 09:00', + validates_datetime :birth_date_and_time, + :is_at => '2000-01-01 09:00', :before => '2000-01-10 23:00', :after => '2000-01-01 09:00', :on_or_before => '2000-01-09 23:00', @@ -46,44 +46,44 @@ end describe "ValidateTimeliness matcher" do attr_accessor :no_validation, :with_validation - + @@attribute_for_type = { :date => :birth_date, :time => :birth_time, :datetime => :birth_date_and_time } - + before do @no_validation = NoValidation.new @with_validation = WithValidation.new end [:date, :time, :datetime].each do |type| - + it "should report that #{type} is validated" do with_validation.should self.send("validate_#{type}", attribute_for_type(type)) end - + it "should report that #{type} is not validated" do no_validation.should_not self.send("validate_#{type}", attribute_for_type(type)) - end + end end - - describe "with equal_to option" do + + describe "with is_at option" do test_values = { :date => ['2000-01-01', '2000-01-02'], :time => ['09:00', '09:01'], :datetime => ['2000-01-01 09:00', '2000-01-01 09:01'] } - + [:date, :time, :datetime].each do |type| it "should report that #{type} is validated" do - with_validation.should self.send("validate_#{type}", attribute_for_type(type), :equal_to => test_values[type][0]) + with_validation.should self.send("validate_#{type}", attribute_for_type(type), :is_at => test_values[type][0]) end - + it "should report that #{type} is not validated when option value is incorrect" do - with_validation.should_not self.send("validate_#{type}", attribute_for_type(type), :equal_to => test_values[type][1]) + with_validation.should_not self.send("validate_#{type}", attribute_for_type(type), :is_at => test_values[type][1]) end - + it "should report that #{type} is not validated with option" do - no_validation.should_not self.send("validate_#{type}", attribute_for_type(type), :equal_to => test_values[type][0]) + no_validation.should_not self.send("validate_#{type}", attribute_for_type(type), :is_at => test_values[type][0]) end end end @@ -92,111 +92,111 @@ describe "ValidateTimeliness matcher" do test_values = { :date => ['2000-01-10', '2000-01-11'], :time => ['23:00', '22:59'], - :datetime => ['2000-01-10 23:00', '2000-01-10 22:59'] + :datetime => ['2000-01-10 23:00', '2000-01-10 22:59'] } - + [:date, :time, :datetime].each do |type| it "should report that #{type} is validated" do with_validation.should self.send("validate_#{type}", attribute_for_type(type), :before => test_values[type][0]) end - + it "should report that #{type} is not validated when option value is incorrect" do with_validation.should_not self.send("validate_#{type}", attribute_for_type(type), :before => test_values[type][1]) end - + it "should report that #{type} is not validated with option" do no_validation.should_not self.send("validate_#{type}", attribute_for_type(type), :before => test_values[type][0]) end end end - + describe "with after option" do test_values = { :date => ['2000-01-01', '2000-01-02'], :time => ['09:00', '09:01'], - :datetime => ['2000-01-01 09:00', '2000-01-01 09:01'] + :datetime => ['2000-01-01 09:00', '2000-01-01 09:01'] } - + [:date, :time, :datetime].each do |type| it "should report that #{type} is validated" do with_validation.should self.send("validate_#{type}", attribute_for_type(type), :after => test_values[type][0]) end - + it "should report that #{type} is not validated when option value is incorrect" do with_validation.should_not self.send("validate_#{type}", attribute_for_type(type), :after => test_values[type][1]) end - + it "should report that #{type} is not validated with option" do no_validation.should_not self.send("validate_#{type}", attribute_for_type(type), :after => test_values[type][0]) end end end - + describe "with on_or_before option" do test_values = { :date => ['2000-01-09', '2000-01-08'], :time => ['22:00', '21:59'], - :datetime => ['2000-01-09 23:00', '2000-01-09 22:59'] + :datetime => ['2000-01-09 23:00', '2000-01-09 22:59'] } - + [:date, :time, :datetime].each do |type| it "should report that #{type} is validated" do with_validation.should self.send("validate_#{type}", attribute_for_type(type), :on_or_before => test_values[type][0]) end - + it "should report that #{type} is not validated when option value is incorrect" do with_validation.should_not self.send("validate_#{type}", attribute_for_type(type), :on_or_before => test_values[type][1]) end - + it "should report that #{type} is not validated with option" do no_validation.should_not self.send("validate_#{type}", attribute_for_type(type), :on_or_before => test_values[type][0]) end end end - + describe "with on_or_after option" do test_values = { :date => ['2000-01-02', '2000-01-03'], :time => ['10:00', '10:01'], - :datetime => ['2000-01-02 09:00', '2000-01-02 09:01'] + :datetime => ['2000-01-02 09:00', '2000-01-02 09:01'] } - + [:date, :time, :datetime].each do |type| it "should report that #{type} is validated" do with_validation.should self.send("validate_#{type}", attribute_for_type(type), :on_or_after => test_values[type][0]) end - + it "should report that #{type} is not validated when option value is incorrect" do with_validation.should_not self.send("validate_#{type}", attribute_for_type(type), :on_or_after => test_values[type][1]) end - + it "should report that #{type} is not validated with option" do no_validation.should_not self.send("validate_#{type}", attribute_for_type(type), :on_or_after => test_values[type][0]) end end end - + describe "between option" do test_values = { :date => [ ['2000-01-01', '2000-01-03'], ['2000-01-01', '2000-01-04'] ], - :time => [ ['09:00', '17:00'], ['09:00', '17:01'] ], - :datetime => [ ['2000-01-01 09:00', '2000-01-01 17:00'], ['2000-01-01 09:00', '2000-01-01 17:01'] ] + :time => [ ['09:00', '17:00'], ['09:00', '17:01'] ], + :datetime => [ ['2000-01-01 09:00', '2000-01-01 17:00'], ['2000-01-01 09:00', '2000-01-01 17:01'] ] } - + [:date, :time, :datetime].each do |type| it "should report that #{type} is validated" do with_validation.should self.send("validate_#{type}", attribute_for_type(type), :between => test_values[type][0]) end - + it "should report that #{type} is not validated when option value is incorrect" do with_validation.should_not self.send("validate_#{type}", attribute_for_type(type), :between => test_values[type][1]) end - + it "should report that #{type} is not validated with option" do no_validation.should_not self.send("validate_#{type}", attribute_for_type(type), :between => test_values[type][0]) end @@ -208,35 +208,35 @@ describe "ValidateTimeliness matcher" do before do @person = CustomMessages.new end - + it "should match error message for invalid" do @person.should validate_date(:birth_date, :invalid_date_message => 'is not really a date') end - + it "should match error message for before option" do - @person.should validate_date(:birth_date, :before => '2000-01-10', + @person.should validate_date(:birth_date, :before => '2000-01-10', :invalid_date_message => 'is not really a date', :before_message => 'is too late') end - + it "should match error message for after option" do - @person.should validate_date(:birth_date, :after => '2000-01-01', + @person.should validate_date(:birth_date, :after => '2000-01-01', :invalid_date_message => 'is not really a date', :after_message => 'is too early') - end + end it "should match error message for on_or_before option" do @person.should validate_date(:birth_date, :on_or_before => '2000-01-09', :invalid_date_message => 'is not really a date', :on_or_before_message => 'is just too late') end - + it "should match error message for on_or_after option" do @person.should validate_date(:birth_date, :on_or_after => '2000-01-02', :invalid_date_message => 'is not really a date', :on_or_after_message => 'is just too early') end - + end def attribute_for_type(type) diff --git a/spec/validator_spec.rb b/spec/validator_spec.rb index a0a9413..1952a3c 100644 --- a/spec/validator_spec.rb +++ b/spec/validator_spec.rb @@ -346,53 +346,53 @@ describe ValidatesTimeliness::Validator do end end - describe "instance with :equal_to restriction" do + describe "instance with :is_at restriction" do describe "for datetime type" do before do - configure_validator(:equal_to => Time.now) + configure_validator(:is_at => Time.now) end - it "should have error when value not equal to :equal_to restriction" do + it "should have error when value not equal to :is_at restriction" do validate_with(:birth_date_and_time, Time.now + 1) - should_have_error(:birth_date_and_time, :equal_to) + should_have_error(:birth_date_and_time, :is_at) end - it "should be valid when value is equal to :equal_to restriction" do + it "should be valid when value is equal to :is_at restriction" do validate_with(:birth_date_and_time, Time.now) - should_have_no_error(:birth_date_and_time, :equal_to) + should_have_no_error(:birth_date_and_time, :is_at) end end describe "for date type" do before do - configure_validator(:type => :date, :equal_to => Date.today) + configure_validator(:type => :date, :is_at => Date.today) end - it "should have error when value is not equal to :equal_to restriction" do + it "should have error when value is not equal to :is_at restriction" do validate_with(:birth_date, Date.today + 1) - should_have_error(:birth_date, :equal_to) + should_have_error(:birth_date, :is_at) end - it "should be valid when value is equal to :equal_to restriction" do + it "should be valid when value is equal to :is_at restriction" do validate_with(:birth_date, Date.today) - should_have_no_error(:birth_date, :equal_to) + should_have_no_error(:birth_date, :is_at) end end describe "for time type" do before do - configure_validator(:type => :time, :equal_to => "09:00:00") + configure_validator(:type => :time, :is_at => "09:00:00") end - it "should have error when value is not equal to :equal_to restriction" do + it "should have error when value is not equal to :is_at restriction" do validate_with(:birth_time, "09:00:01") - should_have_error(:birth_time, :equal_to) + should_have_error(:birth_time, :is_at) end - it "should be valid when value is equal to :equal_to restriction" do + it "should be valid when value is equal to :is_at restriction" do validate_with(:birth_time, "09:00:00") - should_have_no_error(:birth_time, :equal_to) + should_have_no_error(:birth_time, :is_at) end end end @@ -400,9 +400,9 @@ describe ValidatesTimeliness::Validator do describe "instance with :ignore_usec option" do it "should ignore usec on time values when evaluated" do - configure_validator(:equal_to => Time.utc(2000, 1, 1, 0, 0, 0, 0), :ignore_usec => true) + configure_validator(:is_at => Time.utc(2000, 1, 1, 0, 0, 0, 0), :ignore_usec => true) validate_with(:birth_date_and_time, Time.utc(2000, 1, 1, 0, 0, 0, 500)) - should_have_no_error(:birth_date_and_time, :equal_to) + should_have_no_error(:birth_date_and_time, :is_at) end end @@ -422,9 +422,9 @@ describe ValidatesTimeliness::Validator do end it "should should ignore usec value on combined value if :ignore_usec option is true" do - configure_validator(:type => :date, :with_time => Time.mktime(2000,1,1,12,30,0,500), :equal_to => Time.mktime(2000,1,1,12,30), :ignore_usec => true) + configure_validator(:type => :date, :with_time => Time.mktime(2000,1,1,12,30,0,500), :is_at => Time.mktime(2000,1,1,12,30), :ignore_usec => true) validate_with(:birth_date, "2000-01-01") - should_have_no_error(:birth_date, :equal_to) + should_have_no_error(:birth_date, :is_at) end end