Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 153 lines (134 sloc) 4.622 kB
1f8cc44 @myronmarston Allow observers to be enabled and disabled.
myronmarston authored
1 require 'set'
2
3 module ActiveModel
4 # Stores the enabled/disabled state of individual observers for
99363dc @myronmarston Add documentation for new observer enable/disable feature.
myronmarston authored
5 # a particular model class.
1f8cc44 @myronmarston Allow observers to be enabled and disabled.
myronmarston authored
6 class ObserverArray < Array
7 attr_reader :model_class
ab53cc7 update ActiveModel::ObserverArray documentation [ci skip]
Francesco Rodriguez authored
8 def initialize(model_class, *args) #:nodoc:
1f8cc44 @myronmarston Allow observers to be enabled and disabled.
myronmarston authored
9 @model_class = model_class
10 super(*args)
11 end
12
ab53cc7 update ActiveModel::ObserverArray documentation [ci skip]
Francesco Rodriguez authored
13 # Returns +true+ if the given observer is disabled for the model class,
14 # +false+ otherwise.
15 def disabled_for?(observer) #:nodoc:
fef2215 @myronmarston Fix bug with AM::Observer disablement.
myronmarston authored
16 disabled_observers.include?(observer.class)
17 end
18
2a4b780 @smartinez87 Remove extra white spaces on ActiveModel docs.
smartinez87 authored
19 # Disables one or more observers. This supports multiple forms:
99363dc @myronmarston Add documentation for new observer enable/disable feature.
myronmarston authored
20 #
f202088 @robbkidd Make observer enable/disable documentation more informative.
robbkidd authored
21 # ORM.observers.disable :all
b7b97fa add :nodoc: directive to AM::ObserverArray internal methods [ci skip]
Francesco Rodriguez authored
22 # # => disables all observers for all models subclassed from
f202088 @robbkidd Make observer enable/disable documentation more informative.
robbkidd authored
23 # # an ORM base class that includes ActiveModel::Observing
24 # # e.g. ActiveRecord::Base
25 #
99363dc @myronmarston Add documentation for new observer enable/disable feature.
myronmarston authored
26 # ORM.observers.disable :user_observer
27 # # => disables the UserObserver
28 #
29 # User.observers.disable AuditTrail
30 # # => disables the AuditTrail observer for User notifications.
31 # # Other models will still notify the AuditTrail observer.
32 #
33 # ORM.observers.disable :observer_1, :observer_2
34 # # => disables Observer1 and Observer2 for all models.
35 #
36 # User.observers.disable :all do
37 # # all user observers are disabled for
38 # # just the duration of the block
39 # end
1f8cc44 @myronmarston Allow observers to be enabled and disabled.
myronmarston authored
40 def disable(*observers, &block)
41 set_enablement(false, observers, &block)
42 end
43
2a4b780 @smartinez87 Remove extra white spaces on ActiveModel docs.
smartinez87 authored
44 # Enables one or more observers. This supports multiple forms:
99363dc @myronmarston Add documentation for new observer enable/disable feature.
myronmarston authored
45 #
f202088 @robbkidd Make observer enable/disable documentation more informative.
robbkidd authored
46 # ORM.observers.enable :all
b7b97fa add :nodoc: directive to AM::ObserverArray internal methods [ci skip]
Francesco Rodriguez authored
47 # # => enables all observers for all models subclassed from
f202088 @robbkidd Make observer enable/disable documentation more informative.
robbkidd authored
48 # # an ORM base class that includes ActiveModel::Observing
49 # # e.g. ActiveRecord::Base
50 #
99363dc @myronmarston Add documentation for new observer enable/disable feature.
myronmarston authored
51 # ORM.observers.enable :user_observer
52 # # => enables the UserObserver
53 #
54 # User.observers.enable AuditTrail
55 # # => enables the AuditTrail observer for User notifications.
56 # # Other models will not be affected (i.e. they will not
57 # # trigger notifications to AuditTrail if previously disabled)
58 #
59 # ORM.observers.enable :observer_1, :observer_2
60 # # => enables Observer1 and Observer2 for all models.
61 #
62 # User.observers.enable :all do
63 # # all user observers are enabled for
64 # # just the duration of the block
65 # end
66 #
2a4b780 @smartinez87 Remove extra white spaces on ActiveModel docs.
smartinez87 authored
67 # Note: all observers are enabled by default. This method is only
99363dc @myronmarston Add documentation for new observer enable/disable feature.
myronmarston authored
68 # useful when you have previously disabled one or more observers.
1f8cc44 @myronmarston Allow observers to be enabled and disabled.
myronmarston authored
69 def enable(*observers, &block)
70 set_enablement(true, observers, &block)
71 end
72
05d4653 @myronmarston Revert "Revert "Handle enabling/disabling observers at different leve…
myronmarston authored
73 protected
1f8cc44 @myronmarston Allow observers to be enabled and disabled.
myronmarston authored
74
b7b97fa add :nodoc: directive to AM::ObserverArray internal methods [ci skip]
Francesco Rodriguez authored
75 def disabled_observers #:nodoc:
fef2215 @myronmarston Fix bug with AM::Observer disablement.
myronmarston authored
76 @disabled_observers ||= Set.new
77 end
78
b7b97fa add :nodoc: directive to AM::ObserverArray internal methods [ci skip]
Francesco Rodriguez authored
79 def observer_class_for(observer) #:nodoc:
1f8cc44 @myronmarston Allow observers to be enabled and disabled.
myronmarston authored
80 return observer if observer.is_a?(Class)
81
82 if observer.respond_to?(:to_sym) # string/symbol
83 observer.to_s.camelize.constantize
84 else
85 raise ArgumentError, "#{observer} was not a class or a " +
86 "lowercase, underscored class name as expected."
87 end
88 end
89
b7b97fa add :nodoc: directive to AM::ObserverArray internal methods [ci skip]
Francesco Rodriguez authored
90 def start_transaction #:nodoc:
05d4653 @myronmarston Revert "Revert "Handle enabling/disabling observers at different leve…
myronmarston authored
91 disabled_observer_stack.push(disabled_observers.dup)
92 each_subclass_array do |array|
93 array.start_transaction
94 end
95 end
96
b7b97fa add :nodoc: directive to AM::ObserverArray internal methods [ci skip]
Francesco Rodriguez authored
97 def disabled_observer_stack #:nodoc:
05d4653 @myronmarston Revert "Revert "Handle enabling/disabling observers at different leve…
myronmarston authored
98 @disabled_observer_stack ||= []
99 end
100
b7b97fa add :nodoc: directive to AM::ObserverArray internal methods [ci skip]
Francesco Rodriguez authored
101 def end_transaction #:nodoc:
05d4653 @myronmarston Revert "Revert "Handle enabling/disabling observers at different leve…
myronmarston authored
102 @disabled_observers = disabled_observer_stack.pop
103 each_subclass_array do |array|
104 array.end_transaction
105 end
106 end
107
b7b97fa add :nodoc: directive to AM::ObserverArray internal methods [ci skip]
Francesco Rodriguez authored
108 def transaction #:nodoc:
05d4653 @myronmarston Revert "Revert "Handle enabling/disabling observers at different leve…
myronmarston authored
109 start_transaction
1f8cc44 @myronmarston Allow observers to be enabled and disabled.
myronmarston authored
110
111 begin
112 yield
113 ensure
05d4653 @myronmarston Revert "Revert "Handle enabling/disabling observers at different leve…
myronmarston authored
114 end_transaction
115 end
116 end
117
b7b97fa add :nodoc: directive to AM::ObserverArray internal methods [ci skip]
Francesco Rodriguez authored
118 def each_subclass_array #:nodoc:
9a38539 @myronmarston Fix dev env memory leaks by using AS::DescendantsTracker rather than …
myronmarston authored
119 model_class.descendants.each do |subclass|
120 yield subclass.observers
1f8cc44 @myronmarston Allow observers to be enabled and disabled.
myronmarston authored
121 end
122 end
123
b7b97fa add :nodoc: directive to AM::ObserverArray internal methods [ci skip]
Francesco Rodriguez authored
124 def set_enablement(enabled, observers) #:nodoc:
1f8cc44 @myronmarston Allow observers to be enabled and disabled.
myronmarston authored
125 if block_given?
126 transaction do
127 set_enablement(enabled, observers)
128 yield
129 end
130 else
9a38539 @myronmarston Fix dev env memory leaks by using AS::DescendantsTracker rather than …
myronmarston authored
131 observers = ActiveModel::Observer.descendants if observers == [:all]
1f8cc44 @myronmarston Allow observers to be enabled and disabled.
myronmarston authored
132 observers.each do |obs|
133 klass = observer_class_for(obs)
134
135 unless klass < ActiveModel::Observer
136 raise ArgumentError.new("#{obs} does not refer to a valid observer")
137 end
138
139 if enabled
140 disabled_observers.delete(klass)
141 else
142 disabled_observers << klass
143 end
144 end
05d4653 @myronmarston Revert "Revert "Handle enabling/disabling observers at different leve…
myronmarston authored
145
146 each_subclass_array do |array|
147 array.set_enablement(enabled, observers)
148 end
1f8cc44 @myronmarston Allow observers to be enabled and disabled.
myronmarston authored
149 end
150 end
151 end
152 end
Something went wrong with that request. Please try again.