Module: ActiveRecord::Observing::ClassMethods

Defined in:
lib/active_record/observer.rb

Instance Method Summary collapse

Instance Method Details

#instantiate_observersObject

Instantiate the global ActiveRecord observers



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/active_record/observer.rb', line 34

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

#observersObject

Gets the current observers.



29
30
31
# File 'lib/active_record/observer.rb', line 29

def observers
  @observers ||= []
end

#observers=(*observers) ⇒ Object

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.



24
25
26
# File 'lib/active_record/observer.rb', line 24

def observers=(*observers)
  @observers = observers.flatten
end