Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Fix Observer by acting on singleton class [#3505] #6071

Merged
merged 1 commit into from

3 participants

@marcandre

Also addresses [issue #1034] [pull #6068]

@jeremy
Owner

Thanks @marcandre !

@jeremy jeremy merged commit 0393c7c into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 30, 2012
  1. @marcandre

    Fix Observer by acting on singleton class [#3505]

    marcandre authored
    Also [issue #1034] [pull #6068]
This page is out of date. Refresh to see the latest.
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
Something went wrong with that request. Please try again.