class ActiveRecord::Observer
singletons and that call instantiates and registers them.
load their observers by calling ModelObserver.instance
before. Observers are
If by any chance you are using observed models in the initialization you can still
so observed models can make use of extensions.
application initializers. Now observers are loaded after application initializers,
Up to (and including) Rails 2.0.2 observers were instantiated between plugins and
corresponding model class is loaded.
notifies them of events when they occur. As a side-effect, when an observer is loaded its
Observers register themselves in the model class they observe, since it is the class that
== Loading
Observers will not be invoked unless you define these in your application configuration.
config.active_record.observers = :comment_observer, :signup_observerconfig/environment.rb
file.
In order to activate an observer, list it in the config.active_record.observers
configuration setting in your
== Configuration
naming convention of app/models/audit_observer.rb.
If you’re using Active Record within Rails, observer classes are usually stored in app/models with the
== Storing Observers in Rails
The observer can implement callback methods for each of the methods described in the Callbacks module.
== Available callback methods
The AuditObserver will now act on both updates to Account and Balance by treating them both as records.
end
end
AuditTrail.new(record, “UPDATED”)
def after_update(record)
observe :account, :balance
class AuditObserver < ActiveRecord::Observer
If the audit observer needs to watch more than one kind of object, this can be specified with multiple arguments:
end
end
AuditTrail.new(account, “UPDATED”)
def after_update(account)
observe :account
class AuditObserver < ActiveRecord::Observer
either the concrete class (Product) or a symbol for that class (:product):
differently than the class you’re interested in observing, you can use the Observer.observe class method which takes
be tied to observing Comment, ProductManagerObserver to ProductManager, and so on. If you want to name your observer
Observers will by default be mapped to the class with which they share a name. So CommentObserver will
== Observing a class that can’t be inferred
This Observer uses logger to log when specific callbacks are triggered.
end
end
contact.logger.warn(“Contact with an id of #{contact.id} was destroyed!”)
def after_destroy(contact)
end
contact.logger.info(‘New contact added!’)
def after_create(contact)
class ContactObserver < ActiveRecord::Observer
This Observer sends an email when a Comment#save is finished.
end
end
Notifications.deliver_comment(“admin@do.com”, “New comment was posted”, comment)
def after_save(comment)
class CommentObserver < ActiveRecord::Observer
class. Example:
functionality that doesn’t pertain to the core responsibility of the
clutter that normally comes when the model class is burdened with
behavior outside the original class. This is a great way to reduce the
Observer classes respond to lifecycle callbacks to implement trigger-like
def add_observer!(klass)
def add_observer!(klass) klass.add_observer(self) if respond_to?(:after_find) && !klass.method_defined?(:after_find) klass.class_eval 'def after_find() end' end end
def initialize
def initialize Set.new(observed_classes + observed_subclasses).each { |klass| add_observer! klass } end
def observe(*models)
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
def observed_class
The class observed by default is inferred from the observer's class name:
def observed_class if observed_class_name = name[/(.*)Observer/, 1] observed_class_name.constantize else nil end end
def observed_class_inherited(subclass) #:nodoc:
Passes the new subclass.
Special method sent by the observed class when it is inherited.
def observed_class_inherited(subclass) #:nodoc: self.class.observe(observed_classes + [subclass]) add_observer!(subclass) end
def observed_classes
def observed_classes Set.new([self.class.observed_class].compact.flatten) end
def observed_subclasses
def observed_subclasses observed_classes.sum([]) { |klass| klass.send(:subclasses) } end
def update(observed_method, object) #:nodoc:
Send observed_method(object) if the method exists.
def update(observed_method, object) #:nodoc: send(observed_method, object) if respond_to?(observed_method) end