diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2006-11-02 20:20:46 +0000 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2006-11-02 20:20:46 +0000 |
commit | c7c3994d31322a00ab591b9365da5ca63dccaa90 (patch) | |
tree | 21618f5b3e367645b1b29d12c90284b5ea4ab6d8 /activerecord/lib/active_record | |
parent | ea433f15bf98a426239e985220393f63fa6d56c3 (diff) | |
download | rails-c7c3994d31322a00ab591b9365da5ca63dccaa90.tar.gz rails-c7c3994d31322a00ab591b9365da5ca63dccaa90.tar.bz2 rails-c7c3994d31322a00ab591b9365da5ca63dccaa90.zip |
Dynamically generate reader methods for serialized attributes. Closes #6362.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5416 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/lib/active_record')
-rwxr-xr-x | activerecord/lib/active_record/base.rb | 22 |
1 files changed, 19 insertions, 3 deletions
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index d704caafc4..3fc9b4bddb 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -1874,9 +1874,16 @@ module ActiveRecord #:nodoc: # ActiveRecord::Base.generate_read_methods is set to true. def define_read_methods self.class.columns_hash.each do |name, column| - unless self.class.serialized_attributes[name] - define_read_method(name.to_sym, name, column) unless respond_to_without_attributes?(name) - define_question_method(name) unless respond_to_without_attributes?("#{name}?") + unless respond_to_without_attributes?(name) + if self.class.serialized_attributes[name] + define_read_method_for_serialized_attribute(name) + else + define_read_method(name.to_sym, name, column) + end + end + + unless respond_to_without_attributes?("#{name}?") + define_question_method(name) end end end @@ -1894,6 +1901,15 @@ module ActiveRecord #:nodoc: evaluate_read_method attr_name, "def #{symbol}; #{access_code}; end" end + # Define read method for serialized attribute. + def define_read_method_for_serialized_attribute(attr_name) + unless attr_name.to_s == self.class.primary_key.to_s + self.class.read_methods << attr_name + end + + evaluate_read_method attr_name, "def #{attr_name}; unserialize_attribute('#{attr_name}'); end" + end + # Define an attribute ? method. def define_question_method(attr_name) unless attr_name.to_s == self.class.primary_key.to_s |