diff options
Diffstat (limited to 'activemodel/lib/active_model/observing.rb')
-rw-r--r-- | activemodel/lib/active_model/observing.rb | 45 |
1 files changed, 36 insertions, 9 deletions
diff --git a/activemodel/lib/active_model/observing.rb b/activemodel/lib/active_model/observing.rb index ef36f80bec..4682ae07ef 100644 --- a/activemodel/lib/active_model/observing.rb +++ b/activemodel/lib/active_model/observing.rb @@ -1,13 +1,20 @@ require 'singleton' +require 'active_model/observer_array' require 'active_support/core_ext/array/wrap' require 'active_support/core_ext/module/aliasing' require 'active_support/core_ext/module/remove_method' require 'active_support/core_ext/string/inflections' +require 'active_support/core_ext/enumerable' +require 'active_support/descendants_tracker' module ActiveModel module Observing extend ActiveSupport::Concern + included do + extend ActiveSupport::DescendantsTracker + end + module ClassMethods # == Active Model Observers Activation # @@ -30,12 +37,16 @@ module ActiveModel # +instantiate_observers+ is called during startup, and before # each development request. def observers=(*values) - @observers = values.flatten + observers.replace(values.flatten) end - # Gets the current observers. + # Gets an array of observers observing this model. + # The array also provides +enable+ and +disable+ methods + # that allow you to selectively enable and disable observers. + # (see <tt>ActiveModel::ObserverArray.enable</tt> and + # <tt>ActiveModel::ObserverArray.disable</tt> for more on this) def observers - @observers ||= [] + @observers ||= ObserverArray.new(self) end # Gets the current observer instances. @@ -43,12 +54,14 @@ module ActiveModel @observer_instances ||= [] end - # Instantiate the global Active Record observers. + # Instantiate the global observers. def instantiate_observers observers.each { |o| instantiate_observer(o) } end # Add a new observer to the pool. + # The new observer needs to respond to 'update', otherwise it + # raises an +ArgumentError+ exception. def add_observer(observer) unless observer.respond_to? :update raise ArgumentError, "observer needs to respond to `update'" @@ -76,7 +89,11 @@ module ActiveModel elsif observer.respond_to?(:instance) observer.instance else - raise ArgumentError, "#{observer} must be a lowercase, underscored class name (or an instance of the class itself) responding to the instance method. Example: Person.observers = :big_brother # calls BigBrother.instance" + raise ArgumentError, + "#{observer} must be a lowercase, underscored class name (or an " + + "instance of the class itself) responding to the instance " + + "method. Example: Person.observers = :big_brother # calls " + + "BigBrother.instance" end end @@ -133,8 +150,8 @@ module ActiveModel # Observers will by default be mapped to the class with which they share a # name. So CommentObserver will be tied to observing Comment, ProductManagerObserver # to ProductManager, and so on. If you want to name your observer differently than - # the class you're interested in observing, you can use the Observer.observe class - # method which takes either the concrete class (Product) or a symbol for that + # the class you're interested in observing, you can use the <tt>Observer.observe</tt> + # class method which takes either the concrete class (Product) or a symbol for that # class (:product): # # class AuditObserver < ActiveModel::Observer @@ -165,6 +182,7 @@ module ActiveModel # class Observer include Singleton + extend ActiveSupport::DescendantsTracker class << self # Attaches the observer to the supplied model classes. @@ -208,9 +226,12 @@ module ActiveModel self.class.observed_classes end - # Send observed_method(object) if the method exists. + # Send observed_method(object) if the method exists and + # the observer is enabled for the given object's class. def update(observed_method, object) #:nodoc: - send(observed_method, object) if respond_to?(observed_method) + return unless respond_to?(observed_method) + return if disabled_for?(object) + send(observed_method, object) end # Special method sent by the observed class when it is inherited. @@ -224,5 +245,11 @@ module ActiveModel def add_observer!(klass) #:nodoc: klass.add_observer(self) end + + def disabled_for?(object) + klass = object.class + return false unless klass.respond_to?(:observers) + klass.observers.disabled_for?(self) + end end end |