From 7c84bbf1607bf4059de04cc4c8ec84df2334574b Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Mon, 20 Jul 2009 23:57:01 -0500 Subject: Add wrap_with_notifications helper to AMo observing --- activemodel/lib/active_model/observing.rb | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) (limited to 'activemodel/lib') 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 -- cgit v1.2.3