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
|
|
|
|
|
2012-04-29 16:30:54 -04:00
|
|
|
def on_spec(record, *args)
|
|
|
|
stub.event_with(record, *args) if stub
|
2008-06-28 02:29:03 -04:00
|
|
|
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
|
|
|
|
2012-04-29 00:51:56 -04:00
|
|
|
test "raises an appropriate error when a developer accidentally adds the wrong class (i.e. Widget instead of WidgetObserver)" do
|
|
|
|
assert_raise ArgumentError do
|
|
|
|
ObservedModel.observers = ['string']
|
|
|
|
ObservedModel.instantiate_observers
|
|
|
|
end
|
|
|
|
assert_raise ArgumentError do
|
|
|
|
ObservedModel.observers = [:string]
|
|
|
|
ObservedModel.instantiate_observers
|
|
|
|
end
|
|
|
|
assert_raise ArgumentError do
|
|
|
|
ObservedModel.observers = [String]
|
|
|
|
ObservedModel.instantiate_observers
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-06-28 02:29:03 -04:00
|
|
|
test "passes observers to subclasses" do
|
|
|
|
FooObserver.instance
|
|
|
|
bar = Class.new(Foo)
|
2012-04-14 04:48:53 -04:00
|
|
|
assert_equal Foo.observers_count, bar.observers_count
|
2008-06-28 02:29:03 -04:00
|
|
|
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
|
2012-04-29 21:50:48 -04:00
|
|
|
FooObserver.singleton_class.instance_eval do
|
2009-06-11 00:35:34 -04:00
|
|
|
alias_method :original_observed_classes, :observed_classes
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
2012-04-29 21:50:48 -04:00
|
|
|
FooObserver.singleton_class.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
|
2012-04-30 05:55:02 -04:00
|
|
|
assert_equal [Foo], instance.observed_classes
|
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 "tracks explicit observed model class" do
|
|
|
|
FooObserver.observe ObservedModel
|
|
|
|
instance = FooObserver.new
|
2012-04-29 22:02:02 -04:00
|
|
|
assert_equal [ObservedModel], instance.observed_classes
|
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 "tracks explicit observed model as string" do
|
|
|
|
FooObserver.observe 'observed_model'
|
|
|
|
instance = FooObserver.new
|
2012-04-29 22:02:02 -04:00
|
|
|
assert_equal [ObservedModel], instance.observed_classes
|
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 "tracks explicit observed model as symbol" do
|
|
|
|
FooObserver.observe :observed_model
|
|
|
|
instance = FooObserver.new
|
2012-04-29 22:02:02 -04:00
|
|
|
assert_equal [ObservedModel], instance.observed_classes
|
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 "calls existing observer event" do
|
|
|
|
foo = Foo.new
|
|
|
|
FooObserver.instance.stub = stub
|
|
|
|
FooObserver.instance.stub.expects(:event_with).with(foo)
|
2012-04-30 14:20:42 -04:00
|
|
|
Foo.notify_observers(:on_spec, foo)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "calls existing observer event from the instance" do
|
|
|
|
foo = Foo.new
|
|
|
|
FooObserver.instance.stub = stub
|
|
|
|
FooObserver.instance.stub.expects(:event_with).with(foo)
|
|
|
|
foo.notify_observers(:on_spec)
|
2008-06-28 02:29:03 -04:00
|
|
|
end
|
2009-06-11 00:35:34 -04:00
|
|
|
|
2012-04-29 16:30:54 -04:00
|
|
|
test "passes extra arguments" do
|
|
|
|
foo = Foo.new
|
|
|
|
FooObserver.instance.stub = stub
|
|
|
|
FooObserver.instance.stub.expects(:event_with).with(foo, :bar)
|
|
|
|
Foo.send(:notify_observers, :on_spec, foo, :bar)
|
|
|
|
end
|
|
|
|
|
2008-06-28 02:29:03 -04:00
|
|
|
test "skips nonexistent observer event" do
|
|
|
|
foo = Foo.new
|
2012-04-30 14:20:42 -04:00
|
|
|
Foo.notify_observers(:whatever, foo)
|
2008-06-28 02:29:03 -04:00
|
|
|
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
|
2012-04-29 21:50:48 -04:00
|
|
|
|
|
|
|
test "observe redefines observed_classes class method" do
|
|
|
|
class BarObserver < ActiveModel::Observer
|
|
|
|
observe :foo
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal [Foo], BarObserver.observed_classes
|
|
|
|
|
|
|
|
BarObserver.observe(ObservedModel)
|
|
|
|
assert_equal [ObservedModel], BarObserver.observed_classes
|
|
|
|
end
|
2008-11-15 23:25:14 -05:00
|
|
|
end
|