diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2011-02-01 15:23:55 -0800 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2011-02-01 15:23:55 -0800 |
commit | 5b42e9660201fc721075d2bfbe13edb0014dbde2 (patch) | |
tree | 73b2d59a35fbceb9676bf5957d408ca0f9149f72 /activerecord/lib | |
parent | a0fac7192241f3242af410ca16e6dd43b933c98e (diff) | |
download | rails-5b42e9660201fc721075d2bfbe13edb0014dbde2.tar.gz rails-5b42e9660201fc721075d2bfbe13edb0014dbde2.tar.bz2 rails-5b42e9660201fc721075d2bfbe13edb0014dbde2.zip |
make sure de-serialization happens on object instantiation
Diffstat (limited to 'activerecord/lib')
4 files changed, 16 insertions, 5 deletions
diff --git a/activerecord/lib/active_record/attribute_methods/read.rb b/activerecord/lib/active_record/attribute_methods/read.rb index 895c16b9b3..ab86d8bad1 100644 --- a/activerecord/lib/active_record/attribute_methods/read.rb +++ b/activerecord/lib/active_record/attribute_methods/read.rb @@ -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 diff --git a/activerecord/lib/active_record/attribute_methods/write.rb b/activerecord/lib/active_record/attribute_methods/write.rb index 5eba94ec66..6a593a7e0e 100644 --- a/activerecord/lib/active_record/attribute_methods/write.rb +++ b/activerecord/lib/active_record/attribute_methods/write.rb @@ -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 diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 6b82b827b0..04b479bdb2 100644 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -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 diff --git a/activerecord/lib/active_record/validations/uniqueness.rb b/activerecord/lib/active_record/validations/uniqueness.rb index 76110b73e4..a96796f9ff 100644 --- a/activerecord/lib/active_record/validations/uniqueness.rb +++ b/activerecord/lib/active_record/validations/uniqueness.rb @@ -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) |