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.
This commit is contained in:
Andrew Haines 2013-04-20 19:37:14 +01:00
parent df6b4512d5
commit 234fbd98c6
2 changed files with 11 additions and 5 deletions

View File

@ -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)

View File

@ -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