Browse files

make sure de-serialization happens on object instantiation

  • Loading branch information...
1 parent a0fac71 commit 5b42e9660201fc721075d2bfbe13edb0014dbde2 @tenderlove tenderlove committed Feb 1, 2011
View
2 activerecord/lib/active_record/attribute_methods/read.rb
@@ -54,7 +54,7 @@ def cacheable_column?(column)
# 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
View
3 activerecord/lib/active_record/attribute_methods/write.rb
@@ -23,8 +23,7 @@ def write_attribute(attr_name, value)
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
View
10 activerecord/lib/active_record/base.rb
@@ -1446,6 +1446,12 @@ def encode_with(coder)
# 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 @@ def arel_attributes_values(include_primary_key = true, include_readonly_attribut
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
View
6 activerecord/lib/active_record/validations/uniqueness.rb
@@ -15,6 +15,12 @@ def setup(klass)
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)
View
6 activerecord/test/cases/serialization_test.rb
@@ -23,6 +23,12 @@ def setup
@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

0 comments on commit 5b42e96

Please sign in to comment.