mirror of
https://github.com/ditkrg/validates_timeliness.git
synced 2026-01-24 14:56:43 +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
|
end
|
||||||
|
|
||||||
def parse_and_cast(value)
|
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)
|
validator.send(:type_cast_value, value)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@ -39,8 +39,6 @@ module ValidatesTimeliness
|
|||||||
def validate_restrictions(record, attr_name, value)
|
def validate_restrictions(record, attr_name, value)
|
||||||
restriction_methods = {:before => '<', :after => '>', :on_or_before => '<=', :on_or_after => '>='}
|
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]
|
display = ValidatesTimeliness.error_value_formats[type]
|
||||||
|
|
||||||
value = type_cast_value(value)
|
value = type_cast_value(value)
|
||||||
@ -48,7 +46,7 @@ module ValidatesTimeliness
|
|||||||
restriction_methods.each do |option, method|
|
restriction_methods.each do |option, method|
|
||||||
next unless restriction = configuration[option]
|
next unless restriction = configuration[option]
|
||||||
begin
|
begin
|
||||||
compare = self.class.restriction_value(restriction, record, type)
|
compare = restriction_value(restriction, record)
|
||||||
next if compare.nil?
|
next if compare.nil?
|
||||||
compare = type_cast_value(compare)
|
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 }
|
@custom_error_messages = configuration.inject({}) {|h, (k, v)| h[$1.to_sym] = v if k.to_s =~ /(.*)_message$/;h }
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.restriction_value(restriction, record, type)
|
def restriction_value(restriction, record)
|
||||||
case restriction
|
case restriction
|
||||||
when Time, Date, DateTime
|
when Time, Date, DateTime
|
||||||
restriction
|
restriction
|
||||||
when Symbol
|
when Symbol
|
||||||
restriction_value(record.send(restriction), record, type)
|
restriction_value(record.send(restriction), record)
|
||||||
when Proc
|
when Proc
|
||||||
restriction_value(restriction.call(record), record, type)
|
restriction_value(restriction.call(record), record)
|
||||||
else
|
else
|
||||||
record.class.parse_date_time(restriction, type, false)
|
record.class.parse_date_time(restriction, type, false)
|
||||||
end
|
end
|
||||||
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)
|
def type_cast_value(value)
|
||||||
case type
|
case type
|
||||||
when :time
|
when :time
|
||||||
|
|||||||
@ -18,33 +18,34 @@ describe ValidatesTimeliness::Validator do
|
|||||||
|
|
||||||
describe "restriction_value" do
|
describe "restriction_value" do
|
||||||
it "should return Time object when restriction is Time object" 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
|
end
|
||||||
|
|
||||||
it "should return Time object when restriction is string" do
|
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
|
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)
|
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
|
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")
|
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
|
end
|
||||||
|
|
||||||
it "should return Time object when restriction is proc which returns Time object" do
|
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
|
end
|
||||||
|
|
||||||
it "should return Time object when restriction is proc which returns string" do
|
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
|
end
|
||||||
|
|
||||||
def restriction_value(*args)
|
def restriction_value(restriction, type)
|
||||||
ValidatesTimeliness::Validator.send(:restriction_value, *args)
|
configure_validator(:type => type)
|
||||||
|
validator.send(:restriction_value, restriction, person)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user