提交 d1f003e6 编写于 作者: S Sean Griffin

Correctly handle multiple attribute method prefix/suffixes which match

Active Record defines `attribute_method_suffix :?`. That suffix will
match any predicate method when the lookup occurs in Active Model. This
will make it incorrectly decide that `id_changed?` should not exist,
because it attempts to determine if the attribute `id_changed` is
present, rather than `id` with the `_changed?` suffix. Instead, we will
look for any correct match.
上级 08ff4ccb
......@@ -353,14 +353,12 @@ def attribute_method_matchers_cache #:nodoc:
@attribute_method_matchers_cache ||= ThreadSafe::Cache.new(initial_capacity: 4)
end
def attribute_method_matcher(method_name) #:nodoc:
def attribute_method_matchers_matching(method_name) #:nodoc:
attribute_method_matchers_cache.compute_if_absent(method_name) do
# Must try to match prefixes/suffixes first, or else the matcher with no prefix/suffix
# will match every time.
matchers = attribute_method_matchers.partition(&:plain?).reverse.flatten(1)
match = nil
matchers.detect { |method| match = method.match(method_name) }
match
matchers.map { |method| method.match(method_name) }.compact
end
end
......@@ -469,8 +467,8 @@ def attribute_method?(attr_name) #:nodoc:
# Returns a struct representing the matching attribute method.
# The struct's attributes are prefix, base and suffix.
def match_attribute_method?(method_name)
match = self.class.send(:attribute_method_matcher, method_name)
match if match && attribute_method?(match.attr_name)
matches = self.class.send(:attribute_method_matchers_matching, method_name)
matches.detect { |match| attribute_method?(match.attr_name) }
end
def missing_attribute(attr_name, stack)
......
......@@ -1527,4 +1527,14 @@ def test_default_values_are_deeply_dupped
test "records without an id have unique hashes" do
assert_not_equal Post.new.hash, Post.new.hash
end
test "resetting column information doesn't remove attribute methods" do
topic = topics(:first)
assert_not topic.id_changed?
Topic.reset_column_information
assert_not topic.id_changed?
end
end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册