From a5d146c6dc936100451b537060bb6bd6a63c5402 Mon Sep 17 00:00:00 2001 From: David Rueck Date: Wed, 13 Mar 2013 16:04:10 -0700 Subject: [PATCH] Decorated collection also responds to decorated_with? --- lib/draper/collection_decorator.rb | 7 +++++++ spec/draper/collection_decorator_spec.rb | 9 +++++++++ 2 files changed, 16 insertions(+) diff --git a/lib/draper/collection_decorator.rb b/lib/draper/collection_decorator.rb index cefc4be..f9a1cf5 100644 --- a/lib/draper/collection_decorator.rb +++ b/lib/draper/collection_decorator.rb @@ -66,6 +66,13 @@ module Draper true end + # Checks if a given decorator has been applied to the collection. + # + # @param [Class] decorator_class + def decorated_with?(decorator_class) + self.instance_of? decorator_class + end + def kind_of?(klass) decorated_collection.kind_of?(klass) || super end diff --git a/spec/draper/collection_decorator_spec.rb b/spec/draper/collection_decorator_spec.rb index 9ac252f..fe1ca4d 100644 --- a/spec/draper/collection_decorator_spec.rb +++ b/spec/draper/collection_decorator_spec.rb @@ -243,6 +243,15 @@ module Draper end end + describe '#decorated_with?' do + it "checks if a decorator has been applied to a collection" do + decorator = ProductsDecorator.new([Product.new]) + + expect(decorator).to be_decorated_with ProductsDecorator + expect(decorator).not_to be_decorated_with OtherDecorator + end + end + describe '#kind_of?' do it 'asks the kind of its decorated collection' do decorator = ProductsDecorator.new([])