Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow extra arguments for Observers #6063

Merged
merged 1 commit into from Apr 30, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 17 additions & 9 deletions activemodel/lib/active_model/observing.rb
Expand Up @@ -112,13 +112,21 @@ def inherited(subclass)
private private
# Fires notifications to model's observers # Fires notifications to model's observers
# #
# def save # def save
# notify_observers(:before_save) # notify_observers(:before_save)
# ... # ...
# notify_observers(:after_save) # notify_observers(:after_save)
# end # end
def notify_observers(method) #
self.class.notify_observers(method, self) # 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
end end


Expand Down Expand Up @@ -229,10 +237,10 @@ def observed_classes #:nodoc:


# Send observed_method(object) if the method exists and # Send observed_method(object) if the method exists and
# the observer is enabled for the given object's class. # 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 unless respond_to?(observed_method)
return if disabled_for?(object) return if disabled_for?(object)
send(observed_method, object, &block) send(observed_method, object, *extra_args, &block)
end end


# Special method sent by the observed class when it is inherited. # Special method sent by the observed class when it is inherited.
Expand Down
11 changes: 9 additions & 2 deletions activemodel/test/cases/observing_test.rb
Expand Up @@ -14,8 +14,8 @@ class << self


attr_accessor :stub attr_accessor :stub


def on_spec(record) def on_spec(record, *args)
stub.event_with(record) if stub stub.event_with(record, *args) if stub
end end


def around_save(record) def around_save(record)
Expand Down Expand Up @@ -133,6 +133,13 @@ def teardown
Foo.send(:notify_observers, :on_spec, foo) Foo.send(:notify_observers, :on_spec, foo)
end 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 test "skips nonexistent observer event" do
foo = Foo.new foo = Foo.new
Foo.send(:notify_observers, :whatever, foo) Foo.send(:notify_observers, :whatever, foo)
Expand Down