Module: ActiveModel::Observing::ClassMethods
- Included in:
- ActiveRecord::Base
- Defined in:
- lib/rails/observers/active_model/observing.rb
Instance Method Summary collapse
-
#add_observer(observer) ⇒ Object
Add a new observer to the pool.
-
#count_observers ⇒ Object
count_observers
is deprecated. -
#instantiate_observers ⇒ Object
Instantiate the global observers.
-
#notify_observers(*args) ⇒ Object
Fires notifications to model’s observers.
-
#observer_instances ⇒ Object
Returns the current observer instances.
-
#observers ⇒ Object
Gets an array of observers observing this model.
-
#observers=(*values) ⇒ Object
Activates the observers assigned.
-
#observers_count ⇒ Object
Returns the total number of instantiated observers.
Instance Method Details
#add_observer(observer) ⇒ Object
Add a new observer to the pool. The new observer needs to respond to update
, otherwise it raises an ArgumentError
exception.
class Foo
include ActiveModel::Observing
end
class FooObserver < ActiveModel::Observer
end
Foo.add_observer(FooObserver.instance)
Foo.observers_instance
# => [#<FooObserver:0x007fccf55d9390>]
125 126 127 128 129 130 |
# File 'lib/rails/observers/active_model/observing.rb', line 125 def add_observer(observer) unless observer.respond_to? :update raise ArgumentError, "observer needs to respond to 'update'" end observer_instances << observer end |
#count_observers ⇒ Object
count_observers
is deprecated. Use #observers_count.
173 174 175 176 177 |
# File 'lib/rails/observers/active_model/observing.rb', line 173 def count_observers msg = "count_observers is deprecated in favor of observers_count" ActiveSupport::Deprecation.warn(msg) observers_count end |
#instantiate_observers ⇒ Object
Instantiate the global observers.
class Foo
include ActiveModel::Observing
attr_accessor :status
end
class FooObserver < ActiveModel::Observer
def on_spec(record, *args)
record.status = true
end
end
Foo.observers = FooObserver
foo = Foo.new
foo.status = false
foo.notify_observers(:on_spec)
foo.status # => false
Foo.instantiate_observers # => [FooObserver]
foo = Foo.new
foo.status = false
foo.notify_observers(:on_spec)
foo.status # => true
107 108 109 |
# File 'lib/rails/observers/active_model/observing.rb', line 107 def instantiate_observers observers.each { |o| instantiate_observer(o) } end |
#notify_observers(*args) ⇒ Object
Fires notifications to model’s observers.
def save
notify_observers(:before_save)
...
notify_observers(:after_save)
end
Custom notifications can be sent in a similar fashion:
notify_observers(:custom_notification, :foo)
This will call custom_notification
, passing as arguments the current object and :foo
.
146 147 148 |
# File 'lib/rails/observers/active_model/observing.rb', line 146 def notify_observers(*args) observer_instances.each { |observer| observer.update(*args) } end |
#observer_instances ⇒ Object
Returns the current observer instances.
class Foo
include ActiveModel::Observing
attr_accessor :status
end
class FooObserver < ActiveModel::Observer
def on_spec(record, *args)
record.status = true
end
end
Foo.observers = FooObserver
Foo.instantiate_observers
Foo.observer_instances # => [#<FooObserver:0x007fc212c40820>]
76 77 78 |
# File 'lib/rails/observers/active_model/observing.rb', line 76 def observer_instances @observer_instances ||= [] end |
#observers ⇒ Object
Gets an array of observers observing this model. The array also provides enable
and disable
methods that allow you to selectively enable and disable observers (see ActiveModel::ObserverArray.enable and ActiveModel::ObserverArray.disable for more on this).
class ORM
include ActiveModel::Observing
end
ORM.observers = :cacher, :garbage_collector
ORM.observers # => [:cacher, :garbage_collector]
ORM.observers.class # => ActiveModel::ObserverArray
54 55 56 |
# File 'lib/rails/observers/active_model/observing.rb', line 54 def observers @observers ||= ObserverArray.new(self) end |
#observers=(*values) ⇒ Object
Activates the observers assigned.
class ORM
include ActiveModel::Observing
end
# Calls PersonObserver.instance
ORM.observers = :person_observer
# Calls Cacher.instance and GarbageCollector.instance
ORM.observers = :cacher, :garbage_collector
# Same as above, just using explicit class references
ORM.observers = Cacher, GarbageCollector
Note: Setting this does not instantiate the observers yet. instantiate_observers
is called during startup, and before each development request.
38 39 40 |
# File 'lib/rails/observers/active_model/observing.rb', line 38 def observers=(*values) observers.replace(values.flatten) end |
#observers_count ⇒ Object
Returns the total number of instantiated observers.
class Foo
include ActiveModel::Observing
attr_accessor :status
end
class FooObserver < ActiveModel::Observer
def on_spec(record, *args)
record.status = true
end
end
Foo.observers = FooObserver
Foo.observers_count # => 0
Foo.instantiate_observers
Foo.observers_count # => 1
168 169 170 |
# File 'lib/rails/observers/active_model/observing.rb', line 168 def observers_count observer_instances.size end |