public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Make observers define #after_find in the model only if needed.

[#676 state:resolved]
Signed-off-by: Michael Koziarski <michael@koziarski.com>
George Ogata (author)
Tue Jul 22 13:38:26 -0700 2008
NZKoz (committer)
Sat Jul 26 07:26:08 -0700 2008
commit  e8fc894f66daa7909d1790f2cd145844d256d282
tree    d3d694cd8d8aa861b34037e282e9aec4184506e3
parent  f48b9ab5c2741ddbdbc0a9f4cd06875a1e3c8b02
...
189
190
191
 
 
 
192
193
194
...
189
190
191
192
193
194
195
196
197
0
@@ -189,6 +189,9 @@ module ActiveRecord
0
 
0
       def add_observer!(klass)
0
         klass.add_observer(self)
0
+        if respond_to?(:after_find) && !klass.method_defined?(:after_find)
0
+          klass.class_eval 'def after_find() end'
0
+        end
0
       end
0
   end
0
 end
...
143
144
145
146
 
147
148
149
150
151
 
152
153
154
...
159
160
161
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
162
163
164
...
143
144
145
 
146
147
148
149
150
 
151
152
153
154
...
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
0
@@ -143,12 +143,12 @@ class LifecycleTest < ActiveRecord::TestCase
0
     assert_equal developer.name, multi_observer.record.name
0
   end
0
 
0
-  def test_after_find_cannot_be_observed_when_its_not_defined_on_the_model
0
+  def test_after_find_can_be_observed_when_its_not_defined_on_the_model
0
     observer = MinimalisticObserver.instance
0
     assert_equal Minimalistic, MinimalisticObserver.observed_class
0
 
0
     minimalistic = Minimalistic.find(1)
0
-    assert_nil observer.minimalistic
0
+    assert_equal minimalistic, observer.minimalistic
0
   end
0
 
0
   def test_after_find_can_be_observed_when_its_defined_on_the_model
0
@@ -159,6 +159,34 @@ class LifecycleTest < ActiveRecord::TestCase
0
     assert_equal topic, observer.topic
0
   end
0
 
0
+  def test_after_find_is_not_created_if_its_not_used
0
+    # use a fresh class so an observer can't have defined an
0
+    # after_find on it
0
+    model_class = Class.new(ActiveRecord::Base)
0
+    observer_class = Class.new(ActiveRecord::Observer)
0
+    observer_class.observe(model_class)
0
+
0
+    observer = observer_class.instance
0
+
0
+    assert !model_class.method_defined?(:after_find)
0
+  end
0
+
0
+  def test_after_find_is_not_clobbered_if_it_already_exists
0
+    # use a fresh observer class so we can instantiate it (Observer is
0
+    # a Singleton)
0
+    model_class = Class.new(ActiveRecord::Base) do
0
+      def after_find; end
0
+    end
0
+    original_method = model_class.instance_method(:after_find)
0
+    observer_class = Class.new(ActiveRecord::Observer) do
0
+      def after_find; end
0
+    end
0
+    observer_class.observe(model_class)
0
+
0
+    observer = observer_class.instance
0
+    assert_equal original_method, model_class.instance_method(:after_find)
0
+  end
0
+
0
   def test_invalid_observer
0
     assert_raise(ArgumentError) { Topic.observers = Object.new; Topic.instantiate_observers }
0
   end

Comments