class ActiveModel::Observer
them both as records.
The AuditObserver will now act on both updates to Account and Balance by treating
end
end
AuditTrail.new(record, “UPDATED”)
def after_update(record)
observe :account, :balance
class AuditObserver < ActiveModel::Observer
specified with multiple arguments:
If the audit observer needs to watch more than one kind of object, this can be
end
end
AuditTrail.new(account, “UPDATED”)
def after_update(account)
observe :account
class AuditObserver < ActiveModel::Observer
class (:product):
method which takes either the concrete class (Product) or a symbol for that
the class you’re interested in observing, you can use the Observer.observe class
to ProductManager, and so on. If you want to name your observer differently than
name. So CommentObserver will be tied to observing Comment, ProductManagerObserver
Observers will by default be mapped to the class with which they share a
== 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 < ActiveModel::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 < ActiveModel::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 life cycle callbacks to implement trigger-like
== Active Model Observers
def add_observer!(klass) #:nodoc:
def add_observer!(klass) #:nodoc: klass.add_observer(self) end
def initialize
def initialize observed_classes.each { |klass| add_observer!(klass) } end
def observe(*models)
def observe(*models) models.flatten! models.collect! { |model| model.respond_to?(:to_sym) ? model.to_s.camelize.constantize : model } remove_possible_method(:observed_classes) define_method(:observed_classes) { 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
end
[Account, Balance]
def self.observed_classes
class AuditObserver < ActiveModel::Observer
You can override this instead of using the +observe+ helper.
Returns an array of Classes to observe.
def observed_classes Array.wrap(observed_class) end
def observed_classes #:nodoc:
def observed_classes #:nodoc: self.class.observed_classes 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