Permalink
Browse files

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.
  • Loading branch information...
1 parent dcc5e78 commit 68151a89125d473b4fd660aa786d951f15cb790a @haines haines committed Dec 29, 2012
@@ -6,7 +6,7 @@ class CollectionDecorator
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 @@ def find(*args, &block)
end
end
- def ==(other)
- source == (other.respond_to?(:source) ? other.source : other)
- end
-
def to_s
klass = begin
decorator_class
@@ -228,6 +228,16 @@
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
@@ -86,7 +86,7 @@
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

0 comments on commit 68151a8

Please sign in to comment.