mirror of
https://github.com/ditkrg/validates_timeliness.git
synced 2026-01-22 22:06:45 +00:00
Fix type_cast_value for values which don't respond to to_time or to_date (renatoelias)
This commit is contained in:
parent
e345b5dc7d
commit
509336e080
@ -2,7 +2,7 @@ module ValidatesTimeliness
|
|||||||
module Conversion
|
module Conversion
|
||||||
|
|
||||||
def type_cast_value(value, type)
|
def type_cast_value(value, type)
|
||||||
return nil if value.nil?
|
return nil if value.nil? || !value.respond_to?(:to_time)
|
||||||
|
|
||||||
value = value.in_time_zone if value.acts_like?(:time) && @timezone_aware
|
value = value.in_time_zone if value.acts_like?(:time) && @timezone_aware
|
||||||
value = case type
|
value = case type
|
||||||
|
|||||||
@ -22,6 +22,10 @@ describe ValidatesTimeliness::Conversion do
|
|||||||
it "should return date part of datetime value" do
|
it "should return date part of datetime value" do
|
||||||
type_cast_value(DateTime.new(2010, 1, 1, 0, 0, 0), :date).should == Date.new(2010, 1, 1)
|
type_cast_value(DateTime.new(2010, 1, 1, 0, 0, 0), :date).should == Date.new(2010, 1, 1)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'should return nil for invalid value types' do
|
||||||
|
type_cast_value(12, :date).should == nil
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "for time type" do
|
describe "for time type" do
|
||||||
@ -40,6 +44,10 @@ describe ValidatesTimeliness::Conversion do
|
|||||||
it "should return dummy date with time part for datetime value" do
|
it "should return dummy date with time part for datetime value" do
|
||||||
type_cast_value(DateTime.civil_from_format(:utc, 2010, 1, 1, 12, 34, 56), :time).should == Time.utc(2000, 1, 1, 12, 34, 56)
|
type_cast_value(DateTime.civil_from_format(:utc, 2010, 1, 1, 12, 34, 56), :time).should == Time.utc(2000, 1, 1, 12, 34, 56)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'should return nil for invalid value types' do
|
||||||
|
type_cast_value(12, :time).should == nil
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "for datetime type" do
|
describe "for datetime type" do
|
||||||
@ -63,6 +71,10 @@ describe ValidatesTimeliness::Conversion do
|
|||||||
result.should == Time.zone.local(2010, 1, 1, 23, 34, 56)
|
result.should == Time.zone.local(2010, 1, 1, 23, 34, 56)
|
||||||
result.zone.should == 'EST'
|
result.zone.should == 'EST'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'should return nil for invalid value types' do
|
||||||
|
type_cast_value(12, :datetime).should == nil
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "ignore_usec option" do
|
describe "ignore_usec option" do
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user