Skip to content

Commit

Permalink
Merge pull request #119 from MITLibraries/gdt-172
Browse files Browse the repository at this point in the history
Update pagination according to mockupos
  • Loading branch information
matt-bernhardt authored Feb 23, 2024
2 parents 5c3f4d4 + 496974c commit 0d4a8c4
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 30 deletions.
16 changes: 10 additions & 6 deletions app/assets/stylesheets/partials/_pagination.scss
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
.pagination-container {
clear: both;
display: flex;
flex-flow: row nowrap;
justify-content: center;
margin-top: 3em;

.previous {
text-align: left;
}

.current {
text-align: center;
border-right: 1px solid black;
margin-right: 0.5em;
padding-right: 0.5em;
}

.next {
text-align: right;
border-left: 1px solid black;
margin-left: 0.5em;
padding-left: 0.5em;
}
}
8 changes: 4 additions & 4 deletions app/helpers/pagination_helper.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
module PaginationHelper
def next_url(query_params)
query_params[:page] = @pagination[:next]
link_to results_path(query_params), class: 'btn button-primary' do
"Next page #{content_tag(:span, '', class: 'fa fa-chevron-right')}".html_safe
link_to results_path(query_params), 'aria-label': 'Next page' do
'Next »'.html_safe
end
end

def prev_url(query_params)
query_params[:page] = @pagination[:prev]
link_to results_path(query_params), class: 'btn button-primary' do
"#{content_tag(:span, '', class: 'fa fa-chevron-left')} Previous page".html_safe
link_to results_path(query_params), 'aria-label': 'Previous page' do
'« Previous'.html_safe
end
end
end
3 changes: 2 additions & 1 deletion app/models/analyzer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ class Analyzer
def initialize(enhanced_query, response)
@pagination = {}
@pagination[:hits] = hits(response)
@pagination[:page] = enhanced_query[:page]
@pagination[:start] = ((enhanced_query[:page] - 1) * RESULTS_PER_PAGE) + 1
@pagination[:end] = [enhanced_query[:page] * RESULTS_PER_PAGE, hits(response)].min
@pagination[:prev] = enhanced_query[:page] - 1 if enhanced_query[:page] > 1
@pagination[:next] = next_page(enhanced_query[:page], @pagination[:hits])
end
Expand Down
14 changes: 7 additions & 7 deletions app/views/search/_pagination.html.erb
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
<% return if @pagination.nil? %>

<div class="gridband layout-3c pagination-container">
<div class="grid-item previous">
<nav class="pagination-container" aria-label="Pagination">
<div class="previous">
<% if @pagination[:prev] %>
<%= prev_url(@enhanced_query) %>
<% else %>
First page
First
<% end %>
</div>
<div class="grid-item current">Page <%= @pagination[:page] %> of <%= (@pagination[:hits] / 20) + 1 %></div>
<div class="grid-item next">
<div class="current"><%= @pagination[:start] %> - <%= @pagination[:end] %> of <%= @pagination[:hits] %></div>
<div class="next">
<% if @pagination[:next] %>
<%= next_url(@enhanced_query) %>
<% else %>
Last page
Last
<% end %>
</div>
</div>
</nav>
8 changes: 4 additions & 4 deletions test/helpers/pagination_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,29 @@ class PaginationHelperTest < ActionView::TestCase
test 'Next links for basic search' do
@pagination = { next: 12 }
query_params = { q: 'popcorn' }
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))
assert_equal('<a aria-label="Next page" href="/results?page=12&amp;q=popcorn">Next &raquo;</a>', next_url(query_params))
end

test 'Next links for advanced search' do
@pagination = { next: 12 }
query_params = { q: 'popcorn', title: 'titles are cool', contributors: 'yawn' }
assert_equal(
'<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>',
'<a aria-label="Next page" href="/results?contributors=yawn&amp;page=12&amp;q=popcorn&amp;title=titles+are+cool">Next &raquo;</a>',
next_url(query_params)
)
end

test 'Previous links for basic search' do
@pagination = { prev: 11 }
query_params = { q: 'popcorn' }
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))
assert_equal('<a aria-label="Previous page" href="/results?page=11&amp;q=popcorn">&laquo; Previous</a>', prev_url(query_params))
end

test 'Previous links for advanced search' do
@pagination = { prev: 11 }
query_params = { q: 'popcorn', title: 'titles are cool', contributors: 'yawn' }
assert_equal(
'<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>',
'<a aria-label="Previous page" href="/results?contributors=yawn&amp;page=11&amp;q=popcorn&amp;title=titles+are+cool">&laquo; Previous</a>',
prev_url(query_params)
)
end
Expand Down
31 changes: 23 additions & 8 deletions test/models/analyzer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

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

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

assert pagination.key?(:hits)
assert pagination.key?(:start)
assert pagination.key?(:end)
assert pagination.key?(:next)
assert pagination.key?(:page)

refute pagination.key?(:prev)

assert_equal 1, pagination[:start]
assert_equal 20, pagination[:end]
end

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

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

assert pagination.key?(:hits)
assert pagination.key?(:start)
assert pagination.key?(:end)
assert pagination.key?(:next)
assert pagination.key?(:prev)
assert pagination.key?(:page)

assert_equal 21, pagination[:start]
assert_equal 40, pagination[:end]
end

test 'analyzer pagination does not include last page value on last page of results' do
Analyzer.any_instance.stubs(:hits).returns(100)
hit_count = 95
Analyzer.any_instance.stubs(:hits).returns(hit_count)

mocking_hits_so_this_is_empty = {}
eq = {
q: 'data',
page: 28
page: 5
}

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

assert pagination.key?(:hits)
assert pagination.key?(:start)
assert pagination.key?(:end)
assert pagination.key?(:prev)
assert pagination.key?(:page)

assert_nil pagination[:next]
refute pagination[:next]

assert_equal 81, pagination[:start]
assert_equal hit_count, pagination[:end]
end
end

0 comments on commit 0d4a8c4

Please sign in to comment.