Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #6071 from marcandre/observer_redef

Fix Observer by acting on singleton class. Fixes #3505.
  • Loading branch information...
commit 0393c7cd51031fdb4b1abc3ed97d0c178a817c3c 2 parents f975a86 + bad44e4
@jeremy jeremy authored
View
2  activemodel/lib/active_model/observing.rb
@@ -195,7 +195,7 @@ class << self
def observe(*models)
models.flatten!
models.collect! { |model| model.respond_to?(:to_sym) ? model.to_s.camelize.constantize : model }
- redefine_method(:observed_classes) { models }
+ singleton_class.redefine_method(:observed_classes) { models }
end
# Returns an array of Classes to observe.
View
15 activemodel/test/cases/observing_test.rb
@@ -95,13 +95,13 @@ def setup
class ObserverTest < ActiveModel::TestCase
def setup
ObservedModel.observers = :foo_observer
- FooObserver.instance_eval do
+ FooObserver.singleton_class.instance_eval do
alias_method :original_observed_classes, :observed_classes
end
end
def teardown
- FooObserver.instance_eval do
+ FooObserver.singleton_class.instance_eval do
undef_method :observed_classes
alias_method :observed_classes, :original_observed_classes
end
@@ -160,4 +160,15 @@ def teardown
end
assert_equal :in_around_save, yielded_value
end
+
+ test "observe redefines observed_classes class method" do
+ class BarObserver < ActiveModel::Observer
+ observe :foo
+ end
+
+ assert_equal [Foo], BarObserver.observed_classes
+
+ BarObserver.observe(ObservedModel)
+ assert_equal [ObservedModel], BarObserver.observed_classes
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.