Skip to content
This repository

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

Merged
merged 1 commit into from about 2 years ago

3 participants

Marc-André Lafortune Rafael Mendonça França Jeremy Kemper
Marc-André Lafortune

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

Jeremy Kemper
Owner

Thanks @marcandre !

Jeremy Kemper jeremy merged commit 0393c7c into from
Jeremy Kemper jeremy closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Apr 30, 2012
Marc-André Lafortune marcandre Fix Observer by acting on singleton class [#3505]
Also [issue #1034] [pull #6068]
bad44e4
This page is out of date. Refresh to see the latest.
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

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.