1
0
Fork 0
mirror of https://github.com/kaminari/kaminari.git synced 2022-11-09 13:44:37 -05:00

Refactored the path calculators in action_view_extension to expose the path_to_next_page and path_to_prev_page

This commit is contained in:
neilang 2015-04-27 16:33:59 +10:00 committed by Akira Matsuda
parent 1cdadd161e
commit bb3631aa83
2 changed files with 77 additions and 8 deletions

View file

@ -44,9 +44,9 @@ module Kaminari
# <span>At the Beginning</span>
# <% end %>
def link_to_previous_page(scope, name, options = {})
prev_page = Kaminari::Helpers::PrevPage.new self, options.reverse_merge(:current_page => scope.current_page)
prev_page = path_to_prev_page(scope, options)
link_to_if scope.prev_page.present?, name, prev_page.url, options.except(:params, :param_name).reverse_merge(:rel => 'prev') do
link_to_if prev_page, name, prev_page, options.except(:params, :param_name).reverse_merge(:rel => 'prev') do
yield if block_given?
end
end
@ -69,9 +69,9 @@ module Kaminari
# <span>No More Pages</span>
# <% end %>
def link_to_next_page(scope, name, options = {})
next_page = Kaminari::Helpers::NextPage.new self, options.reverse_merge(:current_page => scope.current_page)
next_page = path_to_next_page(scope, options)
link_to_if scope.next_page.present?, name, next_page.url, options.except(:params, :param_name).reverse_merge(:rel => 'next') do
link_to_if next_page, name, next_page, options.except(:params, :param_name).reverse_merge(:rel => 'next') do
yield if block_given?
end
end
@ -127,15 +127,43 @@ module Kaminari
# #-> <link rel="next" href="/items/page/3" /><link rel="prev" href="/items/page/1" />
#
def rel_next_prev_link_tags(scope, options = {})
next_page = Kaminari::Helpers::NextPage.new self, options.reverse_merge(:current_page => scope.current_page)
prev_page = Kaminari::Helpers::PrevPage.new self, options.reverse_merge(:current_page => scope.current_page)
next_page = path_to_next_page(scope, options)
prev_page = path_to_prev_page(scope, options)
output = String.new
output << tag(:link, :rel => "next", :href => next_page.url) if scope.next_page.present?
output << tag(:link, :rel => "prev", :href => prev_page.url) if scope.prev_page.present?
output << tag(:link, :rel => "next", :href => next_page) if next_page
output << tag(:link, :rel => "prev", :href => prev_page) if prev_page
output.html_safe
end
# A helper that calculates the path to the next page.
#
# ==== Examples
# Basic usage:
#
# <%= path_to_next_page @items %>
# #-> /items?page=2
#
# It will return `nil` if there is no next page.
def path_to_next_page(scope, options = {})
return nil unless scope.next_page.present?
Kaminari::Helpers::NextPage.new(self, options.reverse_merge(:current_page => scope.current_page)).url
end
# A helper that calculates the path to the previous page.
#
# ==== Examples
# Basic usage:
#
# <%= path_to_prev_page @items %>
# #-> /items
#
# It will return `nil` if there is no previous page.
def path_to_prev_page(scope, options = {})
return nil unless scope.prev_page.present?
Kaminari::Helpers::PrevPage.new(self, options.reverse_merge(:current_page => scope.current_page)).url
end
module PaginatorExtension
extend ActiveSupport::Concern

View file

@ -368,4 +368,45 @@ describe 'Kaminari::ActionViewExtension', :if => defined?(::Rails::Railtie) && d
it { should_not match(/rel="next"/) }
end
end
describe '#path_to_next_page' do
before do
2.times {|i| User.create! :name => "user#{i}"}
end
subject { helper.path_to_next_page users, :params => {:controller => 'users', :action => 'index'} }
context 'the first page' do
let(:users) { User.page(1).per(1) }
it { should eql '/users?page=2' }
end
context 'the last page' do
let(:users) { User.page(2).per(1) }
it { should be nil }
end
end
describe '#path_to_prev_page' do
before do
3.times {|i| User.create! :name => "user#{i}"}
end
subject { helper.path_to_prev_page users, :params => {:controller => 'users', :action => 'index'} }
context 'the first page' do
let(:users) { User.page(1).per(1) }
it { should be nil }
end
context 'the second page' do
let(:users) { User.page(2).per(1) }
it { should eql '/users' }
end
context 'the last page' do
let(:users) { User.page(3).per(1) }
it { should eql '/users?page=2' }
end
end
end