Skip to content

Commit

Permalink
Use i18n translations for search and sort fields
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeer committed Oct 28, 2016
1 parent c7c3329 commit 135e838
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 38 deletions.
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
28 changes: 26 additions & 2 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), 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 @@ -103,6 +103,30 @@ 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_label(
:"blacklight.search.fields.search.#{field_config.key}",
:"blacklight.search.fields.#{field_config.key}",
(field_config.label if field_config),
key.to_s.humanize
)
end

def sort_field_label(key)
field_config = blacklight_config.sort_fields[key]

field_label(
:"blacklight.search.fields.sort.#{key}",
(field_config.label if field_config),
key.to_s.humanize
)
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
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

0 comments on commit 135e838

Please sign in to comment.