From 1dbac5190b8d0d53719951634cdc00991bb1e71e Mon Sep 17 00:00:00 2001 From: Adam Meehan Date: Thu, 5 Mar 2009 19:56:38 +1100 Subject: [PATCH] fix last refactor so it casts Date object to time if attribute is datetime or time with spec added --- lib/validates_timeliness/active_record/attribute_methods.rb | 2 +- spec/active_record/attribute_methods_spec.rb | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/validates_timeliness/active_record/attribute_methods.rb b/lib/validates_timeliness/active_record/attribute_methods.rb index 5fb04b5..6ee1905 100644 --- a/lib/validates_timeliness/active_record/attribute_methods.rb +++ b/lib/validates_timeliness/active_record/attribute_methods.rb @@ -48,7 +48,7 @@ module ValidatesTimeliness def write_date_time_attribute(attr_name, value, type, time_zone_aware) new = self.class.parse_date_time(value, type) - if new.acts_like?(:time) + if new && type != :date new = new.to_time new = new.in_time_zone if time_zone_aware end diff --git a/spec/active_record/attribute_methods_spec.rb b/spec/active_record/attribute_methods_spec.rb index 5defacd..5aba593 100644 --- a/spec/active_record/attribute_methods_spec.rb +++ b/spec/active_record/attribute_methods_spec.rb @@ -69,6 +69,11 @@ describe ValidatesTimeliness::ActiveRecord::AttributeMethods do @person.birth_date_and_time.should be_kind_of(Time) end + it "should return Time object for datetime attribute read method when assigned Date object" do + @person.birth_date_and_time = Date.today + @person.birth_date_and_time.should be_kind_of(Time) + end + 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)