1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Generate appropriate error more judiciously

This commit is contained in:
Marc-Andre Lafortune 2012-04-29 00:51:56 -04:00
parent 85c056cc1f
commit 569fb1fffb
2 changed files with 18 additions and 2 deletions

View file

@ -90,8 +90,9 @@ module ActiveModel
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,

View file

@ -70,6 +70,21 @@ class ObservingTest < ActiveModel::TestCase
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)