Created
September 13, 2012 16:17
-
-
Save saranyan/3715470 to your computer and use it in GitHub Desktop.
Extend will paginate for all arrays on your rails project
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# config/initializers/will_paginate_array.rb | |
require 'will_paginate/collection' | |
Array.class_eval do | |
attr_reader :total_pages | |
def paginate(options = {}) | |
raise ArgumentError, "parameter hash expected (got #{options.inspect})" unless Hash === options | |
WillPaginate::Collection.create( | |
options[:page] || 1, | |
options[:per_page] || 15, | |
options[:total_entries] || self.length ) do |pager| | |
pager.replace self[pager.offset, pager.per_page].to_a | |
@total_pages = pager.total_entries / pager.per_page | |
end | |
end | |
end | |
# Now you can paginate any array, something like | |
# @posts = <method that return an array>.paginate( page: params[:page] per_page: 5 ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment