2011-04-27 14:07:42 -04:00
|
|
|
require 'set'
|
|
|
|
|
|
|
|
module ActiveModel
|
|
|
|
# Stores the enabled/disabled state of individual observers for
|
2011-05-06 12:59:57 -04:00
|
|
|
# a particular model class.
|
2011-04-27 14:07:42 -04:00
|
|
|
class ObserverArray < Array
|
|
|
|
attr_reader :model_class
|
|
|
|
def initialize(model_class, *args)
|
|
|
|
@model_class = model_class
|
|
|
|
super(*args)
|
|
|
|
end
|
|
|
|
|
2011-05-06 12:59:57 -04:00
|
|
|
# Returns true if the given observer is disabled for the model class.
|
2011-04-28 11:27:15 -04:00
|
|
|
def disabled_for?(observer)
|
|
|
|
disabled_observers.include?(observer.class)
|
|
|
|
end
|
|
|
|
|
2011-05-23 19:39:10 -04:00
|
|
|
# Disables one or more observers. This supports multiple forms:
|
2011-05-06 12:59:57 -04:00
|
|
|
#
|
|
|
|
# ORM.observers.disable :user_observer
|
|
|
|
# # => disables the UserObserver
|
|
|
|
#
|
|
|
|
# User.observers.disable AuditTrail
|
|
|
|
# # => disables the AuditTrail observer for User notifications.
|
|
|
|
# # Other models will still notify the AuditTrail observer.
|
|
|
|
#
|
|
|
|
# ORM.observers.disable :observer_1, :observer_2
|
|
|
|
# # => disables Observer1 and Observer2 for all models.
|
|
|
|
#
|
|
|
|
# ORM.observers.disable :all
|
|
|
|
# # => disables all observers for all models.
|
|
|
|
#
|
|
|
|
# User.observers.disable :all do
|
|
|
|
# # all user observers are disabled for
|
|
|
|
# # just the duration of the block
|
|
|
|
# end
|
2011-04-27 14:07:42 -04:00
|
|
|
def disable(*observers, &block)
|
|
|
|
set_enablement(false, observers, &block)
|
|
|
|
end
|
|
|
|
|
2011-05-23 19:39:10 -04:00
|
|
|
# Enables one or more observers. This supports multiple forms:
|
2011-05-06 12:59:57 -04:00
|
|
|
#
|
|
|
|
# ORM.observers.enable :user_observer
|
|
|
|
# # => enables the UserObserver
|
|
|
|
#
|
|
|
|
# User.observers.enable AuditTrail
|
|
|
|
# # => enables the AuditTrail observer for User notifications.
|
|
|
|
# # Other models will not be affected (i.e. they will not
|
|
|
|
# # trigger notifications to AuditTrail if previously disabled)
|
|
|
|
#
|
|
|
|
# ORM.observers.enable :observer_1, :observer_2
|
|
|
|
# # => enables Observer1 and Observer2 for all models.
|
|
|
|
#
|
|
|
|
# ORM.observers.enable :all
|
|
|
|
# # => enables all observers for all models.
|
|
|
|
#
|
|
|
|
# User.observers.enable :all do
|
|
|
|
# # all user observers are enabled for
|
|
|
|
# # just the duration of the block
|
|
|
|
# end
|
|
|
|
#
|
2011-05-23 19:39:10 -04:00
|
|
|
# Note: all observers are enabled by default. This method is only
|
2011-05-06 12:59:57 -04:00
|
|
|
# useful when you have previously disabled one or more observers.
|
2011-04-27 14:07:42 -04:00
|
|
|
def enable(*observers, &block)
|
|
|
|
set_enablement(true, observers, &block)
|
|
|
|
end
|
|
|
|
|
2011-04-28 10:58:58 -04:00
|
|
|
protected
|
2011-04-27 14:07:42 -04:00
|
|
|
|
2011-04-28 11:27:15 -04:00
|
|
|
def disabled_observers
|
|
|
|
@disabled_observers ||= Set.new
|
|
|
|
end
|
|
|
|
|
2011-04-27 14:07:42 -04:00
|
|
|
def observer_class_for(observer)
|
|
|
|
return observer if observer.is_a?(Class)
|
|
|
|
|
|
|
|
if observer.respond_to?(:to_sym) # string/symbol
|
|
|
|
observer.to_s.camelize.constantize
|
|
|
|
else
|
|
|
|
raise ArgumentError, "#{observer} was not a class or a " +
|
|
|
|
"lowercase, underscored class name as expected."
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-04-28 10:58:58 -04:00
|
|
|
def start_transaction
|
|
|
|
disabled_observer_stack.push(disabled_observers.dup)
|
|
|
|
each_subclass_array do |array|
|
|
|
|
array.start_transaction
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def disabled_observer_stack
|
|
|
|
@disabled_observer_stack ||= []
|
|
|
|
end
|
|
|
|
|
|
|
|
def end_transaction
|
|
|
|
@disabled_observers = disabled_observer_stack.pop
|
|
|
|
each_subclass_array do |array|
|
|
|
|
array.end_transaction
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-04-27 14:07:42 -04:00
|
|
|
def transaction
|
2011-04-28 10:58:58 -04:00
|
|
|
start_transaction
|
2011-04-27 14:07:42 -04:00
|
|
|
|
|
|
|
begin
|
|
|
|
yield
|
|
|
|
ensure
|
2011-04-28 10:58:58 -04:00
|
|
|
end_transaction
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def each_subclass_array
|
2011-04-28 11:07:08 -04:00
|
|
|
model_class.descendants.each do |subclass|
|
|
|
|
yield subclass.observers
|
2011-04-27 14:07:42 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_enablement(enabled, observers)
|
|
|
|
if block_given?
|
|
|
|
transaction do
|
|
|
|
set_enablement(enabled, observers)
|
|
|
|
yield
|
|
|
|
end
|
|
|
|
else
|
2011-04-28 11:07:08 -04:00
|
|
|
observers = ActiveModel::Observer.descendants if observers == [:all]
|
2011-04-27 14:07:42 -04:00
|
|
|
observers.each do |obs|
|
|
|
|
klass = observer_class_for(obs)
|
|
|
|
|
|
|
|
unless klass < ActiveModel::Observer
|
|
|
|
raise ArgumentError.new("#{obs} does not refer to a valid observer")
|
|
|
|
end
|
|
|
|
|
|
|
|
if enabled
|
|
|
|
disabled_observers.delete(klass)
|
|
|
|
else
|
|
|
|
disabled_observers << klass
|
|
|
|
end
|
|
|
|
end
|
2011-04-28 10:58:58 -04:00
|
|
|
|
|
|
|
each_subclass_array do |array|
|
|
|
|
array.set_enablement(enabled, observers)
|
|
|
|
end
|
2011-04-27 14:07:42 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|