Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Add link_to_previous_page helper for sinatra #504

Merged
merged 1 commit into from

4 participants

@ikeay

I found that link_to_previous_page isn't provided for sinatra.
I implemented it.

@amatsuda amatsuda merged commit c347875 into from
@amatsuda
Owner

@ikeay OMG the patch looks perfect! :white_flower: Thank you so much! :metal::metal:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 2, 2014
  1. @ikeay
This page is out of date. Refresh to see the latest.
View
12 lib/kaminari/helpers/sinatra_helpers.rb
@@ -93,6 +93,18 @@ def paginate(scope, options = {}, &block)
paginator.to_s
end
+ def link_to_previous_page(scope, name, options = {})
+ params = options.delete(:params) || (Rack::Utils.parse_query(env['QUERY_STRING']).symbolize_keys rescue {})
+ param_name = options.delete(:param_name) || Kaminari.config.param_name
+ placeholder = options.delete(:placeholder)
+ query = params.merge(param_name => (scope.current_page - 1))
+ unless scope.first_page?
+ link_to name, env['PATH_INFO'] + (query.empty? ? '' : "?#{query.to_query}"), options.reverse_merge(:rel => 'previous')
+ else
+ placeholder
+ end
+ end
+
# A simple "Twitter like" pagination link that creates a link to the next page.
# Works on Sinatra.
#
View
53 spec/helpers/sinatra_helpers_spec.rb
@@ -12,6 +12,17 @@
</div>
EOT
+ ERB_TEMPLATE_FOR_PREVIOUS_PAGE = <<EOT
+ <div>
+ <ul>
+ <% @users.each do |user| %>
+ <li class="user_info"><%= user.id %></li>
+ <% end %>
+ </ul>
+ <%= link_to_previous_page(@users, "Previous!", {:id => 'previous_page_link'}.merge(@options || {})) %>
+ </div>
+EOT
+
ERB_TEMPLATE_FOR_NEXT_PAGE = <<EOT
<div>
<ul>
@@ -125,6 +136,48 @@
end
end
+ describe '#link_to_previous_page' do
+ before do
+ mock_app do
+ register Kaminari::Helpers::SinatraHelpers
+ get '/users' do
+ @page = params[:page] || 2
+ @users = User.page(@page)
+ erb ERB_TEMPLATE_FOR_PREVIOUS_PAGE
+ end
+
+ get '/users_placeholder' do
+ @page = params[:page] || 2
+ @options = {:placeholder => %{<span id='no_previous_page'>No Previous Page</span>}}
+ @users = User.page(@page)
+ erb ERB_TEMPLATE_FOR_PREVIOUS_PAGE
+ end
+ end
+ end
+
+ context 'having more page' do
+ it 'should have a more page link' do
+ get '/users'
+ last_document.search('a#previous_page_link').should be_present
+ last_document.search('a#previous_page_link').text.should match(/Previous!/)
+ end
+ end
+
+ context 'the first page' do
+ it 'should not have a more page link' do
+ get '/users?page=1'
+ last_document.search('a#previous_page_link').should be_empty
+ end
+
+ it 'should have a no more page notation using placeholder' do
+ get '/users_placeholder?page=1'
+ last_document.search('a#previous_page_link').should be_empty
+ last_document.search('span#no_previous_page').should be_present
+ last_document.search('span#no_previous_page').text.should match(/No Previous Page/)
+ end
+ end
+ end
+
describe '#link_to_next_page' do
before do
mock_app do
Something went wrong with that request. Please try again.