Skip to content
This repository
Browse code

Fix Observer by acting on singleton class [#3505]

Also [issue #1034] [pull #6068]
  • Loading branch information...
commit bad44e4f8f690039bd0db92ac25f10af536c6e71 1 parent ad2c5ea
Marc-André Lafortune marcandre authored
2  activemodel/lib/active_model/observing.rb
@@ -194,7 +194,7 @@ class << self
194 194 def observe(*models)
195 195 models.flatten!
196 196 models.collect! { |model| model.respond_to?(:to_sym) ? model.to_s.camelize.constantize : model }
197   - redefine_method(:observed_classes) { models }
  197 + singleton_class.redefine_method(:observed_classes) { models }
198 198 end
199 199
200 200 # Returns an array of Classes to observe.
15 activemodel/test/cases/observing_test.rb
@@ -80,13 +80,13 @@ def setup
80 80 class ObserverTest < ActiveModel::TestCase
81 81 def setup
82 82 ObservedModel.observers = :foo_observer
83   - FooObserver.instance_eval do
  83 + FooObserver.singleton_class.instance_eval do
84 84 alias_method :original_observed_classes, :observed_classes
85 85 end
86 86 end
87 87
88 88 def teardown
89   - FooObserver.instance_eval do
  89 + FooObserver.singleton_class.instance_eval do
90 90 undef_method :observed_classes
91 91 alias_method :observed_classes, :original_observed_classes
92 92 end
@@ -145,4 +145,15 @@ def teardown
145 145 end
146 146 assert_equal :in_around_save, yielded_value
147 147 end
  148 +
  149 + test "observe redefines observed_classes class method" do
  150 + class BarObserver < ActiveModel::Observer
  151 + observe :foo
  152 + end
  153 +
  154 + assert_equal [Foo], BarObserver.observed_classes
  155 +
  156 + BarObserver.observe(ObservedModel)
  157 + assert_equal [ObservedModel], BarObserver.observed_classes
  158 + end
148 159 end

0 comments on commit bad44e4

Please sign in to comment.
Something went wrong with that request. Please try again.