diff options
author | George Ogata <george.ogata@gmail.com> | 2008-07-23 06:38:26 +1000 |
---|---|---|
committer | Michael Koziarski <michael@koziarski.com> | 2008-07-26 16:26:08 +0200 |
commit | e8fc894f66daa7909d1790f2cd145844d256d282 (patch) | |
tree | d3d694cd8d8aa861b34037e282e9aec4184506e3 | |
parent | f48b9ab5c2741ddbdbc0a9f4cd06875a1e3c8b02 (diff) | |
download | rails-e8fc894f66daa7909d1790f2cd145844d256d282.tar.gz rails-e8fc894f66daa7909d1790f2cd145844d256d282.tar.bz2 rails-e8fc894f66daa7909d1790f2cd145844d256d282.zip |
Make observers define #after_find in the model only if needed.
[#676 state:resolved]
Signed-off-by: Michael Koziarski <michael@koziarski.com>
-rw-r--r-- | activerecord/lib/active_record/observer.rb | 3 | ||||
-rwxr-xr-x | activerecord/test/cases/lifecycle_test.rb | 32 |
2 files changed, 33 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/observer.rb b/activerecord/lib/active_record/observer.rb index c96e5f9d51..b35e407cc1 100644 --- a/activerecord/lib/active_record/observer.rb +++ b/activerecord/lib/active_record/observer.rb @@ -189,6 +189,9 @@ module ActiveRecord def add_observer!(klass) klass.add_observer(self) + if respond_to?(:after_find) && !klass.method_defined?(:after_find) + klass.class_eval 'def after_find() end' + end end end end diff --git a/activerecord/test/cases/lifecycle_test.rb b/activerecord/test/cases/lifecycle_test.rb index 3432abee31..ab005c6b00 100755 --- a/activerecord/test/cases/lifecycle_test.rb +++ b/activerecord/test/cases/lifecycle_test.rb @@ -143,12 +143,12 @@ class LifecycleTest < ActiveRecord::TestCase assert_equal developer.name, multi_observer.record.name end - def test_after_find_cannot_be_observed_when_its_not_defined_on_the_model + def test_after_find_can_be_observed_when_its_not_defined_on_the_model observer = MinimalisticObserver.instance assert_equal Minimalistic, MinimalisticObserver.observed_class minimalistic = Minimalistic.find(1) - assert_nil observer.minimalistic + assert_equal minimalistic, observer.minimalistic end def test_after_find_can_be_observed_when_its_defined_on_the_model @@ -159,6 +159,34 @@ class LifecycleTest < ActiveRecord::TestCase assert_equal topic, observer.topic end + def test_after_find_is_not_created_if_its_not_used + # use a fresh class so an observer can't have defined an + # after_find on it + model_class = Class.new(ActiveRecord::Base) + observer_class = Class.new(ActiveRecord::Observer) + observer_class.observe(model_class) + + observer = observer_class.instance + + assert !model_class.method_defined?(:after_find) + end + + def test_after_find_is_not_clobbered_if_it_already_exists + # use a fresh observer class so we can instantiate it (Observer is + # a Singleton) + model_class = Class.new(ActiveRecord::Base) do + def after_find; end + end + original_method = model_class.instance_method(:after_find) + observer_class = Class.new(ActiveRecord::Observer) do + def after_find; end + end + observer_class.observe(model_class) + + observer = observer_class.instance + assert_equal original_method, model_class.instance_method(:after_find) + end + def test_invalid_observer assert_raise(ArgumentError) { Topic.observers = Object.new; Topic.instantiate_observers } end |