Skip to content

Commit

Permalink
Delegate #kind_of? to the underlying decorated collection
Browse files Browse the repository at this point in the history
  • Loading branch information
jtrim authored and haines committed Mar 11, 2013
1 parent 942182e commit daabe77
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/draper/collection_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ def decorated?
true
end

def kind_of?(klass)
decorated_collection.kind_of?(klass) || super
end
alias :is_a? :kind_of?

protected

# @return the collection being decorated.
Expand Down
23 changes: 23 additions & 0 deletions spec/draper/collection_decorator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -259,5 +259,28 @@ module Draper
end
end

describe '#kind_of?' do
it 'asks the kind of its decorated collection' do
decorator = ProductsDecorator.new([])
decorator.decorated_collection.should_receive(:kind_of?).with(Array).and_return("true")
expect(decorator.kind_of?(Array)).to eq "true"
end

context 'when asking the underlying collection returns false' do
it 'asks the CollectionDecorator instance itself' do
decorator = ProductsDecorator.new([])
decorator.decorated_collection.stub(:kind_of?).with(::Draper::CollectionDecorator).and_return(false)
expect(decorator.kind_of?(::Draper::CollectionDecorator)).to be true
end
end
end

describe '#is_a?' do
it 'aliases to #kind_of?' do
decorator = ProductsDecorator.new([])
expect(decorator.method(:kind_of?)).to eq decorator.method(:is_a?)
end
end

end
end

0 comments on commit daabe77

Please sign in to comment.