From bd2f1b5e231e9f2f6f9867edb36cafb56e3c27b1 Mon Sep 17 00:00:00 2001 From: Akira Matsuda Date: Sat, 5 Feb 2011 23:05:40 +0900 Subject: [PATCH] the first implementation of pagination helper --- lib/kaminari/helpers.rb | 40 +++++++++++++++++++++ lib/kaminari/railtie.rb | 3 ++ spec/controllers/application_helper_spec.rb | 21 +++++++++++ 3 files changed, 64 insertions(+) create mode 100644 lib/kaminari/helpers.rb create mode 100644 spec/controllers/application_helper_spec.rb diff --git a/lib/kaminari/helpers.rb b/lib/kaminari/helpers.rb new file mode 100644 index 0000000..5c8e5cc --- /dev/null +++ b/lib/kaminari/helpers.rb @@ -0,0 +1,40 @@ +module Kaminari + module Helpers + def paginate(scope, options = {}, &block) + prev_label, next_label, left, window, right, truncate, style = (options[:prev] || '« Prev'.html_safe), (options[:next] || 'Next »'.html_safe), (options[:left] || 2), (options[:window] || 5), (options[:right] || 2), (options[:truncate] || '...'), (options[:style] || 'page') + current_page, num_pages = scope.current_page, scope.num_pages + + content_tag :div, :class => 'pagination' do + ''.html_safe.tap do |html| + # prev_link + html << link_to_if((current_page > 1), prev_label, url_for(:page => current_page - 1), :class => 'prev', :rel => 'prev') do + content_tag :span, prev_label, :class => 'prev' + end << "\n" + + # page links + truncated = false + (1..num_pages).each do |i| + html << if (i <= left) || ((num_pages - i) < right) || ((i - current_page).abs < window) + truncated = false + content_tag :span, :class => "#{style}#{i == current_page ? ' current' : ''}" do + if i == 1 + #TODO pageが1だったらパラメーターからpageを取り除く + link_to_unless_current i.to_s, :page => i + else + link_to_unless_current i.to_s, :page => i + end + end + else + content_tag(:span, truncate, :class => style).tap { truncated = true } unless truncated + end << "\n" + end + + # next_link + html << link_to_if((current_page < num_pages), next_label, url_for(:page => current_page + 1), :class => 'next', :rel => 'next') do + content_tag :span, next_label, :class => 'next' + end + end + end + end + end +end diff --git a/lib/kaminari/railtie.rb b/lib/kaminari/railtie.rb index 092e9f1..6537197 100644 --- a/lib/kaminari/railtie.rb +++ b/lib/kaminari/railtie.rb @@ -1,11 +1,14 @@ require 'rails' require 'active_record' +require 'action_view' require File.join(File.dirname(__FILE__), 'active_record') +require File.join(File.dirname(__FILE__), 'helpers') module Kaminari class Railtie < ::Rails::Railtie initializer 'paginatablize' do |app| ::ActiveRecord::Base.send :include, Kaminari::ActiveRecord + ::ActionView::Base.send :include, Kaminari::Helpers end end end diff --git a/spec/controllers/application_helper_spec.rb b/spec/controllers/application_helper_spec.rb new file mode 100644 index 0000000..c88470b --- /dev/null +++ b/spec/controllers/application_helper_spec.rb @@ -0,0 +1,21 @@ +require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') + +class ApplicationController < ActionController::Base; end +class UsersController < ApplicationController + def index + @users = User.page params[:page] + render :inline => '<%= paginate @users %>' + end +end + +describe UsersController, 'pagination' do + render_views + before do + 1.upto(30) {|i| User.create! :name => "user#{'%02d' % i}" } + end + + it 'renders' do + get :index + response.body.should =~ /pagination/ + end +end