Skip to content

Commit bb5d63a

Browse files
committedApr 21, 2013
Merge pull request drapergem#530 from haines/decorating_unsaved_associations
Fix decoration of unsaved collection associations
2 parents df6b451 + 234fbd9 commit bb5d63a

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed
 

‎lib/draper/factory.rb

+5-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,11 @@ def decorator
5959
attr_reader :decorator_class, :source
6060

6161
def source_decorator
62-
->(source, options) { source.decorate(options) }
62+
if collection?
63+
->(source, options) { source.decorator_class.decorate_collection(source, options.reverse_merge(with: nil))}
64+
else
65+
->(source, options) { source.decorate(options) }
66+
end
6367
end
6468

6569
def decorator_method(klass)

‎spec/draper/factory_spec.rb

+6-4
Original file line numberDiff line numberDiff line change
@@ -213,13 +213,15 @@ module Draper
213213

214214
context "when decorator_class is unspecified" do
215215
context "and the source is decoratable" do
216-
it "returns the source's #decorate method" do
216+
it "returns the .decorate_collection method from the source's decorator" do
217217
source = []
218-
options = {foo: "bar"}
218+
decorator_class = Class.new(Decorator)
219+
source.stub decorator_class: decorator_class
220+
source.stub decorate: nil
219221
worker = Factory::Worker.new(nil, source)
220222

221-
source.should_receive(:decorate).with(options).and_return(:decorated)
222-
expect(worker.decorator.call(source, options)).to be :decorated
223+
decorator_class.should_receive(:decorate_collection).with(source, foo: "bar", with: nil).and_return(:decorated)
224+
expect(worker.decorator.call(source, foo: "bar")).to be :decorated
223225
end
224226
end
225227

0 commit comments

Comments
 (0)
Please sign in to comment.