Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fix Observer by acting on singleton class [#3505]

Also [issue #1034] [pull #6068]
  • Loading branch information...
commit bad44e4f8f690039bd0db92ac25f10af536c6e71 1 parent ad2c5ea
@marcandre marcandre authored
View
2  activemodel/lib/active_model/observing.rb
@@ -194,7 +194,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
@@ -80,13 +80,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
@@ -145,4 +145,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.