Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delegate CollectionDecorator#kind_of? to the underlying decorated collection #488

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We've used alias_method elsewhere, could you change this to do so as well please?


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