diff --git a/app/helpers/blacklight/configuration_helper_behavior.rb b/app/helpers/blacklight/configuration_helper_behavior.rb
index 378be55f4c..fcaa182fb6 100644
--- a/app/helpers/blacklight/configuration_helper_behavior.rb
+++ b/app/helpers/blacklight/configuration_helper_behavior.rb
@@ -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
@@ -108,23 +98,16 @@ def view_label view
# 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_label(
- :"blacklight.search.fields.search.#{field_config.key}",
- :"blacklight.search.fields.#{field_config.key}",
- (field_config.label if field_config),
- key.to_s.humanize
- )
+ 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_label(
- :"blacklight.search.fields.sort.#{key}",
- (field_config.label if field_config),
- key.to_s.humanize
- )
+ field_config.display_label('sort')
end
##
diff --git a/lib/blacklight/configuration/field.rb b/lib/blacklight/configuration/field.rb
index 6536931b2a..5fd09953e3 100644
--- a/lib/blacklight/configuration/field.rb
+++ b/lib/blacklight/configuration/field.rb
@@ -19,6 +19,15 @@ 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)
@@ -26,5 +35,25 @@ def default_label
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
diff --git a/spec/helpers/blacklight/configuration_helper_behavior_spec.rb b/spec/helpers/blacklight/configuration_helper_behavior_spec.rb
index 5bce79e6b4..e7189e33a2 100644
--- a/spec/helpers/blacklight/configuration_helper_behavior_spec.rb
+++ b/spec/helpers/blacklight/configuration_helper_behavior_spec.rb
@@ -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
@@ -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) })
diff --git a/spec/helpers/blacklight/search_history_constraints_helper_behavior_spec.rb b/spec/helpers/blacklight/search_history_constraints_helper_behavior_spec.rb
index ccba359bc5..d1b896df5c 100644
--- a/spec/helpers/blacklight/search_history_constraints_helper_behavior_spec.rb
+++ b/spec/helpers/blacklight/search_history_constraints_helper_behavior_spec.rb
@@ -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("#{'undefined_facet'.humanize}:value1 and value2")
+ expect(response).to eq("#{'undefined_facet'.titleize}:value1 and value2")
end
context 'with I18n translations for selected facet' do
diff --git a/spec/lib/blacklight/configuration/field_spec.rb b/spec/lib/blacklight/configuration/field_spec.rb
new file mode 100644
index 0000000000..23215c0e97
--- /dev/null
+++ b/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