From dd0357aeb85b6466dea97800c9f45cb6080f1324 Mon Sep 17 00:00:00 2001 From: Chris Salzberg Date: Wed, 17 Apr 2019 21:08:11 +0900 Subject: Make plain matcher match first, not last This code takes the "plain" matcher with no prefix and suffix and puts it at the end of the matchers array such that it is de-prioritized among all matchers. The comment explaining this code, originally from commimt 8b8b7143efe dated in 2011, is in a context where detection from matchers happened immediately. In that situation, the plain matcher would indeed always match first and no others would ever be used. However, the current code does not immediately detect one match but rather maps matchers to matches for the method_name. Detection only happens for matches whose attribute name is valid. In this context, there is no need to bump the plain matcher to the end of the array, since it will only be selected if the attribute name it catpures matches a valid attribute name. --- activemodel/lib/active_model/attribute_methods.rb | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) (limited to 'activemodel') diff --git a/activemodel/lib/active_model/attribute_methods.rb b/activemodel/lib/active_model/attribute_methods.rb index 415f1f679b..cbd7798543 100644 --- a/activemodel/lib/active_model/attribute_methods.rb +++ b/activemodel/lib/active_model/attribute_methods.rb @@ -352,11 +352,7 @@ module ActiveModel def attribute_method_matchers_matching(method_name) attribute_method_matchers_cache.compute_if_absent(method_name) do - # Bump plain matcher to last place so that only methods that do not - # match any other pattern match the actual attribute name. - # This is currently only needed to support legacy usage. - matchers = attribute_method_matchers.partition(&:plain?).reverse.flatten(1) - matchers.map { |matcher| matcher.match(method_name) }.compact + attribute_method_matchers.map { |matcher| matcher.match(method_name) }.compact end end -- cgit v1.2.3