Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add wrap_with_notifications helper to AMo observing
  • Loading branch information
josh committed Jul 21, 2009
1 parent 2685d93 commit 7c84bbf
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 13 deletions.
22 changes: 20 additions & 2 deletions activemodel/lib/active_model/observing.rb
@@ -1,7 +1,8 @@
require 'observer'
require 'singleton'
require 'active_support/core_ext/string/inflections'
require 'active_support/core_ext/array/wrap'
require 'active_support/core_ext/module/aliasing'
require 'active_support/core_ext/string/inflections'

module ActiveModel
module Observing
Expand Down Expand Up @@ -39,6 +40,23 @@ def instantiate_observers
observers.each { |o| instantiate_observer(o) }
end

# Wraps methods with before and after notifications.
#
# wrap_with_notifications :create, :save, :update, :destroy
def wrap_with_notifications(*methods)
methods.each do |method|
class_eval(<<-EOS, __FILE__, __LINE__ + 1)
def #{method}_with_notifications(*args, &block)
notify_observers(:before_#{method})
result = #{method}_without_notifications(*args, &block)
notify_observers(:after_#{method})
result
end
EOS
alias_method_chain(method, :notifications)
end
end

protected
def instantiate_observer(observer)
# string/symbol
Expand All @@ -60,7 +78,7 @@ def inherited(subclass)
end

private
def notify(method) #:nodoc:
def notify_observers(method)
self.class.changed
self.class.notify_observers(method, self)
end
Expand Down
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/callbacks.rb
Expand Up @@ -349,7 +349,7 @@ def callback(method)
result = send(method)
end

notify(method)
notify_observers(method)

return result
end
Expand Down
1 change: 1 addition & 0 deletions activeresource/lib/active_resource.rb
Expand Up @@ -34,6 +34,7 @@ module ActiveResource
autoload :Connection, 'active_resource/connection'
autoload :CustomMethods, 'active_resource/custom_methods'
autoload :Formats, 'active_resource/formats'
autoload :Observing, 'active_resource/observing'
autoload :Validations, 'active_resource/validations'
autoload :HttpMock, 'active_resource/http_mock'
end
13 changes: 3 additions & 10 deletions activeresource/lib/active_resource/base.rb
Expand Up @@ -804,8 +804,7 @@ def dup
# my_company.size = 10
# my_company.save # sends PUT /companies/1 (update)
def save
notify(:before_save)
(new? ? create : update).tap { notify(:after_save) }
new? ? create : update
end

# Deletes the resource from the remote service.
Expand All @@ -821,8 +820,7 @@ def save
# new_person.destroy
# Person.find(new_id) # 404 (Resource Not Found)
def destroy
notify(:before_destroy)
connection.delete(element_path, self.class.headers).tap { notify(:after_destroy) }
connection.delete(element_path, self.class.headers)
end

# Evaluates to <tt>true</tt> if this resource is not <tt>new?</tt> and is
Expand Down Expand Up @@ -997,20 +995,16 @@ def connection(refresh = false)

# Update the resource on the remote service.
def update
notify(:before_update)
connection.put(element_path(prefix_options), encode, self.class.headers).tap do |response|
load_attributes_from_response(response)
notify(:after_update)
end
end

# Create (i.e., \save to the remote service) the \new resource.
def create
notify(:before_create)
connection.post(collection_path, encode, self.class.headers).tap do |response|
self.id = id_from_response(response)
load_attributes_from_response(response)
notify(:after_create)
end
end

Expand Down Expand Up @@ -1093,7 +1087,6 @@ def method_missing(method_symbol, *arguments) #:nodoc:

class Base
extend ActiveModel::Naming
include CustomMethods, Validations
include ActiveModel::Observing
include CustomMethods, Observing, Validations
end
end
10 changes: 10 additions & 0 deletions activeresource/lib/active_resource/observing.rb
@@ -0,0 +1,10 @@
module ActiveResource
module Observing
extend ActiveSupport::Concern
include ActiveModel::Observing

included do
wrap_with_notifications :create, :save, :update, :destroy
end
end
end

0 comments on commit 7c84bbf

Please sign in to comment.