From 23ce3e5fde42d0af9b77ad014bde8f36401fa3b6 Mon Sep 17 00:00:00 2001 From: Lauro Caetano Date: Wed, 11 Dec 2013 00:05:16 -0200 Subject: Prevent invalid code when using dynamic finders with Ruby's reserved words. The dynamic finder was creating the method signature with the parameters name, which may have reserved words and this way creating invalid Ruby code. Closes: #13261 Example: # Before Dog.find_by_alias('dog name') # Was creating this method def self.find_by_alias(alias, options = {}) # After Dog.find_by_alias('dog name') # Will create this method def self.find_by_alias(_alias, options = {}) --- activerecord/lib/active_record/dynamic_matchers.rb | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/dynamic_matchers.rb b/activerecord/lib/active_record/dynamic_matchers.rb index e650ebcf64..5caab09038 100644 --- a/activerecord/lib/active_record/dynamic_matchers.rb +++ b/activerecord/lib/active_record/dynamic_matchers.rb @@ -84,13 +84,18 @@ module ActiveRecord "#{finder}(#{attributes_hash})" end + # The parameters in the signature may have reserved Ruby words, in order + # to prevent errors, we start each param name with `_`. + # # Extended in activerecord-deprecated_finders def signature - attribute_names.join(', ') + attribute_names.map { |name| "_#{name}" }.join(', ') end + # Given that the parameters starts with `_`, the finder needs to use the + # same parameter name. def attributes_hash - "{" + attribute_names.map { |name| ":#{name} => #{name}" }.join(',') + "}" + "{" + attribute_names.map { |name| ":#{name} => _#{name}" }.join(',') + "}" end def finder -- cgit v1.2.3