mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
2a25c5818b
This reverts commit ad62f19287
because the current
subclasses implementation leaks memory in development.
Instead of keeping an array of subclasses, the better solution is to
ask the parent if an observer is being disabled or not.
122 lines
3.9 KiB
Ruby
122 lines
3.9 KiB
Ruby
require 'cases/helper'
|
|
require 'models/observers'
|
|
|
|
class ObserverArrayTest < ActiveModel::TestCase
|
|
def teardown
|
|
ORM.observers.enable :all
|
|
Budget.observers.enable :all
|
|
Widget.observers.enable :all
|
|
end
|
|
|
|
def assert_observer_notified(model_class, observer_class)
|
|
observer_class.instance.before_save_invocations.clear
|
|
model_instance = model_class.new
|
|
model_instance.save
|
|
assert_equal [model_instance], observer_class.instance.before_save_invocations
|
|
end
|
|
|
|
def assert_observer_not_notified(model_class, observer_class)
|
|
observer_class.instance.before_save_invocations.clear
|
|
model_instance = model_class.new
|
|
model_instance.save
|
|
assert_equal [], observer_class.instance.before_save_invocations
|
|
end
|
|
|
|
test "all observers are enabled by default" do
|
|
assert_observer_notified Widget, WidgetObserver
|
|
assert_observer_notified Budget, BudgetObserver
|
|
assert_observer_notified Widget, AuditTrail
|
|
assert_observer_notified Budget, AuditTrail
|
|
end
|
|
|
|
test "can disable individual observers using a class constant" do
|
|
ORM.observers.disable WidgetObserver
|
|
|
|
assert_observer_not_notified Widget, WidgetObserver
|
|
assert_observer_notified Budget, BudgetObserver
|
|
assert_observer_notified Widget, AuditTrail
|
|
assert_observer_notified Budget, AuditTrail
|
|
end
|
|
|
|
test "can disable individual observers using a symbol" do
|
|
ORM.observers.disable :budget_observer
|
|
|
|
assert_observer_notified Widget, WidgetObserver
|
|
assert_observer_not_notified Budget, BudgetObserver
|
|
assert_observer_notified Widget, AuditTrail
|
|
assert_observer_notified Budget, AuditTrail
|
|
end
|
|
|
|
test "can disable all observers using :all" do
|
|
ORM.observers.disable :all
|
|
|
|
assert_observer_not_notified Widget, WidgetObserver
|
|
assert_observer_not_notified Budget, BudgetObserver
|
|
assert_observer_not_notified Widget, AuditTrail
|
|
assert_observer_not_notified Budget, AuditTrail
|
|
end
|
|
|
|
test "can disable observers on individual models without affecting observers on other models" do
|
|
Widget.observers.disable :all
|
|
|
|
assert_observer_not_notified Widget, WidgetObserver
|
|
assert_observer_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
|
|
yielded = true
|
|
assert_observer_notified Widget, WidgetObserver
|
|
assert_observer_not_notified Budget, BudgetObserver
|
|
assert_observer_notified Widget, AuditTrail
|
|
assert_observer_notified Budget, AuditTrail
|
|
end
|
|
|
|
assert yielded
|
|
assert_observer_notified Widget, WidgetObserver
|
|
assert_observer_notified Budget, BudgetObserver
|
|
assert_observer_notified Widget, AuditTrail
|
|
assert_observer_notified Budget, AuditTrail
|
|
end
|
|
|
|
test "can enable observers for the duration of a block" do
|
|
yielded = false
|
|
Widget.observers.disable :all
|
|
|
|
Widget.observers.enable :all do
|
|
yielded = true
|
|
assert_observer_notified Widget, WidgetObserver
|
|
assert_observer_notified Budget, BudgetObserver
|
|
assert_observer_notified Widget, AuditTrail
|
|
assert_observer_notified Budget, AuditTrail
|
|
end
|
|
|
|
assert yielded
|
|
assert_observer_not_notified Widget, WidgetObserver
|
|
assert_observer_notified Budget, BudgetObserver
|
|
assert_observer_not_notified Widget, AuditTrail
|
|
assert_observer_notified Budget, AuditTrail
|
|
end
|
|
|
|
test "raises an appropriate error when a developer accidentally enables or disables the wrong class (i.e. Widget instead of WidgetObserver)" do
|
|
assert_raise ArgumentError do
|
|
ORM.observers.enable :widget
|
|
end
|
|
|
|
assert_raise ArgumentError do
|
|
ORM.observers.enable Widget
|
|
end
|
|
|
|
assert_raise ArgumentError do
|
|
ORM.observers.disable :widget
|
|
end
|
|
|
|
assert_raise ArgumentError do
|
|
ORM.observers.disable Widget
|
|
end
|
|
end
|
|
end
|
|
|