mirror of
https://github.com/ditkrg/validates_timeliness.git
synced 2026-01-23 14:27:52 +00:00
made restriction_value instance method as it relies on validator instance attribute type
removed old type_cast_method class method
This commit is contained in:
parent
0c5cc1a536
commit
6cd6cd9dc0
@ -70,7 +70,7 @@ module Spec
|
||||
end
|
||||
|
||||
def parse_and_cast(value)
|
||||
value = ValidatesTimeliness::Validator.send(:restriction_value, value, record, options[:type])
|
||||
value = validator.send(:restriction_value, value, record)
|
||||
validator.send(:type_cast_value, value)
|
||||
end
|
||||
|
||||
|
||||
@ -39,8 +39,6 @@ module ValidatesTimeliness
|
||||
def validate_restrictions(record, attr_name, value)
|
||||
restriction_methods = {:before => '<', :after => '>', :on_or_before => '<=', :on_or_after => '>='}
|
||||
|
||||
type_cast_method = self.class.restriction_type_cast_method(type)
|
||||
|
||||
display = ValidatesTimeliness.error_value_formats[type]
|
||||
|
||||
value = type_cast_value(value)
|
||||
@ -48,7 +46,7 @@ module ValidatesTimeliness
|
||||
restriction_methods.each do |option, method|
|
||||
next unless restriction = configuration[option]
|
||||
begin
|
||||
compare = self.class.restriction_value(restriction, record, type)
|
||||
compare = restriction_value(restriction, record)
|
||||
next if compare.nil?
|
||||
compare = type_cast_value(compare)
|
||||
|
||||
@ -85,27 +83,19 @@ module ValidatesTimeliness
|
||||
@custom_error_messages = configuration.inject({}) {|h, (k, v)| h[$1.to_sym] = v if k.to_s =~ /(.*)_message$/;h }
|
||||
end
|
||||
|
||||
def self.restriction_value(restriction, record, type)
|
||||
def restriction_value(restriction, record)
|
||||
case restriction
|
||||
when Time, Date, DateTime
|
||||
restriction
|
||||
when Symbol
|
||||
restriction_value(record.send(restriction), record, type)
|
||||
restriction_value(record.send(restriction), record)
|
||||
when Proc
|
||||
restriction_value(restriction.call(record), record, type)
|
||||
restriction_value(restriction.call(record), record)
|
||||
else
|
||||
record.class.parse_date_time(restriction, type, false)
|
||||
end
|
||||
end
|
||||
|
||||
def self.restriction_type_cast_method(type)
|
||||
case type
|
||||
when :time then :to_dummy_time
|
||||
when :date then :to_date
|
||||
when :datetime then :to_time
|
||||
end
|
||||
end
|
||||
|
||||
def type_cast_value(value)
|
||||
case type
|
||||
when :time
|
||||
|
||||
@ -18,33 +18,34 @@ describe ValidatesTimeliness::Validator do
|
||||
|
||||
describe "restriction_value" do
|
||||
it "should return Time object when restriction is Time object" do
|
||||
restriction_value(Time.now, person, :datetime).should be_kind_of(Time)
|
||||
restriction_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", person, :datetime).should be_kind_of(Time)
|
||||
restriction_value("2007-01-01 12:00", :datetime).should be_kind_of(Time)
|
||||
end
|
||||
|
||||
it "should return Time object when restriction is method symbol which returns Time object" do
|
||||
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, person, :datetime).should be_kind_of(Time)
|
||||
restriction_value(:datetime_attr, :datetime).should be_kind_of(Time)
|
||||
end
|
||||
|
||||
it "should return Time object when restriction is method symbol which returns string" do
|
||||
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, person, :datetime).should be_kind_of(Time)
|
||||
restriction_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 }, person, :datetime).should be_kind_of(Time)
|
||||
restriction_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"}, person, :datetime).should be_kind_of(Time)
|
||||
restriction_value(lambda {"2007-01-01 12:00"}, :datetime).should be_kind_of(Time)
|
||||
end
|
||||
|
||||
def restriction_value(*args)
|
||||
ValidatesTimeliness::Validator.send(:restriction_value, *args)
|
||||
def restriction_value(restriction, type)
|
||||
configure_validator(:type => type)
|
||||
validator.send(:restriction_value, restriction, person)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user