diff options
author | Kamal Fariz Mahyuddin <kamal.fariz@gmail.com> | 2011-02-09 23:37:30 +0800 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2011-02-22 16:25:19 -0800 |
commit | 6ba8caf3ee239fd7010b18ff251cb5c2d52b48c4 (patch) | |
tree | 1ca8bb6d5feffbb24fabb64b6c552b1dc3e98b53 /activerecord/test/cases | |
parent | 3265bbb65998d8175f3cd087f355a007bf4d2d47 (diff) | |
download | rails-6ba8caf3ee239fd7010b18ff251cb5c2d52b48c4.tar.gz rails-6ba8caf3ee239fd7010b18ff251cb5c2d52b48c4.tar.bz2 rails-6ba8caf3ee239fd7010b18ff251cb5c2d52b48c4.zip |
Fix observer callbacks firing multiple times on descendant instances
Diffstat (limited to 'activerecord/test/cases')
-rw-r--r-- | activerecord/test/cases/lifecycle_test.rb | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/activerecord/test/cases/lifecycle_test.rb b/activerecord/test/cases/lifecycle_test.rb index 0558deb71b..a42f5d9c9d 100644 --- a/activerecord/test/cases/lifecycle_test.rb +++ b/activerecord/test/cases/lifecycle_test.rb @@ -7,6 +7,12 @@ require 'models/comment' class SpecialDeveloper < Developer; end +class DeveloperObserver < ActiveRecord::Observer + def before_save(developer) + developer.salary += 1 + end +end + class SalaryChecker < ActiveRecord::Observer observe :special_developer attr_accessor :last_saved @@ -196,4 +202,12 @@ class LifecycleTest < ActiveRecord::TestCase assert_equal developer, SalaryChecker.instance.last_saved end + test "callback observing the ancestor does not fire multiple times on descendent" do + DeveloperObserver.instance # activate + developer = Developer.create! :name => 'Ancestor', :salary => 100000 + assert_equal 100001, developer.salary, 'ancestor callback fired multiple times' + developer = SpecialDeveloper.create! :name => 'Descendent', :salary => 100000 + assert_equal 100001, developer.salary, 'descendent callback fired multiple times' + end + end |