Skip to content
This repository
Browse code

Use source.decorate when decorator is not specified

Closes #490
  • Loading branch information...
commit f64859e302482700c060b085bf131c290877e062 1 parent 2444c09
Andrew Haines authored
24  lib/draper/factory.rb
@@ -48,18 +48,22 @@ def call(options)
48 48
       end
49 49
 
50 50
       def decorator
51  
-        return collection_decorator if collection?
52  
-        decorator_class.method(:decorate)
  51
+        return decorator_method(decorator_class) if decorator_class
  52
+        return source_decorator if decoratable?
  53
+        return decorator_method(Draper::CollectionDecorator) if collection?
  54
+        raise Draper::UninferrableDecoratorError.new(source.class)
53 55
       end
54 56
 
55 57
       private
56 58
 
57  
-      attr_reader :source
  59
+      attr_reader :decorator_class, :source
58 60
 
59  
-      def collection_decorator
60  
-        klass = decorator_class || Draper::CollectionDecorator
  61
+      def source_decorator
  62
+        ->(source, options) { source.decorate(options) }
  63
+      end
61 64
 
62  
-        if klass.respond_to?(:decorate_collection)
  65
+      def decorator_method(klass)
  66
+        if collection? && klass.respond_to?(:decorate_collection)
63 67
           klass.method(:decorate_collection)
64 68
         else
65 69
           klass.method(:decorate)
@@ -70,12 +74,8 @@ def collection?
70 74
         source.respond_to?(:first)
71 75
       end
72 76
 
73  
-      def decorator_class
74  
-        @decorator_class || source_decorator_class
75  
-      end
76  
-
77  
-      def source_decorator_class
78  
-        source.decorator_class if source.respond_to?(:decorator_class)
  77
+      def decoratable?
  78
+        source.respond_to?(:decorate)
79 79
       end
80 80
 
81 81
       def update_context(options)
30  spec/draper/factory_spec.rb
@@ -170,12 +170,24 @@ module Draper
170 170
         end
171 171
 
172 172
         context "when decorator_class is unspecified" do
173  
-          it "returns the .decorate method from the source's decorator" do
174  
-            decorator_class = Class.new(Decorator)
175  
-            source = double(decorator_class: decorator_class)
176  
-            worker = Factory::Worker.new(nil, source)
  173
+          context "and the source is decoratable" do
  174
+            it "returns the source's #decorate method" do
  175
+              source = double
  176
+              options = {foo: "bar"}
  177
+              worker = Factory::Worker.new(nil, source)
177 178
 
178  
-            expect(worker.decorator).to eq decorator_class.method(:decorate)
  179
+              source.should_receive(:decorate).with(options).and_return(:decorated)
  180
+              expect(worker.decorator.call(source, options)).to be :decorated
  181
+            end
  182
+          end
  183
+
  184
+          context "and the source is not decoratable" do
  185
+            it "raises an error" do
  186
+              source = double
  187
+              worker = Factory::Worker.new(nil, source)
  188
+
  189
+              expect{worker.decorator}.to raise_error UninferrableDecoratorError
  190
+            end
179 191
           end
180 192
         end
181 193
       end
@@ -201,13 +213,13 @@ module Draper
201 213
 
202 214
         context "when decorator_class is unspecified" do
203 215
           context "and the source is decoratable" do
204  
-            it "returns the .decorate method from the source's decorator" do
205  
-              decorator_class = Class.new(CollectionDecorator)
  216
+            it "returns the source's #decorate method" do
206 217
               source = []
207  
-              source.stub decorator_class: decorator_class
  218
+              options = {foo: "bar"}
208 219
               worker = Factory::Worker.new(nil, source)
209 220
 
210  
-              expect(worker.decorator).to eq decorator_class.method(:decorate)
  221
+              source.should_receive(:decorate).with(options).and_return(:decorated)
  222
+              expect(worker.decorator.call(source, options)).to be :decorated
211 223
             end
212 224
           end
213 225
 

0 notes on commit f64859e

Please sign in to comment.
Something went wrong with that request. Please try again.