Skip to content

Commit 0d4a8c4

Browse files
Merge pull request #119 from MITLibraries/gdt-172
Update pagination according to mockupos
2 parents 5c3f4d4 + 496974c commit 0d4a8c4

File tree

6 files changed

+50
-30
lines changed

6 files changed

+50
-30
lines changed
Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
.pagination-container {
2+
clear: both;
3+
display: flex;
4+
flex-flow: row nowrap;
5+
justify-content: center;
26
margin-top: 3em;
37

48
.previous {
5-
text-align: left;
6-
}
7-
8-
.current {
9-
text-align: center;
9+
border-right: 1px solid black;
10+
margin-right: 0.5em;
11+
padding-right: 0.5em;
1012
}
1113

1214
.next {
13-
text-align: right;
15+
border-left: 1px solid black;
16+
margin-left: 0.5em;
17+
padding-left: 0.5em;
1418
}
1519
}

app/helpers/pagination_helper.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
module PaginationHelper
22
def next_url(query_params)
33
query_params[:page] = @pagination[:next]
4-
link_to results_path(query_params), class: 'btn button-primary' do
5-
"Next page #{content_tag(:span, '', class: 'fa fa-chevron-right')}".html_safe
4+
link_to results_path(query_params), 'aria-label': 'Next page' do
5+
'Next »'.html_safe
66
end
77
end
88

99
def prev_url(query_params)
1010
query_params[:page] = @pagination[:prev]
11-
link_to results_path(query_params), class: 'btn button-primary' do
12-
"#{content_tag(:span, '', class: 'fa fa-chevron-left')} Previous page".html_safe
11+
link_to results_path(query_params), 'aria-label': 'Previous page' do
12+
'« Previous'.html_safe
1313
end
1414
end
1515
end

app/models/analyzer.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ class Analyzer
66
def initialize(enhanced_query, response)
77
@pagination = {}
88
@pagination[:hits] = hits(response)
9-
@pagination[:page] = enhanced_query[:page]
9+
@pagination[:start] = ((enhanced_query[:page] - 1) * RESULTS_PER_PAGE) + 1
10+
@pagination[:end] = [enhanced_query[:page] * RESULTS_PER_PAGE, hits(response)].min
1011
@pagination[:prev] = enhanced_query[:page] - 1 if enhanced_query[:page] > 1
1112
@pagination[:next] = next_page(enhanced_query[:page], @pagination[:hits])
1213
end
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
<% return if @pagination.nil? %>
22

3-
<div class="gridband layout-3c pagination-container">
4-
<div class="grid-item previous">
3+
<nav class="pagination-container" aria-label="Pagination">
4+
<div class="previous">
55
<% if @pagination[:prev] %>
66
<%= prev_url(@enhanced_query) %>
77
<% else %>
8-
First page
8+
First
99
<% end %>
1010
</div>
11-
<div class="grid-item current">Page <%= @pagination[:page] %> of <%= (@pagination[:hits] / 20) + 1 %></div>
12-
<div class="grid-item next">
11+
<div class="current"><%= @pagination[:start] %> - <%= @pagination[:end] %> of <%= @pagination[:hits] %></div>
12+
<div class="next">
1313
<% if @pagination[:next] %>
1414
<%= next_url(@enhanced_query) %>
1515
<% else %>
16-
Last page
16+
Last
1717
<% end %>
1818
</div>
19-
</div>
19+
</nav>

test/helpers/pagination_helper_test.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,29 @@ class PaginationHelperTest < ActionView::TestCase
66
test 'Next links for basic search' do
77
@pagination = { next: 12 }
88
query_params = { q: 'popcorn' }
9-
assert_equal('<a class="btn button-primary" href="/results?page=12&amp;q=popcorn">Next page <span class="fa fa-chevron-right"></span></a>', next_url(query_params))
9+
assert_equal('<a aria-label="Next page" href="/results?page=12&amp;q=popcorn">Next &raquo;</a>', next_url(query_params))
1010
end
1111

1212
test 'Next links for advanced search' do
1313
@pagination = { next: 12 }
1414
query_params = { q: 'popcorn', title: 'titles are cool', contributors: 'yawn' }
1515
assert_equal(
16-
'<a class="btn button-primary" href="/results?contributors=yawn&amp;page=12&amp;q=popcorn&amp;title=titles+are+cool">Next page <span class="fa fa-chevron-right"></span></a>',
16+
'<a aria-label="Next page" href="/results?contributors=yawn&amp;page=12&amp;q=popcorn&amp;title=titles+are+cool">Next &raquo;</a>',
1717
next_url(query_params)
1818
)
1919
end
2020

2121
test 'Previous links for basic search' do
2222
@pagination = { prev: 11 }
2323
query_params = { q: 'popcorn' }
24-
assert_equal('<a class="btn button-primary" href="/results?page=11&amp;q=popcorn"><span class="fa fa-chevron-left"></span> Previous page</a>', prev_url(query_params))
24+
assert_equal('<a aria-label="Previous page" href="/results?page=11&amp;q=popcorn">&laquo; Previous</a>', prev_url(query_params))
2525
end
2626

2727
test 'Previous links for advanced search' do
2828
@pagination = { prev: 11 }
2929
query_params = { q: 'popcorn', title: 'titles are cool', contributors: 'yawn' }
3030
assert_equal(
31-
'<a class="btn button-primary" href="/results?contributors=yawn&amp;page=11&amp;q=popcorn&amp;title=titles+are+cool"><span class="fa fa-chevron-left"></span> Previous page</a>',
31+
'<a aria-label="Previous page" href="/results?contributors=yawn&amp;page=11&amp;q=popcorn&amp;title=titles+are+cool">&laquo; Previous</a>',
3232
prev_url(query_params)
3333
)
3434
end

test/models/analyzer_test.rb

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
class AnalyzerTest < ActiveSupport::TestCase
44
test 'analyzer pagination does not include previous page value on first page of results' do
5-
Analyzer.any_instance.stubs(:hits).returns(100)
5+
hit_count = 95
6+
Analyzer.any_instance.stubs(:hits).returns(hit_count)
67
mocking_hits_so_this_is_empty = {}
78

89
eq = {
@@ -14,14 +15,19 @@ class AnalyzerTest < ActiveSupport::TestCase
1415
pagination = Analyzer.new(eq, mocking_hits_so_this_is_empty).pagination
1516

1617
assert pagination.key?(:hits)
18+
assert pagination.key?(:start)
19+
assert pagination.key?(:end)
1720
assert pagination.key?(:next)
18-
assert pagination.key?(:page)
1921

2022
refute pagination.key?(:prev)
23+
24+
assert_equal 1, pagination[:start]
25+
assert_equal 20, pagination[:end]
2126
end
2227

2328
test 'analyzer pagination includes all values when not on first or last page of results' do
24-
Analyzer.any_instance.stubs(:hits).returns(100)
29+
hit_count = 95
30+
Analyzer.any_instance.stubs(:hits).returns(hit_count)
2531
mocking_hits_so_this_is_empty = {}
2632

2733
eq = {
@@ -33,26 +39,35 @@ class AnalyzerTest < ActiveSupport::TestCase
3339
pagination = Analyzer.new(eq, mocking_hits_so_this_is_empty).pagination
3440

3541
assert pagination.key?(:hits)
42+
assert pagination.key?(:start)
43+
assert pagination.key?(:end)
3644
assert pagination.key?(:next)
3745
assert pagination.key?(:prev)
38-
assert pagination.key?(:page)
46+
47+
assert_equal 21, pagination[:start]
48+
assert_equal 40, pagination[:end]
3949
end
4050

4151
test 'analyzer pagination does not include last page value on last page of results' do
42-
Analyzer.any_instance.stubs(:hits).returns(100)
52+
hit_count = 95
53+
Analyzer.any_instance.stubs(:hits).returns(hit_count)
4354

4455
mocking_hits_so_this_is_empty = {}
4556
eq = {
4657
q: 'data',
47-
page: 28
58+
page: 5
4859
}
4960

5061
pagination = Analyzer.new(eq, mocking_hits_so_this_is_empty).pagination
5162

5263
assert pagination.key?(:hits)
64+
assert pagination.key?(:start)
65+
assert pagination.key?(:end)
5366
assert pagination.key?(:prev)
54-
assert pagination.key?(:page)
5567

56-
assert_nil pagination[:next]
68+
refute pagination[:next]
69+
70+
assert_equal 81, pagination[:start]
71+
assert_equal hit_count, pagination[:end]
5772
end
5873
end

0 commit comments

Comments
 (0)