提交 dd1ec625 编写于 作者: G Godfrey Chan

Fixed a regression introduced in 84cf156e

84cf156e (PR #15694) introduced a subtle regression. There are actually three
distinct entry points to creating an AR object – via .new (i.e. #initialize),
via #init_with (e.g. from YAML or database queries) and via .allocate.

With the patch in 84cf156e, attribute methods and respond_to? will not work
correctly when objects are allocated directly, without going through either

The reason this test case didn't catch the regression was that the `Topic`
class is shared between test cases, so by the time this test case is ran the
attribute methods are very likely to be defined.

Switching to use a fresh anonymous class in the test to ensure we surface this
problem in the future.
上级 ee4e86fa
......@@ -108,6 +108,11 @@ def self.connection_handler=(handler)
end
module ClassMethods
def allocate
define_attribute_methods
super
end
def initialize_find_by_cache
self.find_by_statement_cache = {}.extend(Mutex_m)
end
......
......@@ -143,7 +143,11 @@ def test_id_before_type_cast_with_custom_primary_key
# Syck calls respond_to? before actually calling initialize
def test_respond_to_with_allocated_object
topic = Topic.allocate
klass = Class.new(ActiveRecord::Base) do
self.table_name = 'topics'
end
topic = klass.allocate
assert !topic.respond_to?("nothingness")
assert !topic.respond_to?(:nothingness)
assert_respond_to topic, "title"
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册