mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Allow YAML serialization when using TZ aware attributes
This commit is contained in:
parent
8c6c1dd82e
commit
fab487228c
5 changed files with 28 additions and 3 deletions
|
@ -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
|
||||
|
|
|
@ -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'
|
||||
|
|
14
activerecord/lib/active_record/type/decorator.rb
Normal file
14
activerecord/lib/active_record/type/decorator.rb
Normal file
|
@ -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
|
|
@ -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
|
||||
|
|
|
@ -681,6 +681,14 @@ class AttributeMethodsTest < ActiveRecord::TestCase
|
|||
end
|
||||
end
|
||||
|
||||
def test_yaml_dumping_record_with_time_zone_aware_attribute
|
||||
in_time_zone "Pacific Time (US & Canada)" do
|
||||
record = Topic.new(id: 1)
|
||||
record.written_on = "Jan 01 00:00:00 2014"
|
||||
assert_equal record, YAML.load(YAML.dump(record))
|
||||
end
|
||||
end
|
||||
|
||||
def test_setting_time_zone_conversion_for_attributes_should_write_value_on_class_variable
|
||||
Topic.skip_time_zone_conversion_for_attributes = [:field_a]
|
||||
Minimalistic.skip_time_zone_conversion_for_attributes = [:field_b]
|
||||
|
|
Loading…
Reference in a new issue