An example rails app using the will_paginate and flow_pagination gems together
-
mexpolk-flow_pagination (github.com/mexpolk/flow_pagination/tree/master)
-
haml gem (haml-lang.com/)
-
faker (faker.rubyforge.org/)
-
populator (github.com/ryanb/populator/tree/master)
-
Rails 2.3.3 (www.rubyonrails.org)
Checkout the sources:
git clone git://github.com/mexpolk/flow_pagination_example.git
Install the required gems, from the command line run:
sudo gem install haml sudo gem install faker sudo gem install populator sudo gem install mexpolk-flow_pagination
controllers/categories_controller.rb:
class CategoriesController < ApplicationController def index @categories = Category.paginate :page => params[:page], :order => 'name' end end
app/views/cateogories/index.html.haml:
= javascript_include_tag :all %h1 Categories %ul#categories= render @categories = will_paginate @categories, :renderer => FlowPagination::LinkRenderer
app/views/categories/_category.html.haml:
%li{ :id => "category_#{category.id}" }=category.name
app/views/categories/index.js.rjs:
page.insert_html :bottom, :categories, :partial => @categories page.replace :flow_pagination, will_paginate(@categories, :renderer => FlowPagination::LinkRenderer)
To test this solution I’ve created a rake task to populate the database (lib/tasks/populate.rake):
namespace :db do desc 'Erase and fill database' task :populate => :environment do require 'populator' require 'faker' Category.delete_all Category.populate 100 do |category| category.name = Populator.words(1..3).titleize end end end
Run your application, browse your category listing (localhost:3000/categories). Go to the end of the page and, if you have more than 30 categories (or more than the number specified in the per_page
parameter), you should see the “More” button. Click it and the next records will be added next to the previous ones.
FlowPagination
supports internationalization (Rails i18n
), thus you can change the label like this:
I18n.backend.store_translations :en, 'flow_pagination.button' => { :default => 'Next Records' }
Another way to accomplish the same is to add the following in your locales (config/locales/en.application.yml):
en: flow_pagination: button: Next Records
If you wan’t to use FlowPagination::LinkRenderer
as default, instead of specify the renderer
parameter each call to WillPaginate
helper, you can specify it on Application Helper (app/helpers/application_helper.rb):
module ApplicationHelper WillPaginate::ViewHelpers.pagination_options[:renderer] = FlowPagination::LinkRenderer end
Copyright © 2008 Iván “Mexpolk” Torres
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.