From 68151a89125d473b4fd660aa786d951f15cb790a Mon Sep 17 00:00:00 2001 From: Andrew Haines Date: Sat, 29 Dec 2012 12:43:33 +0000 Subject: [PATCH] Delegate CollectionDecorator#== to decorated collection Decorator#== compares sources, so CollectionDecorator#== can just use a standard array comparison with the decorated collection. This covers the case that the decorated and source collections differ because of changes made after decoration. --- lib/draper/collection_decorator.rb | 6 +----- spec/draper/collection_decorator_spec.rb | 10 ++++++++++ spec/draper/decorated_association_spec.rb | 2 +- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/lib/draper/collection_decorator.rb b/lib/draper/collection_decorator.rb index 66e6964..2644c87 100644 --- a/lib/draper/collection_decorator.rb +++ b/lib/draper/collection_decorator.rb @@ -6,7 +6,7 @@ module Draper attr_accessor :context array_methods = Array.instance_methods - Object.instance_methods - delegate :as_json, *array_methods, to: :decorated_collection + delegate :==, :as_json, *array_methods, to: :decorated_collection # @param source collection to decorate # @option options [Class] :with the class used to decorate items @@ -34,10 +34,6 @@ module Draper end end - def ==(other) - source == (other.respond_to?(:source) ? other.source : other) - end - def to_s klass = begin decorator_class diff --git a/spec/draper/collection_decorator_spec.rb b/spec/draper/collection_decorator_spec.rb index cf6f83a..6b85105 100644 --- a/spec/draper/collection_decorator_spec.rb +++ b/spec/draper/collection_decorator_spec.rb @@ -228,6 +228,16 @@ describe Draper::CollectionDecorator do a.should_not == b end end + + context "when the decorated collection has been modified" do + it "is no longer equal to the source" do + a = Draper::CollectionDecorator.new(source, with: ProductDecorator) + b = source.dup + + a << Product.new.decorate + a.should_not == b + end + end end describe "#to_s" do diff --git a/spec/draper/decorated_association_spec.rb b/spec/draper/decorated_association_spec.rb index 1de928e..c37b3f9 100644 --- a/spec/draper/decorated_association_spec.rb +++ b/spec/draper/decorated_association_spec.rb @@ -86,7 +86,7 @@ describe Draper::DecoratedAssociation do let(:options) { {scope: :foo} } it "applies the scope before decoration" do - scoped = [:scoped] + scoped = [Product.new] associated.should_receive(:foo).and_return(scoped) decorated_association.call.should == scoped end