Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Mickael

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

Paginate over a collection (Array or Hash)

Sometimes 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
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS