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

Use i18n translations for search and sort fields #1566

Merged
merged 2 commits into from Oct 28, 2016
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 0 additions & 18 deletions app/controllers/concerns/blacklight/search_fields.rb
Expand Up @@ -32,27 +32,9 @@ def search_field_list
blacklight_config.search_fields.values
end

# Looks up a search field blacklight_config hash from search_field_list having
# a certain supplied :key.
def search_field_def_for_key(key)
blacklight_config.search_fields[key]
end

# Returns default search field, used for simpler display in history, etc.
# if not set in blacklight_config, defaults to first field listed in #search_field_list
def default_search_field
blacklight_config.default_search_field || search_field_list.first
end

# Shortcut for commonly needed operation, look up display
# label for the key specified. Returns "Keyword" if a label
# can't be found.
def label_for_search_field(key)
field_def = search_field_def_for_key(key)
if field_def && field_def.label
field_def.label
else
I18n.t('blacklight.search.fields.default')
end
end
end
43 changes: 25 additions & 18 deletions app/helpers/blacklight/configuration_helper_behavior.rb
Expand Up @@ -11,7 +11,7 @@ def index_fields _document=nil

# Used in the document_list partial (search view) for building a select element
def sort_fields
active_sort_fields.map { |_key, x| [x.label, x.key] }
active_sort_fields.map { |_sort_key, field_config| [sort_field_label(field_config.key), field_config.key] }
end

def active_sort_fields
Expand All @@ -28,7 +28,7 @@ def search_fields
# marked :include_in_simple_select => false
def search_field_options_for_select
blacklight_config.search_fields.collect do |_key, field_def|
[field_def.label, field_def.key] if should_render_field?(field_def)
[label_for_search_field(field_def.key), field_def.key] if should_render_field?(field_def)
end.compact
end

Expand Down Expand Up @@ -59,37 +59,27 @@ def default_search_field?(selected_search_field)
# Look up the label for the index field
def index_field_label document, field
field_config = index_fields(document)[field]
field_config ||= Blacklight::Configuration::NullField.new(key: field)

field_label(
:"blacklight.search.fields.index.#{field}",
:"blacklight.search.fields.#{field}",
(field_config.label if field_config),
field.to_s.humanize
)
field_config.display_label('index')
end

##
# Look up the label for the show field
def document_show_field_label document, field
field_config = document_show_fields(document)[field]
field_config ||= Blacklight::Configuration::NullField.new(key: field)

field_label(
:"blacklight.search.fields.show.#{field}",
:"blacklight.search.fields.#{field}",
(field_config.label if field_config),
field.to_s.humanize
)
field_config.display_label('show')
end

##
# Look up the label for the facet field
def facet_field_label field
field_config = blacklight_config.facet_fields[field]
defaults = [:"blacklight.search.fields.facet.#{field}", :"blacklight.search.fields.#{field}"]
defaults << field_config.label if field_config
defaults << field.to_s.humanize
field_config ||= Blacklight::Configuration::NullField.new(key: field)

field_label(*defaults)
field_config.display_label('facet')
end

def view_label view
Expand All @@ -103,6 +93,23 @@ def view_label view
)
end

# Shortcut for commonly needed operation, look up display
# label for the key specified. Returns "Keyword" if a label
# can't be found.
def label_for_search_field(key)
field_config = blacklight_config.search_fields[key]
field_config ||= Blacklight::Configuration::NullField.new(key: field)

field_config.display_label('search')
end

def sort_field_label(key)
field_config = blacklight_config.sort_fields[key]
field_config ||= Blacklight::Configuration::NullField.new(key: field)

field_config.display_label('sort')
end

##
# Look up the label for a solr field.
#
Expand Down
4 changes: 2 additions & 2 deletions app/views/catalog/_sort_widget.html.erb
@@ -1,12 +1,12 @@
<% if show_sort_and_per_page? and !active_sort_fields.blank? %>
<div id="sort-dropdown" class="sort-dropdown btn-group">
<button type="button" class="btn btn-secondary dropdown-toggle" data-toggle="dropdown">
<%= t('blacklight.search.sort.label', :field =>current_sort_field.label) %> <span class="caret"></span>
<%= t('blacklight.search.sort.label', :field =>sort_field_label(current_sort_field.key)) %> <span class="caret"></span>
</button>

<ul class="dropdown-menu" role="menu">
<%- active_sort_fields.each do |sort_key, field_config| %>
<li class="dropdown-item"><%= link_to(field_config.label, url_for(search_state.params_for_search(sort: sort_key))) %></li>
<li class="dropdown-item"><%= link_to(sort_field_label(sort_key), url_for(search_state.params_for_search(sort: sort_key))) %></li>
<%- end -%>
</ul>
</div>
Expand Down
29 changes: 29 additions & 0 deletions lib/blacklight/configuration/field.rb
Expand Up @@ -19,12 +19,41 @@ def validate!
raise ArgumentError, "Must supply a field name" if self.field.nil?
end

def display_label(context = nil)
field_label(
(:"blacklight.search.fields.#{context}.#{key}" if context),
:"blacklight.search.fields.#{key}",
label,
default_label
)
end

def default_label
if self.key.respond_to?(:titleize)
self.key.try(:titleize)
else
self.key.to_s.titleize
end
end

private

##
# Look up the label for a solr field.
#
# @overload label
# @param [Symbol] an i18n key
#
# @overload label, i18n_key, another_i18n_key, and_another_i18n_key
# @param [String] default label to display if the i18n look up fails
# @param [Symbol] i18n keys to attempt to look up
# before falling back to the label
# @param [Symbol] any number of additional keys
# @param [Symbol] ...
def field_label *i18n_keys
first, *rest = i18n_keys.compact

I18n.t(first, default: rest)
end
end
end
16 changes: 0 additions & 16 deletions spec/controllers/blacklight/search_fields_spec.rb
Expand Up @@ -29,22 +29,6 @@ class MockConfig
end
end

it "fills in default qt where needed" do
expect(@search_field_obj.search_field_def_for_key("all_fields").qt).to eq @config.default_solr_params[:qt]
end

it "lookups field definitions by key" do
expect(@search_field_obj.search_field_def_for_key("title").key).to eq "title"
end

it "finds label by key" do
expect(@search_field_obj.label_for_search_field("title")).to eq "Title"
end

it "supplies default label for key not found" do
expect(@search_field_obj.label_for_search_field("non_existent_key")).to eq "Keyword"
end

describe "for unspecified :key" do
before do
@bad_config = MockConfig.new
Expand Down
29 changes: 1 addition & 28 deletions spec/helpers/blacklight/configuration_helper_behavior_spec.rb
Expand Up @@ -17,7 +17,7 @@

describe "#sort_fields" do
it "converts the sort fields to select-ready values" do
allow(blacklight_config).to receive_messages(sort_fields: { 'a' => double(key: 'a', label: 'a'), 'b' => double(key: 'b', label: 'b'), c: double(key: 'c', if: false) })
allow(blacklight_config).to receive_messages(sort_fields: { 'a' => double(key: 'a', display_label: 'a'), 'b' => double(key: 'b', display_label: 'b'), c: double(key: 'c', if: false, display_label: nil) })
expect(helper.sort_fields).to eq [['a', 'a'], ['b', 'b']]
end
end
Expand Down Expand Up @@ -115,33 +115,6 @@
end
end

describe "#index_field_label" do
let(:document) { instance_double(SolrDocument) }
it "looks up the label to display for the given document and field" do
allow(helper).to receive(:index_fields).and_return({ "my_field" => double(label: "some label") })
allow(helper).to receive(:field_label).with(:"blacklight.search.fields.index.my_field", :"blacklight.search.fields.my_field", "some label", "My field")
helper.index_field_label document, "my_field"
end
end

describe "#document_show_field_label" do
let(:document) { instance_double(SolrDocument) }
it "looks up the label to display for the given document and field" do
allow(helper).to receive(:document_show_fields).and_return({ "my_field" => double(label: "some label") })
allow(helper).to receive(:field_label).with(:"blacklight.search.fields.show.my_field", :"blacklight.search.fields.my_field", "some label", "My field")
helper.document_show_field_label document, "my_field"
end
end

describe "#facet_field_label" do
let(:document) { instance_double(SolrDocument) }
it "looks up the label to display for the given document and field" do
allow(blacklight_config).to receive(:facet_fields).and_return({ "my_field" => double(label: "some label") })
allow(helper).to receive(:field_label).with(:"blacklight.search.fields.facet.my_field", :"blacklight.search.fields.my_field", "some label", "My field")
helper.facet_field_label "my_field"
end
end

describe "#view_label" do
it "looks up the label to display for the view" do
allow(blacklight_config).to receive(:view).and_return({ "my_view" => double(label: "some label", title: nil) })
Expand Down
Expand Up @@ -79,7 +79,7 @@

it "renders a constraint for a selected facet not in the config" do
response = helper.render_search_to_s_filters(:f => {"undefined_facet" => ["value1", "value2"]})
expect(response).to eq("<span class=\"constraint\"><span class=\"filter-name\">#{'undefined_facet'.humanize}:</span><span class=\"filter-values\"><span class=\"filter-value\">value1</span><span class=\"filter-separator\"> and </span><span class=\"filter-value\">value2</span></span></span>")
expect(response).to eq("<span class=\"constraint\"><span class=\"filter-name\">#{'undefined_facet'.titleize}:</span><span class=\"filter-values\"><span class=\"filter-value\">value1</span><span class=\"filter-separator\"> and </span><span class=\"filter-value\">value2</span></span></span>")
end

context 'with I18n translations for selected facet' do
Expand Down
12 changes: 12 additions & 0 deletions spec/lib/blacklight/configuration/field_spec.rb
@@ -0,0 +1,12 @@
describe Blacklight::Configuration::Field do
subject { described_class.new(key: key, label: label) }
let(:key) { 'some_key' }
let(:label) { 'some label' }

describe '#display_label' do
it "looks up the label to display for the given document and field" do
allow(I18n).to receive(:t).with(:"blacklight.search.fields.my_context.some_key", default: [:"blacklight.search.fields.some_key", label, subject.default_label]).and_return('x')
expect(subject.display_label('my_context')).to eq 'x'
end
end
end