From 234fbd98c6aec47c5e5932e844854c0d192c5e86 Mon Sep 17 00:00:00 2001 From: Andrew Haines Date: Sat, 20 Apr 2013 19:37:14 +0100 Subject: [PATCH] Fix decoration of unsaved collection associations Calling `#decorate` on an unsaved collection association forced a db lookup, clearing any items that have been added (e.g. using `#build`). Closes #518. --- lib/draper/factory.rb | 6 +++++- spec/draper/factory_spec.rb | 10 ++++++---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/lib/draper/factory.rb b/lib/draper/factory.rb index ad82282..184853a 100644 --- a/lib/draper/factory.rb +++ b/lib/draper/factory.rb @@ -59,7 +59,11 @@ module Draper attr_reader :decorator_class, :source def source_decorator - ->(source, options) { source.decorate(options) } + if collection? + ->(source, options) { source.decorator_class.decorate_collection(source, options.reverse_merge(with: nil))} + else + ->(source, options) { source.decorate(options) } + end end def decorator_method(klass) diff --git a/spec/draper/factory_spec.rb b/spec/draper/factory_spec.rb index 70b5e14..53e89c5 100644 --- a/spec/draper/factory_spec.rb +++ b/spec/draper/factory_spec.rb @@ -213,13 +213,15 @@ module Draper context "when decorator_class is unspecified" do context "and the source is decoratable" do - it "returns the source's #decorate method" do + it "returns the .decorate_collection method from the source's decorator" do source = [] - options = {foo: "bar"} + decorator_class = Class.new(Decorator) + source.stub decorator_class: decorator_class + source.stub decorate: nil worker = Factory::Worker.new(nil, source) - source.should_receive(:decorate).with(options).and_return(:decorated) - expect(worker.decorator.call(source, options)).to be :decorated + decorator_class.should_receive(:decorate_collection).with(source, foo: "bar", with: nil).and_return(:decorated) + expect(worker.decorator.call(source, foo: "bar")).to be :decorated end end