Skip to content

Commit

Permalink
Extract field i18n logic from helpers into Blacklight::Configuration:…
Browse files Browse the repository at this point in the history
…:Field
  • Loading branch information
cbeer committed Oct 28, 2016
1 parent 57c6bbf commit 02ea2e5
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 56 deletions.
37 changes: 10 additions & 27 deletions app/helpers/blacklight/configuration_helper_behavior.rb
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 @@ -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

##
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
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

0 comments on commit 02ea2e5

Please sign in to comment.