From fab487228c880c3b85b06de1770679d45f7a7dfa Mon Sep 17 00:00:00 2001 From: Sean Griffin Date: Wed, 17 Sep 2014 12:37:00 -0600 Subject: Allow YAML serialization when using TZ aware attributes --- .../attribute_methods/time_zone_conversion.rb | 2 ++ activerecord/lib/active_record/type.rb | 1 + activerecord/lib/active_record/type/decorator.rb | 14 ++++++++++++++ activerecord/lib/active_record/type/serialized.rb | 6 +++--- 4 files changed, 20 insertions(+), 3 deletions(-) create mode 100644 activerecord/lib/active_record/type/decorator.rb (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/attribute_methods/time_zone_conversion.rb b/activerecord/lib/active_record/attribute_methods/time_zone_conversion.rb index f439bd1ffe..b7fe079ef5 100644 --- a/activerecord/lib/active_record/attribute_methods/time_zone_conversion.rb +++ b/activerecord/lib/active_record/attribute_methods/time_zone_conversion.rb @@ -2,6 +2,8 @@ module ActiveRecord module AttributeMethods module TimeZoneConversion class TimeZoneConverter < SimpleDelegator # :nodoc: + include Type::Decorator + def type_cast_from_database(value) convert_time_to_time_zone(super) end diff --git a/activerecord/lib/active_record/type.rb b/activerecord/lib/active_record/type.rb index f1384e0bb2..e3d6c5957e 100644 --- a/activerecord/lib/active_record/type.rb +++ b/activerecord/lib/active_record/type.rb @@ -1,3 +1,4 @@ +require 'active_record/type/decorator' require 'active_record/type/mutable' require 'active_record/type/numeric' require 'active_record/type/time_value' diff --git a/activerecord/lib/active_record/type/decorator.rb b/activerecord/lib/active_record/type/decorator.rb new file mode 100644 index 0000000000..9fce38ea44 --- /dev/null +++ b/activerecord/lib/active_record/type/decorator.rb @@ -0,0 +1,14 @@ +module ActiveRecord + module Type + module Decorator # :nodoc: + def init_with(coder) + @subtype = coder['subtype'] + __setobj__(@subtype) + end + + def encode_with(coder) + coder['subtype'] = __getobj__ + end + end + end +end diff --git a/activerecord/lib/active_record/type/serialized.rb b/activerecord/lib/active_record/type/serialized.rb index 5b512433b0..17004b3593 100644 --- a/activerecord/lib/active_record/type/serialized.rb +++ b/activerecord/lib/active_record/type/serialized.rb @@ -2,6 +2,7 @@ module ActiveRecord module Type class Serialized < SimpleDelegator # :nodoc: include Mutable + include Decorator attr_reader :subtype, :coder @@ -36,14 +37,13 @@ module ActiveRecord end def init_with(coder) - @subtype = coder['subtype'] @coder = coder['coder'] - __setobj__(@subtype) + super end def encode_with(coder) - coder['subtype'] = @subtype coder['coder'] = @coder + super end private -- cgit v1.2.3