Skip to content

Commit

Permalink
URIs are always joined with forward slash (#11)
Browse files Browse the repository at this point in the history
* Use Rack::Request#path instead of File.join
* Add a few tests for Pagy::Frontend
  • Loading branch information
bquorning authored and ddnexus committed May 27, 2018
1 parent 4ea7e9f commit 144c997
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ GEM
pathutil (0.16.1)
forwardable-extended (~> 2.6)
public_suffix (2.0.5)
rack (2.0.5)
rake (10.5.0)
rb-fsevent (0.10.3)
rb-inotify (0.9.10)
Expand Down Expand Up @@ -273,6 +274,7 @@ DEPENDENCIES
memory_profiler
minitest (~> 5.0)
pagy!
rack
rake (~> 10.0)
slim

Expand Down
2 changes: 1 addition & 1 deletion lib/pagy/frontend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def pagy_info(pagy)

# this works with all Rack-based frameworks (Sinatra, Padrino, Rails, ...)
def pagy_url_for(n)
url = File.join(request.script_name.to_s, request.path_info)
url = request.path
params = request.GET.merge('page'.freeze => n.to_s)
url << '?' << Rack::Utils.build_nested_query(pagy_get_params(params))
end
Expand Down
1 change: 1 addition & 0 deletions pagy.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Gem::Specification.new do |s|
s.add_development_dependency 'bundler', '~> 1.16'
s.add_development_dependency 'rake', '~> 10.0'
s.add_development_dependency 'minitest', '~> 5.0'
s.add_development_dependency 'rack'
s.add_development_dependency 'slim'
s.add_development_dependency 'haml'
s.add_development_dependency 'benchmark-ips'
Expand Down
72 changes: 72 additions & 0 deletions test/frontend_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
require 'test_helper'
require 'rack'

class FrontendTest < Minitest::Test

class TestView
include Pagy::Frontend

def request
Rack::Request.new('SCRIPT_NAME' => '/foo')
end
end

def setup
@frontend = TestView.new
@array = (1..103).to_a.extend(Pagy::Array::PageMethod)
end

def test_pagy_nav_page_1
pagy, paged = @array.pagy(1)

assert_equal(
'<nav class="pagy-nav pagination" role="navigation" aria-label="pager">' \
'<span class="page prev disabled">&lsaquo;&nbsp;Prev</span> ' \
'<span class="page active">1</span> ' \
'<span class="page"><a href="/foo?page=2" rel="next">2</a></span> ' \
'<span class="page"><a href="/foo?page=3">3</a></span> ' \
'<span class="page"><a href="/foo?page=4">4</a></span> ' \
'<span class="page"><a href="/foo?page=5">5</a></span> ' \
'<span class="page"><a href="/foo?page=6">6</a></span> ' \
'<span class="page next"><a href="/foo?page=2" rel="next" aria-label="next">Next&nbsp;&rsaquo;</a></span>' \
'</nav>',
@frontend.pagy_nav(pagy)
)
end

def test_pagy_nav_page_3
pagy, paged = @array.pagy(3)

assert_equal(
'<nav class="pagy-nav pagination" role="navigation" aria-label="pager">' \
'<span class="page prev"><a href="/foo?page=2" rel="prev" aria-label="previous">&lsaquo;&nbsp;Prev</a></span> ' \
'<span class="page"><a href="/foo?page=1">1</a></span> ' \
'<span class="page"><a href="/foo?page=2" rel="prev">2</a></span> ' \
'<span class="page active">3</span> ' \
'<span class="page"><a href="/foo?page=4" rel="next">4</a></span> ' \
'<span class="page"><a href="/foo?page=5">5</a></span> ' \
'<span class="page"><a href="/foo?page=6">6</a></span> ' \
'<span class="page next"><a href="/foo?page=4" rel="next" aria-label="next">Next&nbsp;&rsaquo;</a></span>' \
'</nav>',
@frontend.pagy_nav(pagy)
)
end

def test_pagy_nav_page_6
pagy, paged = @array.pagy(6)

assert_equal(
'<nav class="pagy-nav pagination" role="navigation" aria-label="pager">' \
'<span class="page prev"><a href="/foo?page=5" rel="prev" aria-label="previous">&lsaquo;&nbsp;Prev</a></span> ' \
'<span class="page"><a href="/foo?page=1">1</a></span> ' \
'<span class="page"><a href="/foo?page=2">2</a></span> ' \
'<span class="page"><a href="/foo?page=3">3</a></span> ' \
'<span class="page"><a href="/foo?page=4">4</a></span> ' \
'<span class="page"><a href="/foo?page=5" rel="prev">5</a></span> ' \
'<span class="page active">6</span> ' \
'<span class="page next disabled">Next&nbsp;&rsaquo;</span>' \
'</nav>',
@frontend.pagy_nav(pagy)
)
end
end

0 comments on commit 144c997

Please sign in to comment.