will_paginate

https://github.com/mislav/will_paginate

Ruby

Pagination library for Rails and other Ruby applications

WillPaginate::Collection.create

Just like +new+, but yields the object after instantiation and returns it
afterwards. This is very useful for manual pagination:

  @entries = WillPaginate::Collection.create(1, 10) do |pager|
    result = Post.find(:all, :limit => pager.per_page, :offset => pager.offset)
    # inject the result array into the paginated collection:
    pager.replace(result)

    unless pager.total_entries
      # the pager didn't manage to guess the total count, do it manually
      pager.total_entries = Post.count
    end
  end

The possibilities with this are endless. For another example, here is how
WillPaginate used to define pagination for Array instances:

  Array.class_eval do
    def paginate(page = 1, per_page = 15)
      WillPaginate::Collection.create(page, per_page, size) do |pager|
        pager.replace self[pager.offset, pager.per_page].to_a
      end
    end
  end

The Array#paginate API has since then changed, but this still serves as a
fine example of WillPaginate::Collection usage.

Source | Google | Stack overflow

Edit

git clone [email protected]:mislav/will_paginate.git

cd will_paginate

open lib/will_paginate/collection.rb

Contribute

# Make a new branch

git checkout -b -your-name--update-docs-WillPaginate--Collection-create-for-pr


# Commit to git

git add lib/will_paginate/collection.rbgit commit -m "better docs for WillPaginate::Collection.create"


# Open pull request

gem install hub # on a mac you can `brew install hub`

hub fork

git push <your name> -your-name--update-docs-WillPaginate--Collection-create-for-pr

hub pull-request


# Celebrate!