Skip to content

Commit

Permalink
Merge db0a952 into d1a9955
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeer committed Feb 10, 2014
2 parents d1a9955 + db0a952 commit b1a2a30
Show file tree
Hide file tree
Showing 11 changed files with 230 additions and 52 deletions.
4 changes: 2 additions & 2 deletions app/helpers/blacklight/blacklight_helper_behavior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ def render_index_field_label *args
document = args.first

field = options[:field]
html_escape t(:'blacklight.search.index.label', label: index_fields(document)[field].label)
html_escape t(:"blacklight.search.index.#{document_index_view_type}.label", default: :'blacklight.search.index.label', label: index_field_label(document, field))
end

##
Expand Down Expand Up @@ -226,7 +226,7 @@ def render_document_show_field_label *args

field = options[:field]

html_escape t(:'blacklight.search.show.label', label: document_show_fields(document)[field].label)
html_escape t(:'blacklight.search.show.label', label: document_show_field_label(document, field))
end

##
Expand Down
60 changes: 60 additions & 0 deletions app/helpers/blacklight/configuration_helper_behavior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,66 @@ def document_show_fields document=nil
blacklight_config.show_fields
end

##
# Look up the label for the index field
def index_field_label document, field
label = index_fields(document)[field].label

solr_field_label(
label,
:"blacklight.search.fields.index.#{field}",
:"blacklight.search.fields.#{field}"
)
end

##
# Look up the label for the show field
def document_show_field_label document, field
label = document_show_fields(document)[field].label

solr_field_label(
label,
:"blacklight.search.fields.show.#{field}",
:"blacklight.search.fields.#{field}"
)
end

##
# Look up the label for the facet field
def facet_field_label field
label = blacklight_config.facet_fields[field].label

solr_field_label(
label,
:"blacklight.search.fields.facet.#{field}",
:"blacklight.search.fields.#{field}"
)
end

##
# Look up the label for a solr field.
#
# @overload
# @param [Symbol] an i18n key
#
# @overload
# @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 solr_field_label label, *i18n_keys
if label.is_a? Symbol
return t(label)
end

first, *rest = i18n_keys

rest << label

t(first, default: rest)
end

##
# Get the default index view type
def default_document_index_view_type
Expand Down
2 changes: 1 addition & 1 deletion app/views/catalog/_facet_layout.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<div class="panel panel-default facet_limit blacklight-<%= facet_field.field.parameterize %> <%= 'facet_limit-active' if facet_field_in_params?(facet_field.field) %>">
<div class="<%= "collapsed" if should_collapse_facet?(facet_field) %> collapse-toggle panel-heading" data-toggle="collapse" data-target="#facet-<%= facet_field.label.parameterize %>">
<h5 class="panel-title">
<%= link_to facet_field.label, "#", :"data-no-turbolink" => true %>
<%= link_to facet_field_label(facet_field.field), "#", :"data-no-turbolink" => true %>
</h5>
</div>
<div id="facet-<%= facet_field.label.parameterize %>" class="panel-collapse facet-content <%= should_collapse_facet?(facet_field) ? 'collapse' : 'in' %>">
Expand Down
2 changes: 1 addition & 1 deletion app/views/catalog/facet.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<div class="modal-header">
<button type="button" class="ajax-modal-close close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 class="modal-title"><%= blacklight_config.facet_fields[params[:id]].label %></h3>
<h3 class="modal-title"><%= facet_field_label(@facet.field) %></h3>
</div>
<div class="modal-body">
<div class="facet_extended_list">
Expand Down
3 changes: 2 additions & 1 deletion lib/blacklight/catalog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ def update

# displays values and pagination links for a single facet field
def facet
@pagination = get_facet_pagination(params[:id], params)
@facet = blacklight_config.facet_fields[params[:id]]
@pagination = get_facet_pagination(@facet.field, params)

respond_to do |format|
# Draw the facet selector for users who have javascript disabled:
Expand Down
42 changes: 38 additions & 4 deletions lib/blacklight/configuration/fields.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,30 @@ def add_solr_field config_key, *args, &block
args[0] = args[0].to_s
field_config_from_key_and_hash(config_key, *args)
when Array
field_config_from_array(config_key, *args)
field_config_from_array(config_key, *args, &block)
return # we've iterated over the array above.
else
field_config_from_field_or_hash(config_key, *args)
end

return if field_config.is_a? Array
# look up any dynamic fields
if field_config.field.to_s =~ /\*/ and luke_fields
salient_fields = luke_fields.select do |k,v|
k =~ Regexp.new("^" + field_config.field.to_s.gsub('*', '.+') + "$")
end

salient_fields.each do |field, luke_config|
config = field_config.dup
config.field = field
if self[config_key.pluralize][ config.field ]
self[config_key.pluralize][ config.field ] = config.merge(self[config_key.pluralize][ config.field ])
else
add_solr_field(config_key, config, &block)
end
end

return
end

if block_given?
yield field_config
Expand All @@ -94,6 +112,22 @@ def add_solr_field config_key, *args, &block
end

protected
def luke_fields
if @table[:luke_fields] === false
return nil
end

@table[:luke_fields] ||= begin
if has_key? :blacklight_solr
blacklight_solr.get('admin/luke', params: { fl: '*', 'json.nl' => 'map' })['fields']
end
rescue
false
end

@table[:luke_fields] || nil
end

# Add a solr field by a solr field name and hash
def field_config_from_key_and_hash config_key, solr_field, field_or_hash = {}
field_config = field_config_from_field_or_hash(config_key, field_or_hash)
Expand All @@ -103,9 +137,9 @@ def field_config_from_key_and_hash config_key, solr_field, field_or_hash = {}
end

# Add multiple solr fields using a hash or Field instance
def field_config_from_array config_key, array_of_fields_or_hashes
def field_config_from_array config_key, array_of_fields_or_hashes, &block
array_of_fields_or_hashes.map do |field_or_hash|
add_solr_field(config_key, field_or_hash)
add_solr_field(config_key, field_or_hash, &block)
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/blacklight/utils.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'ostruct'
module Blacklight
class OpenStructWithHashAccess < OpenStruct
delegate :keys, :each, :map, :has_key?, :delete, :length, :reject!, :select!, :include, :fetch, :to => :to_h
delegate :keys, :each, :map, :has_key?, :empty?, :delete, :length, :reject!, :select!, :include, :fetch, :to => :to_h

def []=(key, value)
send "#{key}=", value
Expand Down
42 changes: 42 additions & 0 deletions spec/helpers/configuration_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,46 @@
expect(helper.spell_check_max).to eq config_value
end
end

describe "#index_field_label" do
let(:document) { double }
it "should look up the label to display for the given document and field" do
helper.stub(:index_fields).and_return({ "my_field" => double(label: "some label") })
helper.should_receive(:solr_field_label).with("some label", :"blacklight.search.fields.index.my_field", :"blacklight.search.fields.my_field")
helper.index_field_label document, "my_field"
end
end

describe "#document_show_field_label" do
let(:document) { double }
it "should look up the label to display for the given document and field" do
helper.stub(:document_show_fields).and_return({ "my_field" => double(label: "some label") })
helper.should_receive(:solr_field_label).with("some label", :"blacklight.search.fields.show.my_field", :"blacklight.search.fields.my_field")
helper.document_show_field_label document, "my_field"
end
end

describe "#facet_field_label" do
let(:document) { double }
it "should look up the label to display for the given document and field" do
blacklight_config.stub(:facet_fields).and_return({ "my_field" => double(label: "some label") })
helper.should_receive(:solr_field_label).with("some label", :"blacklight.search.fields.facet.my_field", :"blacklight.search.fields.my_field")
helper.facet_field_label "my_field"
end
end

describe "#solr_field_label" do
it "should look up the label as an i18n string" do
helper.should_receive(:t).with(:some_key).and_return "my label"
label = helper.solr_field_label :some_key

expect(label).to eq "my label"
end

it "should pass the provided i18n keys to I18n.t" do
helper.should_receive(:t).with(:key_a, default: [:key_b, "default text"])

label = helper.solr_field_label "default text", :key_a, :key_b
end
end
end
42 changes: 41 additions & 1 deletion spec/lib/blacklight/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,13 @@
expect(@config.facet_fields).to have(2).fields
end

it "should accept array form with a block" do
expect do |b|
@config.add_facet_field([{ :field => 'format', :label => 'Format'}, { :field => 'publication_date', :label => 'Publication Date' }], &b)
end.to yield_control.twice
end


it "should create default label from titleized solr field" do
@config.add_facet_field("publication_date")

Expand All @@ -167,7 +174,18 @@
it "should raise on nil solr field name" do
expect { @config.add_facet_field(nil) }.to raise_error ArgumentError
end


it "should take wild-carded field names and dereference them to solr fields" do
@config.stub(luke_fields: {
"some_field_facet" => {},
"another_field_facet" => {},
"a_facet_field" => {},
})
expect { |b| @config.add_index_field "*_facet", &b }.to yield_control.twice

expect(@config.index_fields.keys).to eq ["some_field_facet", "another_field_facet"]
end

end

describe "add_index_field" do
Expand Down Expand Up @@ -201,6 +219,17 @@
expect { @config.add_index_field(nil) }.to raise_error ArgumentError
end

it "should take wild-carded field names and dereference them to solr fields" do
@config.stub(luke_fields: {
"some_field_display" => {},
"another_field_display" => {},
"a_facet_field" => {},
})
@config.add_index_field "*_display"

expect(@config.index_fields.keys).to eq ["some_field_display", "another_field_display"]
end

end

describe "add_show_field" do
Expand Down Expand Up @@ -235,6 +264,17 @@
expect { @config.add_show_field(nil) }.to raise_error ArgumentError
end

it "should take wild-carded field names and dereference them to solr fields" do
@config.stub(luke_fields: {
"some_field_display" => {},
"another_field_display" => {},
"a_facet_field" => {},
})
@config.add_show_field "*_display"

expect(@config.show_fields.keys).to eq ["some_field_display", "another_field_display"]
end

end


Expand Down
4 changes: 3 additions & 1 deletion spec/views/catalog/_facet_layout.html.erb_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
describe "catalog/facet_layout" do

let :blacklight_config do
Blacklight::Configuration.new
Blacklight::Configuration.new do |config|
config.facet_fields[facet_field.field] = facet_field
end
end

let :facet_field do
Expand Down
Loading

0 comments on commit b1a2a30

Please sign in to comment.