Skip to content

Instantly share code, notes, and snippets.

@Dimanaux
Last active November 15, 2019 16:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Dimanaux/c03878e59520c69ed65344a297a3efb2 to your computer and use it in GitHub Desktop.
Save Dimanaux/c03878e59520c69ed65344a297a3efb2 to your computer and use it in GitHub Desktop.
class CollectionDecorator
def self.page(objects_relation, page = 1)
CollectionDecorator.new(objects_relation.order("created_at").page(page))
end
def initialize(objects_relation)
@objects_relation = objects_relation
end
include Enumerable
def each(&block)
if block_given?
@objects_relation.each { |p| block.call(p.decorate) }
else
to_enum(:each)
end
end
def to_ary
to_a
end
def respond_to?(method)
@objects_relation.respond_to?(method)
end
def respond_to_missing?(method)
respond_to?(method)
end
def method_missing(method, *args, &block)
@objects_relation.send(method, *args, &block)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment