diff --git a/app/controllers/concerns/blacklight/search_fields.rb b/app/controllers/concerns/blacklight/search_fields.rb index 45097ca781..f3d4946247 100644 --- a/app/controllers/concerns/blacklight/search_fields.rb +++ b/app/controllers/concerns/blacklight/search_fields.rb @@ -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 diff --git a/app/helpers/blacklight/configuration_helper_behavior.rb b/app/helpers/blacklight/configuration_helper_behavior.rb index 90b13c88fe..fcaa182fb6 100644 --- a/app/helpers/blacklight/configuration_helper_behavior.rb +++ b/app/helpers/blacklight/configuration_helper_behavior.rb @@ -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 @@ -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 @@ -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 @@ -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. # diff --git a/app/views/catalog/_sort_widget.html.erb b/app/views/catalog/_sort_widget.html.erb index ef509b97d4..7a0c29bc3e 100644 --- a/app/views/catalog/_sort_widget.html.erb +++ b/app/views/catalog/_sort_widget.html.erb @@ -1,12 +1,12 @@ <% if show_sort_and_per_page? and !active_sort_fields.blank? %>
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/controllers/blacklight/search_fields_spec.rb b/spec/controllers/blacklight/search_fields_spec.rb index 089eaa1a80..dc1c7cbb8c 100644 --- a/spec/controllers/blacklight/search_fields_spec.rb +++ b/spec/controllers/blacklight/search_fields_spec.rb @@ -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 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