Add UninferrableDecoratorError
This commit is contained in:
parent
4ccf21d900
commit
dc53646376
|
@ -18,3 +18,11 @@ require 'draper/railtie' if defined?(Rails)
|
||||||
# Test Support
|
# Test Support
|
||||||
require 'draper/test/rspec_integration' if defined?(RSpec) and RSpec.respond_to?(:configure)
|
require 'draper/test/rspec_integration' if defined?(RSpec) and RSpec.respond_to?(:configure)
|
||||||
require 'draper/test/minitest_integration' if defined?(MiniTest::Rails)
|
require 'draper/test/minitest_integration' if defined?(MiniTest::Rails)
|
||||||
|
|
||||||
|
module Draper
|
||||||
|
class UninferrableDecoratorError < NameError
|
||||||
|
def initialize(klass)
|
||||||
|
super("Could not infer a decorator for #{klass}.")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
|
@ -65,7 +65,7 @@ module Draper
|
||||||
singular_name = name.chomp("Decorator").singularize
|
singular_name = name.chomp("Decorator").singularize
|
||||||
"#{singular_name}Decorator".constantize
|
"#{singular_name}Decorator".constantize
|
||||||
rescue NameError
|
rescue NameError
|
||||||
raise NameError, "Could not infer a decorator for #{name}. Please specify the decorator class when creating instances of this class."
|
raise Draper::UninferrableDecoratorError.new(self)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -25,12 +25,14 @@ module Draper::Decoratable
|
||||||
|
|
||||||
module ClassMethods
|
module ClassMethods
|
||||||
def decorate(options = {})
|
def decorate(options = {})
|
||||||
decorator_proxy = decorator_class.decorate(self.scoped, options)
|
collection_decorator = decorator_class.decorate(self.scoped, options)
|
||||||
block_given? ? yield(decorator_proxy) : decorator_proxy
|
block_given? ? yield(collection_decorator) : collection_decorator
|
||||||
end
|
end
|
||||||
|
|
||||||
def decorator_class
|
def decorator_class
|
||||||
"#{model_name}Decorator".constantize
|
"#{model_name}Decorator".constantize
|
||||||
|
rescue NameError
|
||||||
|
raise Draper::UninferrableDecoratorError.new(self)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -90,7 +90,7 @@ describe Draper::CollectionDecorator do
|
||||||
end
|
end
|
||||||
|
|
||||||
it "raises when decorates an empty array without the klass" do
|
it "raises when decorates an empty array without the klass" do
|
||||||
expect{EnumerableProxy.decorate([])}.to raise_error
|
expect{EnumerableProxy.decorate([])}.to raise_error Draper::UninferrableDecoratorError
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -25,7 +25,7 @@ describe Draper::Decoratable do
|
||||||
|
|
||||||
describe "#decorated_with?" do
|
describe "#decorated_with?" do
|
||||||
it "returns false" do
|
it "returns false" do
|
||||||
subject.should_not be_decorated_with(ProductDecorator)
|
subject.should_not be_decorated_with ProductDecorator
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -35,6 +35,20 @@ describe Draper::Decoratable do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe ".decorator_class" do
|
||||||
|
context "when the decorator can be inferred from the model" do
|
||||||
|
it "returns the inferred decorator class" do
|
||||||
|
Product.decorator_class.should be ProductDecorator
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "when the decorator can't be inferred from the model" do
|
||||||
|
it "throws an UninferrableDecoratorError" do
|
||||||
|
expect{UninferrableDecoratorModel.decorator_class}.to raise_error Draper::UninferrableDecoratorError
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
describe Draper::Decoratable::ClassMethods do
|
describe Draper::Decoratable::ClassMethods do
|
||||||
shared_examples_for "a call to Draper::Decoratable::ClassMethods#decorate" do
|
shared_examples_for "a call to Draper::Decoratable::ClassMethods#decorate" do
|
||||||
subject { klass.limit }
|
subject { klass.limit }
|
||||||
|
|
|
@ -25,6 +25,7 @@ require './spec/support/samples/sequel_product'
|
||||||
require './spec/support/samples/specific_product_decorator'
|
require './spec/support/samples/specific_product_decorator'
|
||||||
require './spec/support/samples/some_thing'
|
require './spec/support/samples/some_thing'
|
||||||
require './spec/support/samples/some_thing_decorator'
|
require './spec/support/samples/some_thing_decorator'
|
||||||
|
require './spec/support/samples/uninferrable_decorator_model'
|
||||||
require './spec/support/samples/widget'
|
require './spec/support/samples/widget'
|
||||||
require './spec/support/samples/widget_decorator'
|
require './spec/support/samples/widget_decorator'
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,3 @@
|
||||||
|
class UninferrableDecoratorModel
|
||||||
|
include Draper::Decoratable
|
||||||
|
end
|
Loading…
Reference in New Issue