Permalink
Browse files

Merge pull request #6062 from marcandre/tweak_observer

Tweak observer
  • Loading branch information...
2 parents f7ccfdc + 569fb1f commit c19acab866a48d57655132f33ab224b59d87685b @tenderlove tenderlove committed Apr 29, 2012
Showing with 21 additions and 5 deletions.
  1. +6 −5 activemodel/lib/active_model/observing.rb
  2. +15 −0 activemodel/test/cases/observing_test.rb
@@ -90,14 +90,15 @@ def count_observers
def instantiate_observer(observer) #:nodoc:
# string/symbol
if observer.respond_to?(:to_sym)
- observer.to_s.camelize.constantize.instance
- elsif observer.respond_to?(:instance)
+ observer = observer.to_s.camelize.constantize
+ end
+ if observer.respond_to?(:instance)
observer.instance
else
raise ArgumentError,
- "#{observer} must be a lowercase, underscored class name (or an " +
- "instance of the class itself) responding to the instance " +
- "method. Example: Person.observers = :big_brother # calls " +
+ "#{observer} must be a lowercase, underscored class name (or " +
+ "the class itself) responding to the method :instance. " +
+ "Example: Person.observers = :big_brother # calls " +
"BigBrother.instance"
end
end
@@ -70,6 +70,21 @@ def setup
ObservedModel.instantiate_observers
end
+ 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
+
test "passes observers to subclasses" do
FooObserver.instance
bar = Class.new(Foo)

0 comments on commit c19acab

Please sign in to comment.