diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2010-08-30 14:09:16 -0700 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2010-09-09 14:37:57 -0700 |
commit | 4b7b8d9e19b662d8a1135fec73b422202f97472a (patch) | |
tree | c747270b5ec551f6d96eb7e47da71b2bb389ae34 /activerecord/lib/active_record/attribute_methods/read.rb | |
parent | cc81b04ae540c0b75f5428951e11dea2f13a0946 (diff) | |
download | rails-4b7b8d9e19b662d8a1135fec73b422202f97472a.tar.gz rails-4b7b8d9e19b662d8a1135fec73b422202f97472a.tar.bz2 rails-4b7b8d9e19b662d8a1135fec73b422202f97472a.zip |
Cache unserialized attributes
Diffstat (limited to 'activerecord/lib/active_record/attribute_methods/read.rb')
-rw-r--r-- | activerecord/lib/active_record/attribute_methods/read.rb | 14 |
1 files changed, 9 insertions, 5 deletions
diff --git a/activerecord/lib/active_record/attribute_methods/read.rb b/activerecord/lib/active_record/attribute_methods/read.rb index 3da3d9d8cc..083c8f88b1 100644 --- a/activerecord/lib/active_record/attribute_methods/read.rb +++ b/activerecord/lib/active_record/attribute_methods/read.rb @@ -26,8 +26,7 @@ module ActiveRecord # Returns the attributes which are cached. By default time related columns # with datatype <tt>:datetime, :timestamp, :time, :date</tt> are cached. def cached_attributes - @cached_attributes ||= - columns.select{|c| attribute_types_cached_by_default.include?(c.type)}.map{|col| col.name}.to_set + @cached_attributes ||= columns.select { |c| cacheable_column?(c) }.map { |col| col.name }.to_set end # Returns +true+ if the provided attribute is being cached. @@ -37,7 +36,7 @@ module ActiveRecord protected def define_method_attribute(attr_name) - if self.serialized_attributes[attr_name] + if serialized_attributes.include?(attr_name) define_read_method_for_serialized_attribute(attr_name) else define_read_method(attr_name.to_sym, attr_name, columns_hash[attr_name]) @@ -49,9 +48,14 @@ module ActiveRecord end private + def cacheable_column?(column) + serialized_attributes.include?(column.name) || attribute_types_cached_by_default.include?(column.type) + end + # Define read method for serialized attribute. def define_read_method_for_serialized_attribute(attr_name) - generated_attribute_methods.module_eval("def #{attr_name}; unserialize_attribute('#{attr_name}'); end", __FILE__, __LINE__) + access_code = "@attributes_cache['#{attr_name}'] ||= unserialize_attribute('#{attr_name}')" + generated_attribute_methods.module_eval("def #{attr_name}; #{access_code}; end", __FILE__, __LINE__) end # Define an attribute reader method. Cope with nil column. @@ -92,7 +96,7 @@ module ActiveRecord # Returns true if the attribute is of a text column and marked for serialization. def unserializable_attribute?(attr_name, column) - column.text? && self.class.serialized_attributes[attr_name] + column.text? && self.class.serialized_attributes.include?(attr_name) end # Returns the unserialized object of the attribute. |