Paginate Over A Collection (Array Or Hash)
Join the DZone community and get the full member experience.
Join For FreeSometimes could happen that we need to paginate something different from a collection, I found this very useful method that acts similar to a paginate but it works for Arrays ( and Hashes )
def paginate_collection(collection, options = {})
default_options = {:per_page => 10, :page => 1}
options = default_options.merge options
pages = Paginator.new self, collection.size, options[:per_page], options[:page]
first = pages.current.offset
last = [first + options[:per_page], collection.size].min
slice = collection[first...last]
return [pages, slice]
end
Opinions expressed by DZone contributors are their own.
Comments