mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Fixing documentation for ActiveModel::Observer
This commit is contained in:
parent
fbc7c2beca
commit
e6063282f9
2 changed files with 21 additions and 8 deletions
|
@ -134,3 +134,11 @@ functionality from the following modules:
|
|||
|
||||
{Learn more}[link:classes/ActiveModel/Naming.html]
|
||||
|
||||
* Adding observer support to your objects
|
||||
|
||||
ActiveModel::Observers allows your object to implement the Observer
|
||||
pattern in a Rails App and take advantage of all the standard observer
|
||||
functions.
|
||||
|
||||
{Learn more}[link:classes/ActiveModel/Observer.html]
|
||||
|
|
@ -24,8 +24,9 @@ module ActiveModel
|
|||
# # 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.
|
||||
# Note: Setting this does not instantiate the observers yet.
|
||||
# +instantiate_observers+ is called during startup, and before
|
||||
# each development request.
|
||||
def observers=(*values)
|
||||
@observers = values.flatten
|
||||
end
|
||||
|
@ -102,10 +103,12 @@ module ActiveModel
|
|||
#
|
||||
# == Observing a class that can't be inferred
|
||||
#
|
||||
# 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 class (:product):
|
||||
# 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
|
||||
# class (:product):
|
||||
#
|
||||
# class AuditObserver < ActiveModel::Observer
|
||||
# observe :account
|
||||
|
@ -115,7 +118,8 @@ module ActiveModel
|
|||
# end
|
||||
# end
|
||||
#
|
||||
# If the audit observer needs to watch more than one kind of object, this can be specified with multiple arguments:
|
||||
# If the audit observer needs to watch more than one kind of object, this can be
|
||||
# specified with multiple arguments:
|
||||
#
|
||||
# class AuditObserver < ActiveModel::Observer
|
||||
# observe :account, :balance
|
||||
|
@ -125,7 +129,8 @@ module ActiveModel
|
|||
# end
|
||||
# end
|
||||
#
|
||||
# The AuditObserver will now act on both updates to Account and Balance by treating them both as records.
|
||||
# The AuditObserver will now act on both updates to Account and Balance by treating
|
||||
# them both as records.
|
||||
#
|
||||
class Observer
|
||||
include Singleton
|
||||
|
|
Loading…
Reference in a new issue