From 4d703592001eef1f7e670660cd701e32105bd521 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Wed, 10 Jun 2009 23:35:34 -0500 Subject: Integrate ActiveModel::Observing into ActiveRecord --- activerecord/lib/active_record/base.rb | 2 +- activerecord/lib/active_record/observer.rb | 95 ++---------------------------- 2 files changed, 5 insertions(+), 92 deletions(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 98898e9c18..c4d143ab05 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -3149,7 +3149,7 @@ module ActiveRecord #:nodoc: include Locking::Optimistic, Locking::Pessimistic include AttributeMethods include Dirty - include Callbacks, Observing, Timestamp + include Callbacks, ActiveModel::Observing, Timestamp include Associations, AssociationPreload, NamedScope # AutosaveAssociation needs to be included before Transactions, because we want diff --git a/activerecord/lib/active_record/observer.rb b/activerecord/lib/active_record/observer.rb index 89ec0962bf..a34ff4a47a 100644 --- a/activerecord/lib/active_record/observer.rb +++ b/activerecord/lib/active_record/observer.rb @@ -2,56 +2,6 @@ require 'singleton' require 'set' module ActiveRecord - module Observing # :nodoc: - extend ActiveSupport::Concern - - module ClassMethods - # Activates the observers assigned. Examples: - # - # # Calls PersonObserver.instance - # ActiveRecord::Base.observers = :person_observer - # - # # Calls Cacher.instance and GarbageCollector.instance - # ActiveRecord::Base.observers = :cacher, :garbage_collector - # - # # Same as above, just using explicit class references - # ActiveRecord::Base.observers = Cacher, GarbageCollector - # - # Note: Setting this does not instantiate the observers yet. +instantiate_observers+ is - # called during startup, and before each development request. - def observers=(*observers) - @observers = observers.flatten - end - - # Gets the current observers. - def observers - @observers ||= [] - end - - # Instantiate the global Active Record observers. - def instantiate_observers - return if @observers.blank? - @observers.each do |observer| - if observer.respond_to?(:to_sym) # Symbol or String - observer.to_s.camelize.constantize.instance - 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" - end - end - end - - protected - # Notify observers when the observed class is subclassed. - def inherited(subclass) - super - changed - notify_observers :observed_class_inherited, subclass - end - end - end - # Observer classes respond to lifecycle callbacks to implement trigger-like # behavior outside the original class. This is a great way to reduce the # clutter that normally comes when the model class is burdened with @@ -137,56 +87,19 @@ module ActiveRecord # load their observers by calling ModelObserver.instance before. Observers are # singletons and that call instantiates and registers them. # - class Observer - include Singleton - - class << self - # Attaches the observer to the supplied model classes. - def observe(*models) - models.flatten! - models.collect! { |model| model.is_a?(Symbol) ? model.to_s.camelize.constantize : model } - define_method(:observed_classes) { Set.new(models) } - end - - # The class observed by default is inferred from the observer's class name: - # assert_equal Person, PersonObserver.observed_class - def observed_class - if observed_class_name = name[/(.*)Observer/, 1] - observed_class_name.constantize - else - nil - end - end - end - - # Start observing the declared classes and their subclasses. + class Observer < ActiveModel::Observer def initialize - Set.new(observed_classes + observed_subclasses).each { |klass| add_observer! klass } - end - - # Send observed_method(object) if the method exists. - def update(observed_method, object) #:nodoc: - send(observed_method, object) if respond_to?(observed_method) - end - - # Special method sent by the observed class when it is inherited. - # Passes the new subclass. - def observed_class_inherited(subclass) #:nodoc: - self.class.observe(observed_classes + [subclass]) - add_observer!(subclass) + super + observed_subclasses.each { |klass| add_observer!(klass) } end protected - def observed_classes - Set.new([self.class.observed_class].compact.flatten) - end - def observed_subclasses observed_classes.sum([]) { |klass| klass.send(:subclasses) } end def add_observer!(klass) - klass.add_observer(self) + super if respond_to?(:after_find) && !klass.method_defined?(:after_find) klass.class_eval 'def after_find() end' end -- cgit v1.2.3