From f936a1f100e75082081e782e5cceb272885c2df7 Mon Sep 17 00:00:00 2001 From: Eric Chapweske Date: Sat, 17 Oct 2009 12:37:15 -0500 Subject: Refactoring attributes/types [#3348 state:resolved] Signed-off-by: Joshua Peek --- .../lib/active_record/types/time_with_zone.rb | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 activerecord/lib/active_record/types/time_with_zone.rb (limited to 'activerecord/lib/active_record/types/time_with_zone.rb') diff --git a/activerecord/lib/active_record/types/time_with_zone.rb b/activerecord/lib/active_record/types/time_with_zone.rb new file mode 100644 index 0000000000..3a8b9292f9 --- /dev/null +++ b/activerecord/lib/active_record/types/time_with_zone.rb @@ -0,0 +1,20 @@ +module ActiveRecord + module Type + class TimeWithZone < Object + + def cast(time) + time = super(time) + time.acts_like?(:time) ? time.in_time_zone : time + end + + def precast(time) + unless time.acts_like?(:time) + time = time.is_a?(String) ? ::Time.zone.parse(time) : time.to_time rescue time + end + time = time.in_time_zone rescue nil if time + super(time) + end + + end + end +end -- cgit v1.2.3