mirror of
https://github.com/github/rails.git
synced 2026-02-05 11:45:13 -05:00
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
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
*SVN*
|
||||
|
||||
* Dynamically generate reader methods for serialized attributes. #6362 [Stefan Kaes]
|
||||
|
||||
* Deprecation: object transactions warning. [Jeremy Kemper]
|
||||
|
||||
* has_one :dependent => :nullify ignores nil associates. #6528 [janovetz, Jeremy Kemper]
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -1476,7 +1476,7 @@ class BasicsTest < Test::Unit::TestCase
|
||||
|
||||
private
|
||||
def assert_readers(model, exceptions)
|
||||
expected_readers = Set.new(model.column_names - (model.serialized_attributes.keys + ['id']))
|
||||
expected_readers = Set.new(model.column_names - ['id'])
|
||||
expected_readers += expected_readers.map { |col| "#{col}?" }
|
||||
expected_readers -= exceptions
|
||||
assert_equal expected_readers, model.read_methods
|
||||
|
||||
Reference in New Issue
Block a user