Permalink
Browse files

Observers not longer add an after_find method to the observed class.

[#625 state:resolved]
  • Loading branch information...
1 parent c7acfbb commit fbef982e4b906b879240a35a1ecff447007da6b2 Stefan Kaes committed with jeremy Jul 15, 2008
Showing with 14 additions and 7 deletions.
  1. +4 −5 activerecord/lib/active_record/observer.rb
  2. +10 −2 activerecord/test/cases/lifecycle_test.rb
View
9 activerecord/lib/active_record/observer.rb
@@ -20,7 +20,7 @@ module ClassMethods
# ActiveRecord::Base.observers = Cacher, GarbageCollector
#
# Note: Setting this does not instantiate the observers yet. +instantiate_observers+ is
- # called during startup, and before each development request.
+ # called during startup, and before each development request.
def observers=(*observers)
@observers = observers.flatten
end
@@ -130,11 +130,11 @@ def inherited(subclass)
# Observers register themselves in the model class they observe, since it is the class that
# notifies them of events when they occur. As a side-effect, when an observer is loaded its
# corresponding model class is loaded.
- #
+ #
# Up to (and including) Rails 2.0.2 observers were instantiated between plugins and
- # application initializers. Now observers are loaded after application initializers,
+ # application initializers. Now observers are loaded after application initializers,
# so observed models can make use of extensions.
- #
+ #
# If by any chance you are using observed models in the initialization you can still
# load their observers by calling <tt>ModelObserver.instance</tt> before. Observers are
# singletons and that call instantiates and registers them.
@@ -189,7 +189,6 @@ def observed_subclasses
def add_observer!(klass)
klass.add_observer(self)
- klass.class_eval 'def after_find() end' unless klass.method_defined?(:after_find)
end
end
end
View
12 activerecord/test/cases/lifecycle_test.rb
@@ -143,12 +143,20 @@ def test_observing_subclasses
assert_equal developer.name, multi_observer.record.name
end
- def test_observing_after_find_when_not_defined_on_the_model
+ def test_after_find_cannot_be_observed_when_its_not_defined_on_the_model
observer = MinimalisticObserver.instance
assert_equal Minimalistic, MinimalisticObserver.observed_class
minimalistic = Minimalistic.find(1)
- assert_equal minimalistic, observer.minimalistic
+ assert_nil observer.minimalistic
+ end
+
+ def test_after_find_can_be_observed_when_its_defined_on_the_model
+ observer = TopicObserver.instance
+ assert_equal Topic, TopicObserver.observed_class
+
+ topic = Topic.find(1)
+ assert_equal topic, observer.topic
end
def test_invalid_observer

0 comments on commit fbef982

Please sign in to comment.