aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/lib/active_model
diff options
context:
space:
mode:
Diffstat (limited to 'activemodel/lib/active_model')
-rw-r--r--activemodel/lib/active_model/observer_array.rb34
-rw-r--r--activemodel/lib/active_model/observing.rb5
2 files changed, 36 insertions, 3 deletions
diff --git a/activemodel/lib/active_model/observer_array.rb b/activemodel/lib/active_model/observer_array.rb
index b8aa9cc1e2..f3b5811b81 100644
--- a/activemodel/lib/active_model/observer_array.rb
+++ b/activemodel/lib/active_model/observer_array.rb
@@ -48,7 +48,7 @@ module ActiveModel
set_enablement(true, observers, &block)
end
- private
+ protected
def observer_class_for(observer)
return observer if observer.is_a?(Class)
@@ -61,13 +61,37 @@ module ActiveModel
end
end
+ 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
+
def transaction
- orig_disabled_observers = disabled_observers.dup
+ start_transaction
begin
yield
ensure
- @disabled_observers = orig_disabled_observers
+ end_transaction
+ end
+ end
+
+ def each_subclass_array
+ model_class.subclasses.each do |subclass|
+ yield self.class.for(subclass)
end
end
@@ -92,6 +116,10 @@ module ActiveModel
disabled_observers << klass
end
end
+
+ each_subclass_array do |array|
+ array.set_enablement(enabled, observers)
+ end
end
end
end
diff --git a/activemodel/lib/active_model/observing.rb b/activemodel/lib/active_model/observing.rb
index e1a2ce218d..ba6be46670 100644
--- a/activemodel/lib/active_model/observing.rb
+++ b/activemodel/lib/active_model/observing.rb
@@ -70,6 +70,10 @@ module ActiveModel
observer_instances.size
end
+ def subclasses
+ @subclasses ||= []
+ end
+
protected
def instantiate_observer(observer) #:nodoc:
# string/symbol
@@ -89,6 +93,7 @@ module ActiveModel
# Notify observers when the observed class is subclassed.
def inherited(subclass)
super
+ subclasses << subclass
notify_observers :observed_class_inherited, subclass
end
end