Skip to content

Instantly share code, notes, and snippets.

@electronicbites
Created January 9, 2009 11:35
Show Gist options
  • Save electronicbites/45089 to your computer and use it in GitHub Desktop.
Save electronicbites/45089 to your computer and use it in GitHub Desktop.
# helper method to paginate a non ActiveRecord collection like a Array
def paginate_collection(collection, opts = {})
opts[:per_page] ||= 20
opts[:page] ||= 1
opts[:page] = opts[:page].to_i
opts[:per_page] = opts[:per_page].to_i
returning WillPaginate::Collection.new( opts[:page], opts[:per_page], collection.size ) do |pager|
start = (opts[:page]-1) * opts[:per_page]
finish = start + opts[:per_page]
pager.replace collection[ start...finish ]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment