Skip to content

Commit

Permalink
rename sinatra_helpers, add bacis tests
Browse files Browse the repository at this point in the history
  • Loading branch information
udzura committed Nov 22, 2011
1 parent 1da155b commit 9dec2ad
Show file tree
Hide file tree
Showing 7 changed files with 206 additions and 19 deletions.
4 changes: 4 additions & 0 deletions kaminari.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,8 @@ Gem::Specification.new do |s|
s.add_development_dependency 'steak', ['>= 0']
s.add_development_dependency 'capybara', ['>= 0']
s.add_development_dependency 'database_cleaner', ['>= 0']
s.add_development_dependency 'padrino-helpers', ['~> 0.10']
s.add_development_dependency 'rack-test', ['>= 0']
s.add_development_dependency 'sinatra-contrib', ['~> 1.3']
s.add_development_dependency 'nokogiri', ['>= 0']
end
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

require 'padrino-helpers'
module Kaminari::Helpers
module SinatraHelper
module SinatraHelpers
class << self
def registered(app)
app.register Padrino::Helpers
Expand Down Expand Up @@ -45,15 +45,6 @@ def params
end

module HelperMethods
# A quick backport from Rails' link_to_unless helper
def link_to_unless(condition, *args, &blk)
unless condition
link_to *args
else
block_given? ? blk.call : nil
end
end

# A helper that renders the pagination links - for Sinatra.
#
# <%= paginate @articles %>
Expand All @@ -63,7 +54,7 @@ def link_to_unless(condition, *args, &blk)
# * <tt>:outer_window</tt> - The "outer window" size (0 by default).
# * <tt>:left</tt> - The "left outer window" size (0 by default).
# * <tt>:right</tt> - The "right outer window" size (0 by default).
# * <tt>:params</tt> - url_for parameters for the links (:controller, :action, etc.)
# * <tt>:params</tt> - url_for parameters for the links (:id, :locale, etc.)
# * <tt>:param_name</tt> - parameter name for page number in the links (:page by default)
# * <tt>:remote</tt> - Ajax? (false by default)
# * <tt>:ANY_OTHER_VALUES</tt> - Any other hash key & values would be directly passed into each tag as :locals value.
Expand All @@ -90,17 +81,19 @@ def paginate(scope, options = {}, &block)
# <%= link_to_next_page @items, 'Next Page', :remote => true %>
#
# By default, it renders nothing if there are no more results on the next page.
# You can customize this output by passing a block.
# You can customize this output by passing a parameter <tt>:placeholder</tt>.
#
# <%= link_to_next_page @items, 'Next Page', :placeholder => %{<span>No More Pages</span>} %>
#
# <%= link_to_next_page @users, 'Next Page' do %>
# <span>No More Pages</span>
# <% end %>
def link_to_next_page(scope, name, options = {}, &block)
def link_to_next_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))
link_to_unless scope.last_page?, name, env['PATH_INFO'] + (query.empty? ? '' : "?#{query.to_query}"), options.merge(:rel => 'next') do
block.call; nil if block
unless scope.last_page?
link_to name, env['PATH_INFO'] + (query.empty? ? '' : "?#{query.to_query}"), options.merge(:rel => 'next')
else
placeholder
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/kaminari/sinatra.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'kaminari'
module Kaminari
module Helpers
autoload :SinatraHelper, 'kaminari/helpers/sinatra_helper'
autoload :SinatraHelpers, 'kaminari/helpers/sinatra_helpers'
end
end
Kaminari::Hooks.init!
Expand Down
1 change: 1 addition & 0 deletions spec/acceptance/users_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

feature 'Users' do
background do
User.delete_all
1.upto(100) {|i| User.create! :name => "user#{'%03d' % i}" }
end
scenario 'navigating by pagination links' do
Expand Down
1 change: 1 addition & 0 deletions spec/helpers/action_view_extension_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

describe 'Kaminari::ActionViewExtension' do
before do
User.delete_all
50.times {|i| User.create! :name => "user#{i}"}
end
describe '#paginate' do
Expand Down
174 changes: 174 additions & 0 deletions spec/helpers/sinatra_helpers_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
require File.expand_path('../spec_helper', File.dirname(__FILE__))
require File.expand_path('../spec_helper_for_sinatra', File.dirname(__FILE__))

ERB_TEMPLATE_FOR_PAGINATE = <<EOT
<div>
<ul>
<% @users.each do |user| %>
<li class="user_info"><%= user.id %></li>
<% end %>
</ul>
<%= paginate @users, @options %>
</div>
EOT

ERB_TEMPLATE_FOR_NEXT_PAGE = <<EOT
<div>
<ul>
<% @users.each do |user| %>
<li class="user_info"><%= user.id %></li>
<% end %>
</ul>
<%= link_to_next_page(@users, "Next!", {:id => 'next_page_link'}.merge(@options || {})) %>
</div>
EOT

describe 'Kaminari::Helpers::SinatraHelper' do
before do
50.times {|i| User.create! :name => "user#{i}"}
end

describe '#paginate' do
before do
mock_app do
register Kaminari::Helpers::SinatraHelpers
get '/users' do
@page = params[:page] || 1
@users = User.page(@page)
@options = {}
erb ERB_TEMPLATE_FOR_PAGINATE
end
end
end

context 'normal paginations with Sinatra' do
before { get '/users' }

it 'should have a navigation tag' do
last_document.search('nav.pagination').should_not be_empty
end

it 'should have pagination links' do
last_document.search('.page a').should have_at_least(1).items
last_document.search('.next a').should have_at_least(1).items
last_document.search('.last a').should have_at_least(1).items
end

it 'should point to current page' do
last_document.search('.current').text.should match /1/

get '/users?page=2'
last_document.search('.current').text.should match /2/
end

it 'should load 25 users' do
last_document.search('li.user_info').should have(25).items
end

it 'should preserve params' do
get '/users?foo=bar'
last_document.search('.page a').should(be_all do |elm|
elm.attribute('href').value =~ /foo=bar/
end)
end
end

context 'optional paginations with Sinatra' do
it 'should have 5 windows with 1 gap' do
mock_app do
register Kaminari::Helpers::SinatraHelpers
get '/users' do
@page = params[:page] || 1
@users = User.page(@page).per(5)
@options = {}
erb ERB_TEMPLATE_FOR_PAGINATE
end
end

get '/users'
last_document.search('.page').should have(6).items
last_document.search('.gap').should have(1).item
end

it 'should controll the inner window size' do
mock_app do
register Kaminari::Helpers::SinatraHelpers
get '/users' do
@page = params[:page] || 1
@users = User.page(@page).per(3)
@options = {:window => 10}
erb ERB_TEMPLATE_FOR_PAGINATE
end
end

get '/users'
last_document.search('.page').should have(12).items
last_document.search('.gap').should have(1).item
end

it 'should specify a page param name' do
mock_app do
register Kaminari::Helpers::SinatraHelpers
get '/users' do
@page = params[:page] || 1
@users = User.page(@page).per(3)
@options = {:param_name => :user_page}
erb ERB_TEMPLATE_FOR_PAGINATE
end
end

get '/users'
last_document.search('.page a').should(be_all do |elm|
elm.attribute('href').value =~ /user_page=\d+/
end)
end
end
end

describe '#link_to_next_page' do
before do
mock_app do
register Kaminari::Helpers::SinatraHelpers
get '/users' do
@page = params[:page] || 1
@users = User.page(@page)
erb ERB_TEMPLATE_FOR_NEXT_PAGE
end

get '/users_placeholder' do
@page = params[:page] || 1
@options = {:placeholder => %{<span id='no_next_page'>No Next Page</span>}}
@users = User.page(@page)
erb ERB_TEMPLATE_FOR_NEXT_PAGE
end
end
end

context 'having more page' do
it 'should have a more page link' do
get '/users'
last_document.search('a#next_page_link').should be_present
last_document.search('a#next_page_link').text.should match /Next!/
end
end

context 'the last page' do
before do
User.delete_all
50.times {|i| User.create! :name => "user#{i}"}
end

it 'should not have a more page link' do
get '/users?page=2'
last_document.search('a#next_page_link').should be_empty
end

it 'should have a no more page notation using placeholder' do
get '/users_placeholder?page=2'
last_document.search('a#next_page_link').should be_empty
last_document.search('span#no_next_page').should be_present
last_document.search('span#no_next_page').text.should match /No Next Page/
end
end
end
end
14 changes: 14 additions & 0 deletions spec/spec_helper_for_sinatra.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require 'sinatra/base'
require 'kaminari/sinatra'
require 'rack/test'
require 'sinatra/test_helpers'

RSpec.configure do |config|
config.include Rack::Test::Methods
config.include Sinatra::TestHelpers
end

require 'nokogiri'
def last_document
Nokogiri::HTML(last_response.body)
end

0 comments on commit 9dec2ad

Please sign in to comment.