aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel
diff options
context:
space:
mode:
authorJoshua Peek <josh@joshpeek.com>2009-07-20 23:57:01 -0500
committerJoshua Peek <josh@joshpeek.com>2009-07-20 23:57:01 -0500
commit7c84bbf1607bf4059de04cc4c8ec84df2334574b (patch)
tree438e601981282ccdd8ed25424411eba6476d0d26 /activemodel
parent2685d93b0728b647b6f49f3e1802c779d5fb9867 (diff)
downloadrails-7c84bbf1607bf4059de04cc4c8ec84df2334574b.tar.gz
rails-7c84bbf1607bf4059de04cc4c8ec84df2334574b.tar.bz2
rails-7c84bbf1607bf4059de04cc4c8ec84df2334574b.zip
Add wrap_with_notifications helper to AMo observing
Diffstat (limited to 'activemodel')
-rw-r--r--activemodel/lib/active_model/observing.rb22
1 files changed, 20 insertions, 2 deletions
diff --git a/activemodel/lib/active_model/observing.rb b/activemodel/lib/active_model/observing.rb
index 7bad2397ae..707b1a0da6 100644
--- a/activemodel/lib/active_model/observing.rb
+++ b/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
@@ -39,6 +40,23 @@ module ActiveModel
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
@@ -60,7 +78,7 @@ module ActiveModel
end
private
- def notify(method) #:nodoc:
+ def notify_observers(method)
self.class.changed
self.class.notify_observers(method, self)
end