Permalink
Browse files

Use source.decorate when decorator is not specified

Closes #490
  • Loading branch information...
haines committed Mar 10, 2013
1 parent 2444c09 commit f64859e302482700c060b085bf131c290877e062
Showing with 33 additions and 21 deletions.
  1. +12 −12 lib/draper/factory.rb
  2. +21 −9 spec/draper/factory_spec.rb
View
@@ -48,18 +48,22 @@ def call(options)
end
def decorator
- return collection_decorator if collection?
- decorator_class.method(:decorate)
+ return decorator_method(decorator_class) if decorator_class
+ return source_decorator if decoratable?
+ return decorator_method(Draper::CollectionDecorator) if collection?
+ raise Draper::UninferrableDecoratorError.new(source.class)
end
private
- attr_reader :source
+ attr_reader :decorator_class, :source
- def collection_decorator
- klass = decorator_class || Draper::CollectionDecorator
+ def source_decorator
+ ->(source, options) { source.decorate(options) }
+ end
- if klass.respond_to?(:decorate_collection)
+ def decorator_method(klass)
+ if collection? && klass.respond_to?(:decorate_collection)
klass.method(:decorate_collection)
else
klass.method(:decorate)
@@ -70,12 +74,8 @@ def collection?
source.respond_to?(:first)
end
- def decorator_class
- @decorator_class || source_decorator_class
- end
-
- def source_decorator_class
- source.decorator_class if source.respond_to?(:decorator_class)
+ def decoratable?
+ source.respond_to?(:decorate)
end
def update_context(options)
@@ -170,12 +170,24 @@ module Draper
end
context "when decorator_class is unspecified" do
- it "returns the .decorate method from the source's decorator" do
- decorator_class = Class.new(Decorator)
- source = double(decorator_class: decorator_class)
- worker = Factory::Worker.new(nil, source)
+ context "and the source is decoratable" do
+ it "returns the source's #decorate method" do
+ source = double
+ options = {foo: "bar"}
+ worker = Factory::Worker.new(nil, source)
- expect(worker.decorator).to eq decorator_class.method(:decorate)
+ source.should_receive(:decorate).with(options).and_return(:decorated)
+ expect(worker.decorator.call(source, options)).to be :decorated
+ end
+ end
+
+ context "and the source is not decoratable" do
+ it "raises an error" do
+ source = double
+ worker = Factory::Worker.new(nil, source)
+
+ expect{worker.decorator}.to raise_error UninferrableDecoratorError
+ end
end
end
end
@@ -201,13 +213,13 @@ module Draper
context "when decorator_class is unspecified" do
context "and the source is decoratable" do
- it "returns the .decorate method from the source's decorator" do
- decorator_class = Class.new(CollectionDecorator)
+ it "returns the source's #decorate method" do
source = []
- source.stub decorator_class: decorator_class
+ options = {foo: "bar"}
worker = Factory::Worker.new(nil, source)
- expect(worker.decorator).to eq decorator_class.method(:decorate)
+ source.should_receive(:decorate).with(options).and_return(:decorated)
+ expect(worker.decorator.call(source, options)).to be :decorated
end
end

0 comments on commit f64859e

Please sign in to comment.