2009-03-20 06:32:24 -04:00
|
|
|
require 'cases/helper'
|
2008-06-28 02:29:03 -04:00
|
|
|
|
2009-07-21 01:13:26 -04:00
|
|
|
class ObservedModel
|
|
|
|
include ActiveModel::Observing
|
|
|
|
|
2008-06-28 02:29:03 -04:00
|
|
|
class Observer
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class FooObserver < ActiveModel::Observer
|
|
|
|
class << self
|
|
|
|
public :new
|
|
|
|
end
|
2009-06-11 00:35:34 -04:00
|
|
|
|
2008-06-28 02:29:03 -04:00
|
|
|
attr_accessor :stub
|
|
|
|
|
|
|
|
def on_spec(record)
|
|
|
|
stub.event_with(record) if stub
|
|
|
|
end
|
2011-08-12 22:58:37 -04:00
|
|
|
|
|
|
|
def around_save(record)
|
|
|
|
yield :in_around_save
|
|
|
|
end
|
2008-06-28 02:29:03 -04:00
|
|
|
end
|
|
|
|
|
2009-07-21 01:13:26 -04:00
|
|
|
class Foo
|
|
|
|
include ActiveModel::Observing
|
2008-06-28 02:29:03 -04:00
|
|
|
end
|
|
|
|
|
2008-06-28 03:55:02 -04:00
|
|
|
class ObservingTest < ActiveModel::TestCase
|
2008-06-28 02:29:03 -04:00
|
|
|
def setup
|
|
|
|
ObservedModel.observers.clear
|
|
|
|
end
|
|
|
|
|
|
|
|
test "initializes model with no cached observers" do
|
|
|
|
assert ObservedModel.observers.empty?, "Not empty: #{ObservedModel.observers.inspect}"
|
|
|
|
end
|
2009-06-11 00:35:34 -04:00
|
|
|
|
2008-06-28 02:29:03 -04:00
|
|
|
test "stores cached observers in an array" do
|
|
|
|
ObservedModel.observers << :foo
|
|
|
|
assert ObservedModel.observers.include?(:foo), ":foo not in #{ObservedModel.observers.inspect}"
|
|
|
|
end
|
2009-06-11 00:35:34 -04:00
|
|
|
|
2008-06-28 02:29:03 -04:00
|
|
|
test "flattens array of assigned cached observers" do
|
|
|
|
ObservedModel.observers = [[:foo], :bar]
|
|
|
|
assert ObservedModel.observers.include?(:foo), ":foo not in #{ObservedModel.observers.inspect}"
|
|
|
|
assert ObservedModel.observers.include?(:bar), ":bar not in #{ObservedModel.observers.inspect}"
|
|
|
|
end
|
|
|
|
|
2011-04-27 14:07:42 -04:00
|
|
|
test "uses an ObserverArray so observers can be disabled" do
|
|
|
|
ObservedModel.observers = [:foo, :bar]
|
|
|
|
assert ObservedModel.observers.is_a?(ActiveModel::ObserverArray)
|
|
|
|
end
|
|
|
|
|
2008-11-19 14:08:34 -05:00
|
|
|
test "instantiates observer names passed as strings" do
|
|
|
|
ObservedModel.observers << 'foo_observer'
|
|
|
|
FooObserver.expects(:instance)
|
|
|
|
ObservedModel.instantiate_observers
|
|
|
|
end
|
|
|
|
|
|
|
|
test "instantiates observer names passed as symbols" do
|
|
|
|
ObservedModel.observers << :foo_observer
|
|
|
|
FooObserver.expects(:instance)
|
|
|
|
ObservedModel.instantiate_observers
|
|
|
|
end
|
|
|
|
|
|
|
|
test "instantiates observer classes" do
|
|
|
|
ObservedModel.observers << ObservedModel::Observer
|
|
|
|
ObservedModel::Observer.expects(:instance)
|
|
|
|
ObservedModel.instantiate_observers
|
2008-06-28 02:29:03 -04:00
|
|
|
end
|
2009-06-11 00:35:34 -04:00
|
|
|
|
2008-06-28 02:29:03 -04:00
|
|
|
test "passes observers to subclasses" do
|
|
|
|
FooObserver.instance
|
|
|
|
bar = Class.new(Foo)
|
|
|
|
assert_equal Foo.count_observers, bar.count_observers
|
|
|
|
end
|
|
|
|
end
|
2009-06-11 00:35:34 -04:00
|
|
|
|
2008-06-28 03:55:02 -04:00
|
|
|
class ObserverTest < ActiveModel::TestCase
|
2008-06-28 02:29:03 -04:00
|
|
|
def setup
|
|
|
|
ObservedModel.observers = :foo_observer
|
2009-06-11 00:35:34 -04:00
|
|
|
FooObserver.instance_eval do
|
|
|
|
alias_method :original_observed_classes, :observed_classes
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
|
|
|
FooObserver.instance_eval do
|
2009-09-05 20:10:21 -04:00
|
|
|
undef_method :observed_classes
|
2009-06-11 00:35:34 -04:00
|
|
|
alias_method :observed_classes, :original_observed_classes
|
|
|
|
end
|
2008-06-28 02:29:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "guesses implicit observable model name" do
|
2009-06-11 00:35:34 -04:00
|
|
|
assert_equal Foo, FooObserver.observed_class
|
2008-06-28 02:29:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "tracks implicit observable models" do
|
|
|
|
instance = FooObserver.new
|
|
|
|
assert instance.send(:observed_classes).include?(Foo), "Foo not in #{instance.send(:observed_classes).inspect}"
|
|
|
|
assert !instance.send(:observed_classes).include?(ObservedModel), "ObservedModel in #{instance.send(:observed_classes).inspect}"
|
|
|
|
end
|
2009-06-11 00:35:34 -04:00
|
|
|
|
2008-06-28 02:29:03 -04:00
|
|
|
test "tracks explicit observed model class" do
|
|
|
|
old_instance = FooObserver.new
|
|
|
|
assert !old_instance.send(:observed_classes).include?(ObservedModel), "ObservedModel in #{old_instance.send(:observed_classes).inspect}"
|
|
|
|
FooObserver.observe ObservedModel
|
|
|
|
instance = FooObserver.new
|
|
|
|
assert instance.send(:observed_classes).include?(ObservedModel), "ObservedModel not in #{instance.send(:observed_classes).inspect}"
|
|
|
|
end
|
2009-06-11 00:35:34 -04:00
|
|
|
|
2008-06-28 02:29:03 -04:00
|
|
|
test "tracks explicit observed model as string" do
|
|
|
|
old_instance = FooObserver.new
|
|
|
|
assert !old_instance.send(:observed_classes).include?(ObservedModel), "ObservedModel in #{old_instance.send(:observed_classes).inspect}"
|
|
|
|
FooObserver.observe 'observed_model'
|
|
|
|
instance = FooObserver.new
|
|
|
|
assert instance.send(:observed_classes).include?(ObservedModel), "ObservedModel not in #{instance.send(:observed_classes).inspect}"
|
|
|
|
end
|
2009-06-11 00:35:34 -04:00
|
|
|
|
2008-06-28 02:29:03 -04:00
|
|
|
test "tracks explicit observed model as symbol" do
|
|
|
|
old_instance = FooObserver.new
|
|
|
|
assert !old_instance.send(:observed_classes).include?(ObservedModel), "ObservedModel in #{old_instance.send(:observed_classes).inspect}"
|
|
|
|
FooObserver.observe :observed_model
|
|
|
|
instance = FooObserver.new
|
|
|
|
assert instance.send(:observed_classes).include?(ObservedModel), "ObservedModel not in #{instance.send(:observed_classes).inspect}"
|
|
|
|
end
|
2009-06-11 00:35:34 -04:00
|
|
|
|
2008-06-28 02:29:03 -04:00
|
|
|
test "calls existing observer event" do
|
|
|
|
foo = Foo.new
|
|
|
|
FooObserver.instance.stub = stub
|
|
|
|
FooObserver.instance.stub.expects(:event_with).with(foo)
|
|
|
|
Foo.send(:notify_observers, :on_spec, foo)
|
|
|
|
end
|
2009-06-11 00:35:34 -04:00
|
|
|
|
2008-06-28 02:29:03 -04:00
|
|
|
test "skips nonexistent observer event" do
|
|
|
|
foo = Foo.new
|
|
|
|
Foo.send(:notify_observers, :whatever, foo)
|
|
|
|
end
|
2011-08-12 22:58:37 -04:00
|
|
|
|
|
|
|
test "update passes a block on to the observer" do
|
|
|
|
yielded_value = nil
|
|
|
|
FooObserver.instance.update(:around_save, Foo.new) do |val|
|
|
|
|
yielded_value = val
|
|
|
|
end
|
|
|
|
assert_equal :in_around_save, yielded_value
|
|
|
|
end
|
2008-11-15 23:25:14 -05:00
|
|
|
end
|