aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2012-04-30 16:43:22 -0700
committerAaron Patterson <aaron.patterson@gmail.com>2012-04-30 16:43:22 -0700
commit206b43a954dc6c6c0b4b3916cade2561413efdb5 (patch)
tree7bf51668bbe0a13e31052433dfc4838b412b07ca
parentd7d379967a7bc6b2e562d271c673c3e142294224 (diff)
parent24c068d67dab4559ab24f77482cd671563161ec2 (diff)
downloadrails-206b43a954dc6c6c0b4b3916cade2561413efdb5.tar.gz
rails-206b43a954dc6c6c0b4b3916cade2561413efdb5.tar.bz2
rails-206b43a954dc6c6c0b4b3916cade2561413efdb5.zip
Merge pull request #6063 from marcandre/observer_extra_args
Allow extra arguments for Observers
-rw-r--r--activemodel/lib/active_model/observing.rb26
-rw-r--r--activemodel/test/cases/observing_test.rb11
2 files changed, 26 insertions, 11 deletions
diff --git a/activemodel/lib/active_model/observing.rb b/activemodel/lib/active_model/observing.rb
index f3781f7a68..4c7dae42f0 100644
--- a/activemodel/lib/active_model/observing.rb
+++ b/activemodel/lib/active_model/observing.rb
@@ -113,13 +113,21 @@ module ActiveModel
private
# Fires notifications to model's observers
#
- # def save
- # notify_observers(:before_save)
- # ...
- # notify_observers(:after_save)
- # end
- def notify_observers(method)
- self.class.notify_observers(method, self)
+ # def save
+ # notify_observers(:before_save)
+ # ...
+ # notify_observers(:after_save)
+ # end
+ #
+ # Custom notifications can be sent in a similar fashion:
+ #
+ # notify_observers(:custom_notification, :foo)
+ #
+ # This will call +custom_notification+, passing as arguments
+ # the current object and :foo.
+ #
+ def notify_observers(method, *extra_args)
+ self.class.notify_observers(method, self, *extra_args)
end
end
@@ -230,10 +238,10 @@ module ActiveModel
# Send observed_method(object) if the method exists and
# the observer is enabled for the given object's class.
- def update(observed_method, object, &block) #:nodoc:
+ def update(observed_method, object, *extra_args, &block) #:nodoc:
return unless respond_to?(observed_method)
return if disabled_for?(object)
- send(observed_method, object, &block)
+ send(observed_method, object, *extra_args, &block)
end
# Special method sent by the observed class when it is inherited.
diff --git a/activemodel/test/cases/observing_test.rb b/activemodel/test/cases/observing_test.rb
index 9891be8d77..c91938a7ee 100644
--- a/activemodel/test/cases/observing_test.rb
+++ b/activemodel/test/cases/observing_test.rb
@@ -14,8 +14,8 @@ class FooObserver < ActiveModel::Observer
attr_accessor :stub
- def on_spec(record)
- stub.event_with(record) if stub
+ def on_spec(record, *args)
+ stub.event_with(record, *args) if stub
end
def around_save(record)
@@ -141,6 +141,13 @@ class ObserverTest < ActiveModel::TestCase
Foo.send(:notify_observers, :on_spec, foo)
end
+ test "passes extra arguments" do
+ foo = Foo.new
+ FooObserver.instance.stub = stub
+ FooObserver.instance.stub.expects(:event_with).with(foo, :bar)
+ Foo.send(:notify_observers, :on_spec, foo, :bar)
+ end
+
test "skips nonexistent observer event" do
foo = Foo.new
Foo.send(:notify_observers, :whatever, foo)