mirror of
https://github.com/kaminari/kaminari.git
synced 2022-11-09 13:44:37 -05:00
Use NextPage and PrevPage to generate link tags for rel=next and rel=prev
fixes #535, closes #509
This commit is contained in:
parent
f7977ec2a1
commit
a12d92a6af
2 changed files with 14 additions and 22 deletions
|
@ -117,13 +117,12 @@ module Kaminari
|
|||
# #-> <link rel="next" href="/items/page/3" /><link rel="prev" href="/items/page/1" />
|
||||
#
|
||||
def rel_next_prev_link_tags(scope, options = {})
|
||||
params = options.delete(:params) || {}
|
||||
param_name = options.delete(:param_name) || Kaminari.config.param_name
|
||||
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)
|
||||
|
||||
output = ""
|
||||
output << '<link rel="next" href="' + url_for(params.merge(param_name => scope.next_page, :only_path => true)) + '"/>' if scope.next_page
|
||||
output << '<link rel="prev" href="' + url_for(params.merge(param_name => scope.prev_page, :only_path => true)) + '"/>' if scope.prev_page
|
||||
|
||||
output << tag(:link, :rel => "next", :href => next_page.url) if scope.next_page
|
||||
output << tag(:link, :rel => "prev", :href => prev_page.url) if scope.prev_page
|
||||
output.html_safe
|
||||
end
|
||||
end
|
||||
|
|
|
@ -266,38 +266,31 @@ describe 'Kaminari::ActionViewExtension', :if => defined?(Rails) do
|
|||
|
||||
describe '#rel_next_prev_link_tags' do
|
||||
before do
|
||||
80.times {|i| User.create! :name => "user#{i}"}
|
||||
31.times {|i| User.create! :name => "user#{i}"}
|
||||
end
|
||||
|
||||
context 'the first page' do
|
||||
before do
|
||||
@users = User.page(1).per(25)
|
||||
end
|
||||
subject { helper.rel_next_prev_link_tags users, :params => {:controller => 'users', :action => 'index'} }
|
||||
|
||||
context 'the first page' do
|
||||
let(:users) { User.page(1).per(10) }
|
||||
|
||||
subject { helper.rel_next_prev_link_tags @users, :params => {:controller => 'users', :action => 'index'} }
|
||||
it { should_not match(/rel="prev"/) }
|
||||
it { should match(/rel="next"/) }
|
||||
it { should match(/\?page=2/) }
|
||||
end
|
||||
|
||||
context 'the middle page' do
|
||||
before do
|
||||
@users = User.page(3).per(25)
|
||||
end
|
||||
context 'the second page' do
|
||||
let(:users) { User.page(2).per(10) }
|
||||
|
||||
subject { helper.rel_next_prev_link_tags @users, :params => {:controller => 'users', :action => 'index'} }
|
||||
it { should match(/rel="prev"/) }
|
||||
it { should match(/\?page=2/) }
|
||||
it { should_not match(/\?page=1/) }
|
||||
it { should match(/rel="next"/) }
|
||||
it { should match(/\?page=4/) }
|
||||
it { should match(/\?page=3/) }
|
||||
end
|
||||
|
||||
context 'the last page' do
|
||||
before do
|
||||
@users = User.page(4).per(25)
|
||||
end
|
||||
let(:users) { User.page(4).per(10) }
|
||||
|
||||
subject { helper.rel_next_prev_link_tags @users, :params => {:controller => 'users', :action => 'index'} }
|
||||
it { should match(/rel="prev"/) }
|
||||
it { should match(/\?page=3"/) }
|
||||
it { should_not match(/rel="next"/) }
|
||||
|
|
Loading…
Reference in a new issue