Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding option for single page count #3315

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<div class="search-context page-links">
<%= link_to_previous_document %> |
<%= link_to_previous_document %> <%= display_separator %>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we put the conditional for total == 1 here, then we can avoid checking it several times in the component class. I think that'll make the code simpler to read.


<%= item_page_entry_info %> |
<%= item_page_entry_info %> <%= display_separator %>

<%= link_to_next_document %>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,52 @@ def initialize(search_context:, search_session:, current_document:)
end

def render?
@search_context.present? && (@search_context[:prev] || @search_context[:next]) && (@search_session['document_id'] == @current_document_id)
return false unless @search_session['document_id'] == @current_document_id

return true if total == 1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe we need to check for @search_context here. Otherwise it will display on pages with no search context.


@search_context.present? && (@search_context[:prev] || @search_context[:next])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this effectively the same as:

Suggested change
return true if total == 1
@search_context.present? && (@search_context[:prev] || @search_context[:next])
@search_context.present?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure, because maybe you can have a search_context but not have a :prev or :next?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right – my guess is that case (you have a context, but no prev or next) is the same thing as having a single result. But just a guess!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Separately I have to remove the "|" separating the different sections, so that will be another commit.

end

def display_separator
jcoyne marked this conversation as resolved.
Show resolved Hide resolved
'|' unless total == 1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would make this display separator a translation.

Copy link
Author

@hudajkhan hudajkhan Sep 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have tried to do that in the latest commit.

end

##
# Displays "showing X of Y items" message.
#
# @return [String]
def item_page_entry_info
t('blacklight.search.entry_pagination_info.other', current: number_with_delimiter(count),
t('blacklight.search.entry_pagination_info.other', current: current_count,
total: number_with_delimiter(total),
count: total).html_safe
end

def link_to_previous_document(previous_document = nil, classes: 'previous', **link_opts)
return if total == 1

previous_document ||= @search_context[:prev]
link_opts = session_tracking_params(previous_document, count - 1, per_page: per_page, search_id: search_id).merge(class: classes, rel: 'prev').merge(link_opts)

link_to_unless previous_document.nil?, raw(t('views.pagination.previous')), url_for_document(previous_document), link_opts do
tag.span raw(t('views.pagination.previous')), class: 'previous'
end
end

def link_to_next_document(next_document = nil, classes: 'next', **link_opts)
return if total == 1

next_document ||= @search_context[:next]
link_opts = session_tracking_params(next_document, count + 1, per_page: per_page, search_id: search_id).merge(class: classes, rel: 'next').merge(link_opts)
link_to_unless next_document.nil?, raw(t('views.pagination.next')), url_for_document(next_document), link_opts do
tag.span raw(t('views.pagination.next')), class: 'next'
end
end

def current_count
total == 1 ? total : number_with_delimiter(count)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need a conditional here?

Suggested change
total == 1 ? total : number_with_delimiter(count)
number_with_delimiter(count)

end

private

def count
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@
end
end

context 'when there is exactly one search result with no next or previous document' do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if someone goes directly to a show page without any session history. Will they still see this?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After the most recent changes, I went directly to an item page url in a separate browser and that did not show the "1 of 1" content.

let(:search_context) { {} }
let(:search_session) { { 'document_id' => current_document_id, 'total' => '1' } }

it "renders single page count" do
expect(render.to_html).to include '<strong>1</strong> of <strong>1</strong>'
expect(render.to_html).not_to include '|'
expect(render.css('span.previous').to_html).to be_blank
expect(render.css('span.next').to_html).to be_blank
end
end

context 'when there is next and previous' do
let(:search_context) { { next: next_doc, prev: prev_doc } }
let(:prev_doc) { SolrDocument.new(id: '777') }
Expand Down