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

Merge branch 'master' of https://github.com/joe1chen/kaminari into joe1chen-master

Conflicts:
	spec/helpers/action_view_extension_spec.rb
This commit is contained in:
Akira Matsuda 2012-05-15 22:40:50 +09:00
commit e67057eed3
2 changed files with 77 additions and 1 deletions

View file

@ -105,5 +105,45 @@ module Kaminari
t('helpers.page_entries_info.more_pages.display_entries', :entry_name => entry_name, :first => first, :last => last, :total => collection.total_count)
end.html_safe
end
# Renders rel="next" and rel="prev" links to be used in the head.
#
# ==== Examples
# Basic usage:
#
# In head:
# <head>
# <title>My Website</title>
# <%= yield :head %>
# </head>
#
# Somewhere in body:
# <% content_for :head do %>
# <%= rel_next_prev_link_tags @items %>
# <% end %>
#
# #-> <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
output = ""
if !scope.first_page? && !scope.last_page?
# If not first and not last, then output both links.
output << '<link rel="next" href="' + url_for(params.merge(param_name => (scope.current_page + 1))) + '"/>'
output << '<link rel="prev" href="' + url_for(params.merge(param_name => (scope.current_page - 1))) + '"/>'
elsif scope.first_page?
# If first page, add next link unless last page.
output << '<link rel="next" href="' + url_for(params.merge(param_name => (scope.current_page + 1))) + '"/>' unless scope.last_page?
else
# If last page, add prev link unless first page.
output << '<link rel="prev" href="' + url_for(params.merge(param_name => (scope.current_page - 1))) + '"/>' unless scope.first_page?
end
output.html_safe
end
end
end

View file

@ -210,4 +210,40 @@ describe 'Kaminari::ActionViewExtension' do
end
end
end
end
describe '#rel_next_prev_link_tags' do
before do
75.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'} }
it { should be_a String }
it { should match /rel="next"/ }
it { should_not match /rel="prev"/ }
end
context 'the middle page' do
before do
@users = User.page(2).per(25)
end
subject { helper.rel_next_prev_link_tags @users, :params => {:controller => 'users', :action => 'index'} }
it { should be_a String }
it { should match /rel="next"/ }
it { should match /rel="prev"/ }
end
context 'the last page' do
before do
@users = User.page(3).per(25)
end
subject { helper.rel_next_prev_link_tags @users, :params => {:controller => 'users', :action => 'index'} }
it { should be_a String }
it { should_not match /rel="next"/ }
it { should match /rel="prev"/ }
end
end
end