diff options
author | George Ogata <george.ogata@gmail.com> | 2008-06-09 13:45:57 +1000 |
---|---|---|
committer | Pratik Naik <pratiknaik@gmail.com> | 2008-06-17 19:45:18 +0100 |
commit | bc4a2f156b368334ca7267d15f4dd10864ac492d (patch) | |
tree | e5e94216fdbd306a9b753e5b14167d3cf17f33e4 | |
parent | 1b4b8fbbd95d7d674df7f013fd88b9b7f859a6f1 (diff) | |
download | rails-bc4a2f156b368334ca7267d15f4dd10864ac492d.tar.gz rails-bc4a2f156b368334ca7267d15f4dd10864ac492d.tar.bz2 rails-bc4a2f156b368334ca7267d15f4dd10864ac492d.zip |
Fix observers that use after_find. [#375 state:resolved]
Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
-rw-r--r-- | activerecord/lib/active_record/observer.rb | 2 | ||||
-rwxr-xr-x | activerecord/test/cases/lifecycle_test.rb | 17 |
2 files changed, 18 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/observer.rb b/activerecord/lib/active_record/observer.rb index 6e55e36b7d..25e0e61c69 100644 --- a/activerecord/lib/active_record/observer.rb +++ b/activerecord/lib/active_record/observer.rb @@ -189,7 +189,7 @@ module ActiveRecord def add_observer!(klass) klass.add_observer(self) - klass.class_eval 'def after_find() end' unless klass.respond_to?(:after_find) + klass.class_eval 'def after_find() end' unless klass.method_defined?(:after_find) end end end diff --git a/activerecord/test/cases/lifecycle_test.rb b/activerecord/test/cases/lifecycle_test.rb index ddcacacc3a..258f7c7a0f 100755 --- a/activerecord/test/cases/lifecycle_test.rb +++ b/activerecord/test/cases/lifecycle_test.rb @@ -2,6 +2,7 @@ require "cases/helper" require 'models/topic' require 'models/developer' require 'models/reply' +require 'models/minimalistic' class Topic; def after_find() end end class Developer; def after_find() end end @@ -44,6 +45,14 @@ class TopicObserver < ActiveRecord::Observer end end +class MinimalisticObserver < ActiveRecord::Observer + attr_reader :minimalistic + + def after_find(minimalistic) + @minimalistic = minimalistic + end +end + class MultiObserver < ActiveRecord::Observer attr_reader :record @@ -134,6 +143,14 @@ class LifecycleTest < ActiveRecord::TestCase assert_equal developer.name, multi_observer.record.name end + def test_observing_after_find_when_not_defined_on_the_model + observer = MinimalisticObserver.instance + assert_equal Minimalistic, MinimalisticObserver.observed_class + + minimalistic = Minimalistic.find(1) + assert_equal minimalistic, observer.minimalistic + end + def test_invalid_observer assert_raise(ArgumentError) { Topic.observers = Object.new; Topic.instantiate_observers } end |