mirror of
https://github.com/kaminari/kaminari.git
synced 2022-11-09 13:44:37 -05:00
the first implementation of pagination helper
This commit is contained in:
parent
bfa1449f16
commit
bd2f1b5e23
3 changed files with 64 additions and 0 deletions
40
lib/kaminari/helpers.rb
Normal file
40
lib/kaminari/helpers.rb
Normal file
|
@ -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
|
|
@ -1,11 +1,14 @@
|
||||||
require 'rails'
|
require 'rails'
|
||||||
require 'active_record'
|
require 'active_record'
|
||||||
|
require 'action_view'
|
||||||
require File.join(File.dirname(__FILE__), 'active_record')
|
require File.join(File.dirname(__FILE__), 'active_record')
|
||||||
|
require File.join(File.dirname(__FILE__), 'helpers')
|
||||||
|
|
||||||
module Kaminari
|
module Kaminari
|
||||||
class Railtie < ::Rails::Railtie
|
class Railtie < ::Rails::Railtie
|
||||||
initializer 'paginatablize' do |app|
|
initializer 'paginatablize' do |app|
|
||||||
::ActiveRecord::Base.send :include, Kaminari::ActiveRecord
|
::ActiveRecord::Base.send :include, Kaminari::ActiveRecord
|
||||||
|
::ActionView::Base.send :include, Kaminari::Helpers
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
21
spec/controllers/application_helper_spec.rb
Normal file
21
spec/controllers/application_helper_spec.rb
Normal file
|
@ -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
|
Loading…
Reference in a new issue