mirror of
https://github.com/github/rails.git
synced 2026-04-26 03:00:59 -04:00
make sure de-serialization happens on object instantiation
This commit is contained in:
@@ -54,7 +54,7 @@ module ActiveRecord
|
||||
|
||||
# Define read method for serialized attribute.
|
||||
def define_read_method_for_serialized_attribute(attr_name)
|
||||
access_code = "@attributes_cache['#{attr_name}'] ||= unserialize_attribute('#{attr_name}')"
|
||||
access_code = "@attributes_cache['#{attr_name}'] ||= @attributes['#{attr_name}']"
|
||||
generated_attribute_methods.module_eval("def _#{attr_name}; #{access_code}; end; alias #{attr_name} _#{attr_name}", __FILE__, __LINE__)
|
||||
end
|
||||
|
||||
|
||||
@@ -23,8 +23,7 @@ module ActiveRecord
|
||||
if (column = column_for_attribute(attr_name)) && column.number?
|
||||
@attributes[attr_name] = convert_number_column_value(value)
|
||||
else
|
||||
coder = self.class.serialized_attributes[attr_name]
|
||||
@attributes[attr_name] = coder ? coder.dump(value) : value
|
||||
@attributes[attr_name] = value
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -1446,6 +1446,12 @@ MSG
|
||||
# post.title # => 'hello world'
|
||||
def init_with(coder)
|
||||
@attributes = coder['attributes']
|
||||
|
||||
(@attributes.keys & self.class.serialized_attributes.keys).each do |key|
|
||||
coder = self.class.serialized_attributes[key]
|
||||
@attributes[key] = coder.load @attributes[key]
|
||||
end
|
||||
|
||||
@attributes_cache, @previously_changed, @changed_attributes = {}, {}, {}
|
||||
@association_cache = {}
|
||||
@aggregation_cache = {}
|
||||
@@ -1748,8 +1754,8 @@ MSG
|
||||
|
||||
if include_readonly_attributes || (!include_readonly_attributes && !self.class.readonly_attributes.include?(name))
|
||||
|
||||
value = if klass.serialized_attributes[name]
|
||||
@attributes[name]
|
||||
value = if coder = klass.serialized_attributes[name]
|
||||
coder.dump @attributes[name]
|
||||
else
|
||||
# FIXME: we need @attributes to be used consistently.
|
||||
# If the values stored in @attributes were already type
|
||||
|
||||
@@ -15,6 +15,12 @@ module ActiveRecord
|
||||
def validate_each(record, attribute, value)
|
||||
finder_class = find_finder_class_for(record)
|
||||
|
||||
coder = record.class.serialized_attributes[attribute.to_s]
|
||||
|
||||
if value && coder
|
||||
value = coder.dump value
|
||||
end
|
||||
|
||||
sql, params = mount_sql_and_params(finder_class, record.class.quoted_table_name, attribute, value)
|
||||
|
||||
relation = finder_class.unscoped.where(sql, *params)
|
||||
|
||||
@@ -23,6 +23,12 @@ class SerializationTest < ActiveRecord::TestCase
|
||||
@contact = Contact.new(@contact_attributes)
|
||||
end
|
||||
|
||||
def test_serialized_init_with
|
||||
topic = Topic.allocate
|
||||
topic.init_with('attributes' => { 'content' => '--- foo' })
|
||||
assert_equal 'foo', topic.content
|
||||
end
|
||||
|
||||
def test_to_xml
|
||||
xml = REXML::Document.new(topics(:first).to_xml(:indent => 0))
|
||||
bonus_time_in_current_timezone = topics(:first).bonus_time.xmlschema
|
||||
|
||||
Reference in New Issue
Block a user