Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "Optimization: Optimize CollectionDecorator Enumerable behavior" #801

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 1 addition & 18 deletions lib/draper/collection_decorator.rb
Expand Up @@ -15,7 +15,7 @@ class CollectionDecorator
# to each item's decorator.
attr_accessor :context

array_methods = Array.instance_methods - Object.instance_methods - Enumerable.instance_methods
array_methods = Array.instance_methods - Object.instance_methods
delegate :==, :as_json, *array_methods, to: :decorated_collection

# @param [Enumerable] object
Expand All @@ -42,23 +42,6 @@ def decorated_collection
@decorated_collection ||= object.map{|item| decorate_item(item)}
end

# Optimization to prevent unnecessary iteration (useful for larger collections).
# Iterates over the collection, decorating objects as it goes. If the decorated collection is
# already set, iterate over it instead.
def each(&block)
if @decorated_collection
@decorated_collection.each(&block)
else
@decorated_collection = []
object.each do |item|
decorated_item = decorate_item(item)
@decorated_collection << decorated_item

block.call(decorated_item)
end
end
end

delegate :find, to: :decorated_collection

def to_s
Expand Down
33 changes: 0 additions & 33 deletions spec/draper/collection_decorator_spec.rb
Expand Up @@ -288,38 +288,5 @@ module Draper
end
end

describe "#each" do
it "iterates over the collection, decorating as it goes" do
collection = [Product.new]
decorator = CollectionDecorator.new(collection)

expect(collection).to_not receive(:map)
decorator.each { |product| product.decorated? }
expect(decorator.instance_variable_get(:@decorated_collection)[0]).to be_decorated
end

it "uses decorated_collection if already set" do
decorated_collection = double(:decorated_collection)
decorator = CollectionDecorator.new([])
decorator.instance_variable_set(:@decorated_collection, decorated_collection)

expect(decorated_collection).to receive(:each)

decorator.each
end
end

describe "Enumerable methods" do
it "doesn't delegate Enumerable methods to its decorated collection" do
decorated_collection = double(:decorated_collection)
decorator = CollectionDecorator.new([])
decorator.instance_variable_set(:@decorated_collection, decorated_collection)

expect(decorated_collection).to_not receive(:map)

decorator.map
end
end

end
end