aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRafael Mendonça França <rafaelmfranca@gmail.com>2013-12-11 14:38:27 -0800
committerRafael Mendonça França <rafaelmfranca@gmail.com>2013-12-11 14:38:27 -0800
commitc9b8ca97481fd88534cbe11c45181f3043a560ec (patch)
tree5313810bfcf98cfd0c7cd0590edadba7550f8902
parent6deb0a09c1e9b2d7c1d603978d26fd13aec50c83 (diff)
parent23ce3e5fde42d0af9b77ad014bde8f36401fa3b6 (diff)
downloadrails-c9b8ca97481fd88534cbe11c45181f3043a560ec.tar.gz
rails-c9b8ca97481fd88534cbe11c45181f3043a560ec.tar.bz2
rails-c9b8ca97481fd88534cbe11c45181f3043a560ec.zip
Merge pull request #13264 from laurocaetano/fix_dynamic_finder_with_reserved_words
Prevent invalid code when using dynamic finders with reserved ruby word.
-rw-r--r--activerecord/lib/active_record/dynamic_matchers.rb9
-rw-r--r--activerecord/test/cases/finder_test.rb8
-rw-r--r--activerecord/test/schema/schema.rb1
3 files changed, 16 insertions, 2 deletions
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
diff --git a/activerecord/test/cases/finder_test.rb b/activerecord/test/cases/finder_test.rb
index 5e8f1c851f..7a00f7fa8f 100644
--- a/activerecord/test/cases/finder_test.rb
+++ b/activerecord/test/cases/finder_test.rb
@@ -12,6 +12,7 @@ require 'models/developer'
require 'models/customer'
require 'models/toy'
require 'models/matey'
+require 'models/dog'
class FinderTest < ActiveRecord::TestCase
fixtures :companies, :topics, :entrants, :developers, :developers_projects, :posts, :comments, :accounts, :authors, :customers, :categories, :categorizations
@@ -641,6 +642,13 @@ class FinderTest < ActiveRecord::TestCase
assert_raise(ActiveRecord::RecordNotFound) { Topic.find_by_title!("The First Topic!") }
end
+ def test_find_by_on_attribute_that_is_a_reserved_word
+ dog_alias = 'Dog'
+ dog = Dog.create(alias: dog_alias)
+
+ assert_equal dog, Dog.find_by_alias(dog_alias)
+ end
+
def test_find_by_one_attribute_that_is_an_alias
assert_equal topics(:first), Topic.find_by_heading("The First Topic")
assert_nil Topic.find_by_heading("The First Topic!")
diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb
index 92e2e4d409..ac546fc296 100644
--- a/activerecord/test/schema/schema.rb
+++ b/activerecord/test/schema/schema.rb
@@ -245,6 +245,7 @@ ActiveRecord::Schema.define do
t.integer :trainer_id
t.integer :breeder_id
t.integer :dog_lover_id
+ t.string :alias
end
create_table :edges, force: true, id: false do |t|