mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #20080 from robertjlooby/fix_overwriting_by_dynamic_finders
put dynamic matchers on GeneratedAssociationMethods instead of model
This commit is contained in:
commit
d5ba9a42a6
3 changed files with 24 additions and 3 deletions
|
@ -16,7 +16,7 @@ module ActiveRecord
|
||||||
|
|
||||||
if match && match.valid?
|
if match && match.valid?
|
||||||
match.define
|
match.define
|
||||||
send(name, *arguments, &block)
|
generated_association_methods.send(name, *arguments, &block)
|
||||||
else
|
else
|
||||||
super
|
super
|
||||||
end
|
end
|
||||||
|
@ -60,7 +60,7 @@ module ActiveRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
def define
|
def define
|
||||||
model.class_eval <<-CODE, __FILE__, __LINE__ + 1
|
model.generated_association_methods.class_eval <<-CODE, __FILE__, __LINE__ + 1
|
||||||
def self.#{name}(#{signature})
|
def self.#{name}(#{signature})
|
||||||
#{body}
|
#{body}
|
||||||
end
|
end
|
||||||
|
@ -70,7 +70,7 @@ module ActiveRecord
|
||||||
private
|
private
|
||||||
|
|
||||||
def body
|
def body
|
||||||
"#{finder}(#{attributes_hash})"
|
"#{model}.#{finder}(#{attributes_hash})"
|
||||||
end
|
end
|
||||||
|
|
||||||
# The parameters in the signature may have reserved Ruby words, in order
|
# The parameters in the signature may have reserved Ruby words, in order
|
||||||
|
|
|
@ -17,6 +17,7 @@ require 'models/matey'
|
||||||
require 'models/dog'
|
require 'models/dog'
|
||||||
require 'models/car'
|
require 'models/car'
|
||||||
require 'models/tyre'
|
require 'models/tyre'
|
||||||
|
require 'models/electron'
|
||||||
|
|
||||||
class FinderTest < ActiveRecord::TestCase
|
class FinderTest < ActiveRecord::TestCase
|
||||||
fixtures :companies, :topics, :entrants, :developers, :developers_projects, :posts, :comments, :accounts, :authors, :customers, :categories, :categorizations, :cars
|
fixtures :companies, :topics, :entrants, :developers, :developers_projects, :posts, :comments, :accounts, :authors, :customers, :categories, :categorizations, :cars
|
||||||
|
@ -799,6 +800,18 @@ class FinderTest < ActiveRecord::TestCase
|
||||||
assert_nil Topic.find_by_title("The First Topic!")
|
assert_nil Topic.find_by_title("The First Topic!")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_find_by_does_not_overwrite_method_on_class
|
||||||
|
Electron.create(name: 'First Electron')
|
||||||
|
|
||||||
|
assert_equal 0, Electron.times_called_find_by_name
|
||||||
|
|
||||||
|
Electron.find_by_name('First Electron')
|
||||||
|
assert_equal 1, Electron.times_called_find_by_name
|
||||||
|
|
||||||
|
Electron.find_by_name('Some Other Electron')
|
||||||
|
assert_equal 2, Electron.times_called_find_by_name
|
||||||
|
end
|
||||||
|
|
||||||
def test_find_by_one_attribute_bang
|
def test_find_by_one_attribute_bang
|
||||||
assert_equal topics(:first), Topic.find_by_title!("The First Topic")
|
assert_equal topics(:first), Topic.find_by_title!("The First Topic")
|
||||||
assert_raises_with_message(ActiveRecord::RecordNotFound, "Couldn't find Topic") do
|
assert_raises_with_message(ActiveRecord::RecordNotFound, "Couldn't find Topic") do
|
||||||
|
|
|
@ -2,4 +2,12 @@ class Electron < ActiveRecord::Base
|
||||||
belongs_to :molecule
|
belongs_to :molecule
|
||||||
|
|
||||||
validates_presence_of :name
|
validates_presence_of :name
|
||||||
|
|
||||||
|
cattr_reader :times_called_find_by_name
|
||||||
|
@@times_called_find_by_name = 0
|
||||||
|
|
||||||
|
def self.find_by_name(name)
|
||||||
|
@@times_called_find_by_name += 1
|
||||||
|
super(name)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue