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:
parent
dcc5e784c0
commit
68151a8912
|
@ -6,7 +6,7 @@ module Draper
|
||||||
attr_accessor :context
|
attr_accessor :context
|
||||||
|
|
||||||
array_methods = Array.instance_methods - Object.instance_methods
|
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
|
# @param source collection to decorate
|
||||||
# @option options [Class] :with the class used to decorate items
|
# @option options [Class] :with the class used to decorate items
|
||||||
|
@ -34,10 +34,6 @@ module Draper
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def ==(other)
|
|
||||||
source == (other.respond_to?(:source) ? other.source : other)
|
|
||||||
end
|
|
||||||
|
|
||||||
def to_s
|
def to_s
|
||||||
klass = begin
|
klass = begin
|
||||||
decorator_class
|
decorator_class
|
||||||
|
|
|
@ -228,6 +228,16 @@ describe Draper::CollectionDecorator do
|
||||||
a.should_not == b
|
a.should_not == b
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|
||||||
describe "#to_s" do
|
describe "#to_s" do
|
||||||
|
|
|
@ -86,7 +86,7 @@ describe Draper::DecoratedAssociation do
|
||||||
let(:options) { {scope: :foo} }
|
let(:options) { {scope: :foo} }
|
||||||
|
|
||||||
it "applies the scope before decoration" do
|
it "applies the scope before decoration" do
|
||||||
scoped = [:scoped]
|
scoped = [Product.new]
|
||||||
associated.should_receive(:foo).and_return(scoped)
|
associated.should_receive(:foo).and_return(scoped)
|
||||||
decorated_association.call.should == scoped
|
decorated_association.call.should == scoped
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue