mirror of
https://github.com/ditkrg/validates_timeliness.git
synced 2026-01-25 15:22:58 +00:00
added :with_date and :with_time options
refactored restriction_value into evaluate_option_value class method for more general usage refactored type_cast_value into class method
This commit is contained in:
@@ -18,9 +18,8 @@ describe ValidatesTimeliness::Validator do
|
||||
|
||||
describe "option keys validation" do
|
||||
before do
|
||||
@valid_options = ValidatesTimeliness::Validator::VALID_OPTIONS.inject({}) {|hash, opt| hash[opt] = nil; hash }
|
||||
@valid_options.delete(:invalid_date_message)
|
||||
@valid_options.delete(:invalid_time_message)
|
||||
keys = ValidatesTimeliness::Validator::VALID_OPTIONS - [:invalid_date_message, :invalid_time_message, :with_date, :with_time]
|
||||
@valid_options = keys.inject({}) {|hash, opt| hash[opt] = nil; hash }
|
||||
end
|
||||
|
||||
it "should raise error if invalid option key passed" do
|
||||
@@ -33,55 +32,55 @@ describe ValidatesTimeliness::Validator do
|
||||
end
|
||||
end
|
||||
|
||||
describe "restriction_value" do
|
||||
describe "evaluate_option_value" do
|
||||
it "should return Time object when restriction is Time object" do
|
||||
restriction_value(Time.now, :datetime).should be_kind_of(Time)
|
||||
evaluate_option_value(Time.now, :datetime).should be_kind_of(Time)
|
||||
end
|
||||
|
||||
it "should return Time object when restriction is string" do
|
||||
restriction_value("2007-01-01 12:00", :datetime).should be_kind_of(Time)
|
||||
evaluate_option_value("2007-01-01 12:00", :datetime).should be_kind_of(Time)
|
||||
end
|
||||
|
||||
it "should return Time object when restriction is method and method returns Time object" do
|
||||
person.stub!(:datetime_attr).and_return(Time.now)
|
||||
restriction_value(:datetime_attr, :datetime).should be_kind_of(Time)
|
||||
evaluate_option_value(:datetime_attr, :datetime).should be_kind_of(Time)
|
||||
end
|
||||
|
||||
it "should return Time object when restriction is method and method returns string" do
|
||||
person.stub!(:datetime_attr).and_return("2007-01-01 12:00")
|
||||
restriction_value(:datetime_attr, :datetime).should be_kind_of(Time)
|
||||
evaluate_option_value(:datetime_attr, :datetime).should be_kind_of(Time)
|
||||
end
|
||||
|
||||
it "should return Time object when restriction is proc which returns Time object" do
|
||||
restriction_value(lambda { Time.now }, :datetime).should be_kind_of(Time)
|
||||
evaluate_option_value(lambda { Time.now }, :datetime).should be_kind_of(Time)
|
||||
end
|
||||
|
||||
it "should return Time object when restriction is proc which returns string" do
|
||||
restriction_value(lambda {"2007-01-01 12:00"}, :datetime).should be_kind_of(Time)
|
||||
evaluate_option_value(lambda {"2007-01-01 12:00"}, :datetime).should be_kind_of(Time)
|
||||
end
|
||||
|
||||
it "should return array of Time objects when restriction is array of Time objects" do
|
||||
time1, time2 = Time.now, 1.day.ago
|
||||
restriction_value([time1, time2], :datetime).should == [time2, time1]
|
||||
evaluate_option_value([time1, time2], :datetime).should == [time2, time1]
|
||||
end
|
||||
|
||||
it "should return array of Time objects when restriction is array of strings" do
|
||||
time1, time2 = "2000-01-02", "2000-01-01"
|
||||
restriction_value([time1, time2], :datetime).should == [Person.parse_date_time(time2, :datetime), Person.parse_date_time(time1, :datetime)]
|
||||
evaluate_option_value([time1, time2], :datetime).should == [Person.parse_date_time(time2, :datetime), Person.parse_date_time(time1, :datetime)]
|
||||
end
|
||||
|
||||
it "should return array of Time objects when restriction is Range of Time objects" do
|
||||
time1, time2 = Time.now, 1.day.ago
|
||||
restriction_value(time1..time2, :datetime).should == [time2, time1]
|
||||
evaluate_option_value(time1..time2, :datetime).should == [time2, time1]
|
||||
end
|
||||
|
||||
it "should return array of Time objects when restriction is Range of time strings" do
|
||||
time1, time2 = "2000-01-02", "2000-01-01"
|
||||
restriction_value(time1..time2, :datetime).should == [Person.parse_date_time(time2, :datetime), Person.parse_date_time(time1, :datetime)]
|
||||
evaluate_option_value(time1..time2, :datetime).should == [Person.parse_date_time(time2, :datetime), Person.parse_date_time(time1, :datetime)]
|
||||
end
|
||||
def restriction_value(restriction, type)
|
||||
def evaluate_option_value(restriction, type)
|
||||
configure_validator(:type => type)
|
||||
validator.send(:restriction_value, restriction, person)
|
||||
validator.class.send(:evaluate_option_value, restriction, type, person)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -347,6 +346,37 @@ describe ValidatesTimeliness::Validator do
|
||||
end
|
||||
end
|
||||
|
||||
describe "instance with :with_time option" do
|
||||
|
||||
it "should validate date attribute as datetime combining value of :with_time against restrictions " do
|
||||
configure_validator(:type => :date, :with_time => '12:31', :on_or_before => Time.mktime(2000,1,1,12,30))
|
||||
validate_with(:birth_date, "2000-01-01")
|
||||
should_have_error(:birth_date, :on_or_before)
|
||||
end
|
||||
|
||||
it "should skip restriction validation if :with_time value is nil" do
|
||||
configure_validator(:type => :date, :with_time => nil, :on_or_before => Time.mktime(2000,1,1,12,30))
|
||||
validate_with(:birth_date, "2000-01-01")
|
||||
should_have_no_error(:birth_date, :on_or_before)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
describe "instance with :with_date option" do
|
||||
|
||||
it "should validate time attribute as datetime combining value of :with_date against restrictions " do
|
||||
configure_validator(:type => :time, :with_date => '2009-01-01', :on_or_before => Time.mktime(2000,1,1,12,30))
|
||||
validate_with(:birth_date, "12:30")
|
||||
should_have_error(:birth_date, :on_or_before)
|
||||
end
|
||||
|
||||
it "should skip restriction validation if :with_date value is nil" do
|
||||
configure_validator(:type => :time, :with_date => nil, :on_or_before => Time.mktime(2000,1,1,12,30))
|
||||
validate_with(:birth_date, "12:30")
|
||||
should_have_no_error(:birth_date, :on_or_before)
|
||||
end
|
||||
end
|
||||
|
||||
describe "instance with mixed value and restriction types" do
|
||||
|
||||
it "should validate datetime attribute with Date restriction" do
|
||||
|
||||
Reference in New Issue
Block a user