diff --git a/lib/kaminari/helpers/action_view_extension.rb b/lib/kaminari/helpers/action_view_extension.rb index cd157eb..023e3f1 100644 --- a/lib/kaminari/helpers/action_view_extension.rb +++ b/lib/kaminari/helpers/action_view_extension.rb @@ -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: + # + # My Website + # <%= yield :head %> + # + # + # Somewhere in body: + # <% content_for :head do %> + # <%= rel_next_prev_link_tags @items %> + # <% end %> + # + # #-> + # + 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 << '' + output << '' + elsif scope.first_page? + # If first page, add next link unless last page. + output << '' unless scope.last_page? + else + # If last page, add prev link unless first page. + output << '' unless scope.first_page? + end + + output.html_safe + end + end end diff --git a/spec/helpers/action_view_extension_spec.rb b/spec/helpers/action_view_extension_spec.rb index 662dad4..159c447 100644 --- a/spec/helpers/action_view_extension_spec.rb +++ b/spec/helpers/action_view_extension_spec.rb @@ -210,4 +210,40 @@ describe 'Kaminari::ActionViewExtension' do end end end -end \ No newline at end of file + + 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