Permalink
Browse files

Fix bug with AM::Observer disablement.

Now that we propagate the enabling/disabling to descendants, we no longer have to check the disabled_observer Set on each superclass of the model class.  This was causing a bug when disabling all observers at a superclass level and then enabling an individual observer at a subclass level.

Plus the logic is simpler now :).
  • Loading branch information...
1 parent 9a38539 commit fef22157b07f101229d29544d578bfe2cb9fedfe @myronmarston myronmarston committed Apr 28, 2011
@@ -4,34 +4,16 @@ module ActiveModel
# Stores the enabled/disabled state of individual observers for
# a particular model classes.
class ObserverArray < Array
- # returns false if:
- # - the ObserverArray for the given model's class has the given observer
- # in its disabled_observers set.
- # - or that is the case at any level of the model's superclass chain.
- def self.observer_enabled?(observer, model)
- klass = model.class
- observer_class = observer.class
-
- loop do
- break unless klass.respond_to?(:observers)
- array = klass.observers
- return false if array.disabled_observers.include?(observer_class)
- klass = klass.superclass
- end
-
- true # observers are enabled by default
- end
-
- def disabled_observers
- @disabled_observers ||= Set.new
- end
-
attr_reader :model_class
def initialize(model_class, *args)
@model_class = model_class
super(*args)
end
+ def disabled_for?(observer)
+ disabled_observers.include?(observer.class)
+ end
+
def disable(*observers, &block)
set_enablement(false, observers, &block)
end
@@ -42,6 +24,10 @@ def enable(*observers, &block)
protected
+ def disabled_observers
+ @disabled_observers ||= Set.new
+ end
+
def observer_class_for(observer)
return observer if observer.is_a?(Class)
@@ -222,9 +222,9 @@ def observed_classes #:nodoc:
# Send observed_method(object) if the method exists.
def update(observed_method, object) #:nodoc:
- if respond_to?(observed_method) && ObserverArray.observer_enabled?(self, object)
- send(observed_method, object)
- end
+ return unless respond_to?(observed_method)
+ return if disabled_for?(object)
+ send(observed_method, object)
end
# Special method sent by the observed class when it is inherited.
@@ -238,5 +238,11 @@ def observed_class_inherited(subclass) #:nodoc:
def add_observer!(klass) #:nodoc:
klass.add_observer(self)
end
+
+ def disabled_for?(object)
+ klass = object.class
+ return false unless klass.respond_to?(:observers)
+ klass.observers.disabled_for?(self)
+ end
end
end
@@ -65,6 +65,16 @@ def assert_observer_not_notified(model_class, observer_class)
assert_observer_notified Budget, AuditTrail
end
+ test "can enable observers on individual models without affecting those observers on other models" do
+ ORM.observers.disable :all
+ Budget.observers.enable AuditTrail
+
+ assert_observer_not_notified Widget, WidgetObserver
+ assert_observer_not_notified Budget, BudgetObserver
+ assert_observer_not_notified Widget, AuditTrail
+ assert_observer_notified Budget, AuditTrail
+ end
+
test "can disable observers for the duration of a block" do
yielded = false
ORM.observers.disable :budget_observer do

0 comments on commit fef2215

Please sign in to comment.