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

Enable new functionality for pagination information on grouped results #2140

Merged
merged 4 commits into from
Sep 30, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
19 changes: 9 additions & 10 deletions app/helpers/blacklight/catalog_helper_behavior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,21 @@ def json_api_link_tag(options = {})

##
# Override the Kaminari page_entries_info helper with our own, blacklight-aware
# implementation.
# Displays the "showing X through Y of N" message.
# implementation. Why do we have to do this?
# - We need custom counting information for grouped results
# - We need to provide number_with_delimiter strings to i18n keys
# If we didn't have to do either one of these, we could get away with removing
# this entirely.
#
# @param [RSolr::Resource] collection (or other Kaminari-compatible objects)
# @return [String]
def page_entries_info(collection, options = {})
def page_entries_info(collection, entry_name: nil)
return unless show_pagination? collection

entry_name = if options[:entry_name]
options[:entry_name]
elsif collection.respond_to? :model # DataMapper
collection.model.model_name.human.downcase
elsif collection.respond_to?(:model_name) && !collection.model_name.nil? # AR, Blacklight::PaginationMethods
Copy link
Member

Choose a reason for hiding this comment

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

This change is breaking our site which uses both solr and non solr documents. because collection.model_name.downcase throws an error when collection.model_name is nil.

collection.model_name.human.downcase
entry_name = if entry_name
entry_name.pluralize(collection.size, I18n.locale)
else
t('blacklight.entry_name.default')
collection.entry_name(count: collection.size).downcase
Copy link
Member

Choose a reason for hiding this comment

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

Actually our issue is when collection.entry_name returns nil we get an error. We pushed up a PR to fix this.

end

entry_name = entry_name.pluralize unless collection.total_count == 1
Expand Down
2 changes: 2 additions & 0 deletions config/locales/blacklight.de.yml
Original file line number Diff line number Diff line change
Expand Up @@ -215,5 +215,7 @@ de:

entry_name:
default: 'Eintrag'
grouped:
default: 'gruppiertes Ergebnis'

did_you_mean: 'Meinten Sie: %{options}?'
2 changes: 2 additions & 0 deletions config/locales/blacklight.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -215,5 +215,7 @@ en:

entry_name:
default: 'entry'
grouped:
default: 'grouped result'

did_you_mean: 'Did you mean to type: %{options}?'
2 changes: 2 additions & 0 deletions config/locales/blacklight.es.yml
Original file line number Diff line number Diff line change
Expand Up @@ -215,5 +215,7 @@ es:

entry_name:
default: 'entrada'
grouped:
default: 'resultado agrupado'

did_you_mean: '¿Quiere decir %{options}?'
2 changes: 2 additions & 0 deletions config/locales/blacklight.fr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -219,5 +219,7 @@ fr:

entry_name:
default: 'résultat'
grouped:
default: 'résultat groupé'

did_you_mean: 'Essayez peut-être avec : %{options}'
2 changes: 2 additions & 0 deletions config/locales/blacklight.hu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -208,5 +208,7 @@ hu:

entry_name:
default: 'bejegyzés'
grouped:
default: 'csoportosított eredmény'

did_you_mean: 'Arra gondolt, hogy: %{options}?'
2 changes: 2 additions & 0 deletions config/locales/blacklight.it.yml
Original file line number Diff line number Diff line change
Expand Up @@ -215,5 +215,7 @@ it:

entry_name:
default: 'termine di ricerca'
grouped:
default: 'risultato raggruppato'

did_you_mean: 'Intendevi digitare: %{options}?'
2 changes: 2 additions & 0 deletions config/locales/blacklight.nl.yml
Original file line number Diff line number Diff line change
Expand Up @@ -208,5 +208,7 @@ nl:

entry_name:
default: 'ingang'
grouped:
default: 'gegroepeerd resultaat'

did_you_mean: 'Bedoelde u: %{options}?'
2 changes: 2 additions & 0 deletions config/locales/blacklight.pt-BR.yml
Original file line number Diff line number Diff line change
Expand Up @@ -217,5 +217,7 @@ pt-BR:

entry_name:
default: 'registro'
grouped:
default: 'resultado agrupado'

did_you_mean: 'Você quis dizer: %{options}?'
2 changes: 2 additions & 0 deletions config/locales/blacklight.sq.yml
Original file line number Diff line number Diff line change
Expand Up @@ -206,5 +206,7 @@ sq:

entry_name:
default: 'entry'
grouped:
default: 'rezultat i grupuar'

did_you_mean: 'A keni menduar: %{options}?'
2 changes: 2 additions & 0 deletions config/locales/blacklight.zh.yml
Original file line number Diff line number Diff line change
Expand Up @@ -208,5 +208,7 @@ zh:

entry_name:
default: '条目'
grouped:
default: '分组结果'

did_you_mean: '你是要输入: %{options} 吗?'
10 changes: 10 additions & 0 deletions lib/blacklight/solr/response/group_response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ def empty?
total.zero?
end

##
# Overridden from Blacklight::Solr::Response::PaginationMethods to support
# grouped key specific i18n keys. `key` is the field being grouped
def entry_name(options)
I18n.t(
"blacklight.entry_name.grouped.#{key}",
default: :'blacklight.entry_name.grouped.default'
).pluralize(options[:count])
end

def method_missing meth, *args, &block
if response.respond_to? meth
response.send(meth, *args, &block)
Expand Down
12 changes: 12 additions & 0 deletions lib/blacklight/solr/response/pagination_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,16 @@ def offset_value #:nodoc:
def total_count #:nodoc:
total
end

##
# Should return response documents size, not hash size
def size
total_count
end

##
# Meant to have the same signature as Kaminari::PaginatableArray#entry_name
def entry_name(options)
I18n.t('blacklight.entry_name.default').pluralize(options[:count])
end
end
9 changes: 0 additions & 9 deletions spec/helpers/catalog_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,6 @@ def render_grouped_response?
expect(html).to eq "<strong>1</strong> entry found"
expect(html).to be_html_safe
end

it "uses the model_name from the response" do
response = mock_response total: 1
allow(response).to receive(:model_name).and_return(double(human: 'thingy'))

html = page_entries_info(response)
expect(html).to eq "<strong>1</strong> thingy found"
expect(html).to be_html_safe
end
end

it "with a single page of results" do
Expand Down
13 changes: 13 additions & 0 deletions spec/models/blacklight/solr/response/group_response_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,19 @@
expect(group.empty?).to be false
end
end

describe "entry_name" do
it "accesses a custom field grouped i18n key" do
allow(I18n).to receive(:t).with(
'blacklight.entry_name.grouped.result_group_ssi',
default: :'blacklight.entry_name.grouped.default'
).and_return('cool group')
expect(group.entry_name(count: 2)).to eq 'cool groups'
end
it "falls back to default group key" do
expect(group.entry_name(count: 2)).to eq 'grouped results'
end
end
end

def create_response(response, params = {})
Expand Down
3 changes: 3 additions & 0 deletions spec/models/blacklight/solr/response_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@
expect(r.total_count).to eq(r.total)
expect(r.next_page).to eq(r.current_page + 1)
expect(r.prev_page).to eq(nil)
expect(r.entry_name(count: 1)).to eq 'entry'
expect(r.entry_name(count: 2)).to eq 'entries'
expect(r.size).to eq 26
if Kaminari.config.respond_to? :max_pages
expect(r.max_pages).to be_nil
end
Expand Down