Skip to content

Commit

Permalink
Blacklight facet, index and show parameters should support a blacklig…
Browse files Browse the repository at this point in the history
…ht key that may be different from the solr field name
  • Loading branch information
cbeer committed Feb 24, 2015
1 parent ae5b46c commit 8e56c78
Show file tree
Hide file tree
Showing 13 changed files with 48 additions and 37 deletions.
2 changes: 1 addition & 1 deletion app/helpers/blacklight/facets_helper_behavior.rb
Expand Up @@ -209,7 +209,7 @@ def facet_display_value field, item
end

def facet_field_id facet_field
"facet-#{facet_field.field.parameterize}"
"facet-#{facet_field.key.parameterize}"
end

private
Expand Down
4 changes: 2 additions & 2 deletions app/views/catalog/_facet_layout.html.erb
@@ -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="panel panel-default facet_limit blacklight-<%= facet_field.key.parameterize %> <%= 'facet_limit-active' if facet_field_in_params?(facet_field.key) %>">
<div class="<%= "collapsed" if should_collapse_facet?(facet_field) %> collapse-toggle panel-heading" data-toggle="collapse" data-target="#<%= facet_field_id(facet_field) %>">
<h5 class="panel-title">
<%= link_to facet_field_label(facet_field.field), "#", :"data-no-turbolink" => true %>
<%= link_to facet_field_label(facet_field.key), "#", :"data-no-turbolink" => true %>
</h5>
</div>
<div id="<%= facet_field_id(facet_field) %>" 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
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"><%= facet_field_label(@facet.field) %></h3>
<h3 class="modal-title"><%= facet_field_label(@facet.key) %></h3>
</div>
<div class="modal-body">
<div class="facet_extended_list">
Expand Down
2 changes: 1 addition & 1 deletion lib/blacklight/catalog.rb
Expand Up @@ -77,7 +77,7 @@ def track
# displays values and pagination links for a single facet field
def facet
@facet = blacklight_config.facet_fields[params[:id]]
@response = get_facet_field_response(@facet.field, params)
@response = get_facet_field_response(@facet.key, params)
@display_facet = @response.facets.first

@pagination = facet_paginator(@facet, @display_facet)
Expand Down
1 change: 1 addition & 0 deletions lib/blacklight/configuration.rb
Expand Up @@ -302,6 +302,7 @@ def facet_fields_to_add_to_solr
.reject { |k,v| v[:query] || v[:pivot] }
.map { |k,v| v.field }
end
deprecation_deprecate :facet_fields_to_add_to_solr

##
# Provide a 'deep copy' of Blacklight::Configuration that can be modifyed without affecting
Expand Down
12 changes: 7 additions & 5 deletions lib/blacklight/configuration/facet_field.rb
Expand Up @@ -3,16 +3,18 @@ class Configuration::FacetField < Blacklight::Configuration::Field
def normalize! blacklight_config = nil
self.query.stringify_keys! if self.query

if self.single and self.tag.blank? and self.ex.blank?
self.tag = "#{self.field}_single"
self.ex = "#{self.field}_single"
end

self.collapse = true if self.collapse.nil?
self.show = true if self.show.nil?
self.if ||= self.show

super

if self.single and self.tag.blank? and self.ex.blank?
self.tag = "#{self.key}_single"
self.ex = "#{self.key}_single"
end

self
end
end
end
11 changes: 8 additions & 3 deletions lib/blacklight/configuration/field.rb
@@ -1,9 +1,14 @@
module Blacklight
class Configuration::Field < OpenStructWithHashAccess
def normalize! blacklight_config = nil
self.field ||= self.key
self.key ||= self.field

self.label ||= default_label

self.if = true if self.if.nil?
self.unless = false if self.unless.nil?

self
end

Expand All @@ -12,10 +17,10 @@ def validate!
end

def default_label
if self.field.respond_to?(:titleize)
self.field.try(:titleize)
if self.key.respond_to?(:titleize)
self.key.try(:titleize)
else
self.field.to_s.titleize
self.key.to_s.titleize
end
end
end
Expand Down
17 changes: 9 additions & 8 deletions lib/blacklight/configuration/fields.rb
Expand Up @@ -83,16 +83,18 @@ def add_blacklight_field config_key, *args, &block
end

# look up any dynamic fields
if field_config.field.to_s =~ /\*/ and luke_fields
if (field_config.field || field_config.key).to_s =~ /\*/ and luke_fields
wildcard_field = (field_config.field || field_config.key).to_s
salient_fields = luke_fields.select do |k,v|
k =~ Regexp.new("^" + field_config.field.to_s.gsub('*', '.+') + "$")
k =~ Regexp.new("^" + wildcard_field.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 ])
config.key = field
if self[config_key.pluralize][ config.key ]
self[config_key.pluralize][ config.key ] = config.merge(self[config_key.pluralize][ config.key ])
else
add_blacklight_field(config_key, config, &block)
end
Expand All @@ -108,9 +110,9 @@ def add_blacklight_field config_key, *args, &block
field_config.normalize!(self)
field_config.validate!

raise "A #{config_key} with the key #{field_config.field} already exists." if self[config_key.pluralize][field_config.field].present?
raise "A #{config_key} with the key #{field_config.key} already exists." if self[config_key.pluralize][field_config.key].present?

self[config_key.pluralize][ field_config.field ] = field_config
self[config_key.pluralize][ field_config.key ] = field_config
end
alias_method :add_solr_field, :add_blacklight_field
deprecation_deprecate add_solr_field: :add_blacklight_field
Expand All @@ -135,8 +137,7 @@ def luke_fields
# Add a solr field by a solr field name and hash
def field_config_from_key_and_hash config_key, field_name, field_or_hash = {}
field_config = field_config_from_field_or_hash(config_key, field_or_hash)
field_config.field = field_name

field_config.key = field_name
field_config
end

Expand Down
10 changes: 4 additions & 6 deletions lib/blacklight/configuration/search_field.rb
@@ -1,14 +1,12 @@
module Blacklight
class Configuration::SearchField < Blacklight::Configuration::Field
def normalize! blacklight_config = nil
# Some normalization, calculate display_label from key,
# and make sure we have a qt from defaults.
self.key ||= self.field
self.field ||= self.key
self.label ||= self.key.try(:titlecase)
self.qt ||= blacklight_config.default_solr_params[:qt] if blacklight_config && blacklight_config.default_solr_params
self.if ||= self.include_in_simple_select

super
self.qt ||= blacklight_config.default_solr_params[:qt] if blacklight_config && blacklight_config.default_solr_params

self
end

def validate!
Expand Down
9 changes: 4 additions & 5 deletions lib/blacklight/configuration/sort_field.rb
@@ -1,14 +1,13 @@
module Blacklight
class Configuration::SortField < Blacklight::Configuration::Field
def normalize! blacklight_config = nil
self.sort ||= self.field

self.field ||= self.key
super
self.field ||= self.label.try(:parameterize)
self.field ||= self.sort

self.key ||= self.field
super
self.sort ||= self.field

self
end

def validate!
Expand Down
2 changes: 1 addition & 1 deletion lib/blacklight/document_presenter.rb
Expand Up @@ -166,7 +166,7 @@ def get_field_values field, field_config, options = {}
@controller.send(field_config.helper_method, options.merge(:document => @document, :field => field, :value => value))
when (field_config and field_config.link_to_search)
link_field = if field_config.link_to_search === true
field_config.field
field_config.key
else
field_config.link_to_search
end
Expand Down
11 changes: 8 additions & 3 deletions lib/blacklight/facet.rb
Expand Up @@ -8,7 +8,7 @@ def facet_paginator field_config, display_facet
Blacklight::Solr::FacetPaginator.new(display_facet.items,
sort: display_facet.sort,
offset: display_facet.offset,
limit: facet_limit_for(field_config.field))
limit: facet_limit_for(field_config.key))
end

def facets_from_request(fields = facet_field_names)
Expand All @@ -20,7 +20,12 @@ def facet_field_names
end

def facet_configuration_for_field(field)
blacklight_config.facet_fields[field] || Blacklight::Configuration::FacetField.new(:field => field).normalize!
f = blacklight_config.facet_fields[field]
f ||= begin
_, value = blacklight_config.facet_fields.find { |k,v| v.field == field }
value
end
f ||= Blacklight::Configuration::FacetField.new(:field => field).normalize!
end


Expand All @@ -30,7 +35,7 @@ def facet_by_field_name field
when String, Symbol
extract_facet_by_field_name(field)
when Blacklight::Configuration::FacetField
extract_facet_by_field_name(field.field)
extract_facet_by_field_name(field.key)
else
field
end
Expand Down
2 changes: 1 addition & 1 deletion spec/helpers/facets_helper_spec.rb
Expand Up @@ -383,7 +383,7 @@

describe "#facet_field_id" do
it "should be the parameterized version of the facet field" do
expect(helper.facet_field_id double(field: 'some field')).to eq "facet-some-field"
expect(helper.facet_field_id double(key: 'some field')).to eq "facet-some-field"
end
end
end

0 comments on commit 8e56c78

Please sign in to comment.