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.
This commit is contained in:
Andrew Haines 2012-12-29 12:43:33 +00:00
parent dcc5e784c0
commit 68151a8912
3 changed files with 12 additions and 6 deletions

View File

@ -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

View File

@ -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

View File

@ -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