Decorate structs as singular objects (and not as collections)

Since structs respond to `.first`, Draper thinks they're collections and
always tries to wrap them in CollectionDecorators, which is surprising!

This is similar to the issue and fix in [#259].
This commit is contained in:
Jordan Killpack 2014-04-01 16:56:55 -04:00
parent 8e22c0c109
commit 67638332ce
2 changed files with 12 additions and 1 deletions

View File

@ -75,7 +75,7 @@ module Draper
end
def collection?
object.respond_to?(:first)
object.respond_to?(:first) && !object.is_a?(Struct)
end
def decoratable?

View File

@ -190,6 +190,17 @@ module Draper
end
end
end
context "when the object is a struct" do
it "returns a singular decorator" do
object = Struct.new(:stuff).new("things")
decorator_class = Class.new(Decorator)
worker = Factory::Worker.new(decorator_class, object)
expect(worker.decorator).to eq decorator_class.method(:decorate)
end
end
end
context "for a collection object" do