diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2015-09-09 05:50:42 -0300 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2015-09-09 05:50:42 -0300 |
commit | d5ba9a42a6e93b163a49f99d739aa56820e044d0 (patch) | |
tree | bb70a8bc3405c711fb18458b48937a5c93d4dcbd /activerecord/test | |
parent | 30c503395bf6bf7db1ec0295bd661ce644628db5 (diff) | |
parent | ad389286bbce5a43de34dc2df3968fa1f47181e1 (diff) | |
download | rails-d5ba9a42a6e93b163a49f99d739aa56820e044d0.tar.gz rails-d5ba9a42a6e93b163a49f99d739aa56820e044d0.tar.bz2 rails-d5ba9a42a6e93b163a49f99d739aa56820e044d0.zip |
Merge pull request #20080 from robertjlooby/fix_overwriting_by_dynamic_finders
put dynamic matchers on GeneratedAssociationMethods instead of model
Diffstat (limited to 'activerecord/test')
-rw-r--r-- | activerecord/test/cases/finder_test.rb | 13 | ||||
-rw-r--r-- | activerecord/test/models/electron.rb | 8 |
2 files changed, 21 insertions, 0 deletions
diff --git a/activerecord/test/cases/finder_test.rb b/activerecord/test/cases/finder_test.rb index 4b819a82e8..daf8299242 100644 --- a/activerecord/test/cases/finder_test.rb +++ b/activerecord/test/cases/finder_test.rb @@ -17,6 +17,7 @@ require 'models/matey' require 'models/dog' require 'models/car' require 'models/tyre' +require 'models/electron' class FinderTest < ActiveRecord::TestCase 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!") 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 assert_equal topics(:first), Topic.find_by_title!("The First Topic") assert_raises_with_message(ActiveRecord::RecordNotFound, "Couldn't find Topic") do diff --git a/activerecord/test/models/electron.rb b/activerecord/test/models/electron.rb index 6fc270673f..aacdaa71b7 100644 --- a/activerecord/test/models/electron.rb +++ b/activerecord/test/models/electron.rb @@ -2,4 +2,12 @@ class Electron < ActiveRecord::Base belongs_to :molecule 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 |