Skip to content

Commit

Permalink
Fix CollectionDecorator#to_s
Browse files Browse the repository at this point in the history
Previously it could raise an error when set to infer each item's
decorator.
  • Loading branch information
haines committed Dec 26, 2012
1 parent 9f49e1c commit eefd7d0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/draper/collection_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,13 @@ def ==(other)
end

def to_s
"#<CollectionDecorator of #{decorator_class} for #{source.inspect}>"
klass = begin
decorator_class
rescue Draper::UninferrableDecoratorError
"inferred decorators"
end

"#<CollectionDecorator of #{klass} for #{source.inspect}>"
end

def context=(value)
Expand Down
21 changes: 21 additions & 0 deletions spec/draper/collection_decorator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -286,4 +286,25 @@ def page_number
end
end

describe "#to_s" do
subject { Draper::CollectionDecorator.new(source, options) }
let(:source) { ["a", "b", "c"] }

context "when :with option was given" do
let(:options) { {with: ProductDecorator} }

it "returns a string representation of the CollectionDecorator" do
subject.to_s.should == '#<CollectionDecorator of ProductDecorator for ["a", "b", "c"]>'
end
end

context "when :with option was not given" do
let(:options) { {} }

it "returns a string representation of the CollectionDecorator" do
subject.to_s.should == '#<CollectionDecorator of inferred decorators for ["a", "b", "c"]>'
end
end
end

end

0 comments on commit eefd7d0

Please sign in to comment.