class Blacklight::Configuration

Blacklight::Configuration holds the configuration for a Blacklight::Controller, including fields to display, facets to show, sort options, and search fields.

Constants

ADVANCED_SEARCH_PARAMETERS
BASIC_SEARCH_PARAMETERS

Public Class Methods

default_configuration(&block) click to toggle source
# File lib/blacklight/configuration.rb, line 16
def default_configuration(&block)
  @default_configurations ||= []

  if block
    @default_configurations << block

    block.call if @default_configuration_initialized
  end

  @default_configurations
end
initialize_default_configuration() click to toggle source
# File lib/blacklight/configuration.rb, line 28
def initialize_default_configuration
  @default_configurations&.map(&:call)
  @default_configuration_initialized = true
end
initialized_default_configuration?() click to toggle source
# File lib/blacklight/configuration.rb, line 33
def initialized_default_configuration?
  @default_configuration_initialized
end
new(hash = {}) { |self| ... } click to toggle source
Calls superclass method
# File lib/blacklight/configuration.rb, line 376
def initialize(hash = {})
  self.class.initialize_default_configuration unless self.class.initialized_default_configuration?

  super(self.class.default_values.deep_transform_values(&method(:_deep_copy)).merge(hash))
  yield(self) if block_given?

  @view_config ||= {}
end
property(key, default: nil) click to toggle source
# File lib/blacklight/configuration.rb, line 12
def property(key, default: nil)
  default_values[key] = default
end

Public Instance Methods

add_facet_fields_to_solr_request!(*fields) click to toggle source

Add any configured facet fields to the default solr parameters hash @overload add_facet_fields_to_solr_request!

add all facet fields to the solr request

@overload add_facet_fields_to_solr_request! field, field, etc

@param [Symbol] field Field names to add to the solr request
# File lib/blacklight/configuration.rb, line 462
def add_facet_fields_to_solr_request!(*fields)
  if fields.empty?
    self.add_facet_fields_to_solr_request = true
  else
    facet_fields.slice(*fields).each_value { |v| v.include_in_request = true }
  end
end
add_field_configuration_to_solr_request!(*fields) click to toggle source

Add any configured facet fields to the default solr parameters hash @overload add_field_configuration_to_solr_request!

add all index, show, and facet fields to the solr request

@overload add_field_configuration_to_solr_request! field, field, etc

@param [Symbol] field Field names to add to the solr request
# File lib/blacklight/configuration.rb, line 475
def add_field_configuration_to_solr_request!(*fields)
  if fields.empty?
    self.add_field_configuration_to_solr_request = true
  else
    index_fields.slice(*fields).each_value { |v| v.include_in_request = true }
    show_fields.slice(*fields).each_value { |v| v.include_in_request = true }
    facet_fields.slice(*fields).each_value { |v| v.include_in_request = true }
  end
end
add_nav_action(name, opts = {}) click to toggle source

Add a partial to the header navbar @!macro partial_if_unless

# File lib/blacklight/configuration.rb, line 580
def add_nav_action(name, opts = {})
  add_action(navbar.partials, name, opts)
end
add_results_collection_tool(name, opts = {}) click to toggle source

Add a tool for the search result list itself @!macro partial_if_unless

# File lib/blacklight/configuration.rb, line 568
def add_results_collection_tool(name, opts = {})
  add_action(index.collection_actions, name, opts)
end
add_results_document_tool(name, opts = {}) click to toggle source

Add a partial to the tools for each document in the search results. @!macro partial_if_unless

# File lib/blacklight/configuration.rb, line 574
def add_results_document_tool(name, opts = {})
  add_action(index.document_actions, name, opts)
end
add_show_header_tools_partial(name, opts = {}) click to toggle source

Add a partial to the show page header when rendering a document. @!macro partial_if_unless

@param name [String] the name of the document partial
@param opts [Hash]
@option opts [Class] :component draw a component
@option opts [String] :partial partial to draw if component is false
@option opts [Symbol,Proc] :if render this action if the method identified by the symbol or the proc evaluates to true. The proc will receive the action configuration and the document or documents for the action.
@option opts [Symbol,Proc] :unless render this action unless the method identified by the symbol or the proc evaluates to true. The proc will receive the action configuration and the document or documents for the action.
# File lib/blacklight/configuration.rb, line 557
def add_show_header_tools_partial(name, opts = {})
  opts[:partial] ||= 'document_action'

  add_action(show.header_actions, name, opts)
  klass && ActionBuilder.new(klass, name, opts).build
end
add_show_tools_partial(name, opts = {}) click to toggle source

Add a partial to the tools when rendering a document. @!macro partial_if_unless

@param name [String] the name of the document partial
@param opts [Hash]
@option opts [Class] :component draw a component
@option opts [String] :partial partial to draw if component is false
@option opts [Symbol,Proc] :if render this action if the method identified by the symbol or the proc evaluates to true. The proc will receive the action configuration and the document or documents for the action.
@option opts [Symbol,Proc] :unless render this action unless the method identified by the symbol or the proc evaluates to true. The proc will receive the action configuration and the document or documents for the action.
# File lib/blacklight/configuration.rb, line 542
def add_show_tools_partial(name, opts = {})
  opts[:partial] ||= 'document_action'

  add_action(show.document_actions, name, opts)
  klass && ActionBuilder.new(klass, name, opts).build
end
build(klass) click to toggle source

builds a copy for the provided controller class @param [Class] klass configuration host class

# File lib/blacklight/configuration.rb, line 496
def build(klass)
  deep_copy.tap do |conf|
    conf.klass = klass
  end
end
Also aliased as: inheritable_copy
configure() { |self| ... } click to toggle source

DSL helper @yield [config] @yieldparam [Blacklight::Configuration] @return [Blacklight::Configuration]

# File lib/blacklight/configuration.rb, line 404
def configure
  yield self if block_given?
  self
end
deep_copy() click to toggle source

Provide a ‘deep copy’ of Blacklight::Configuration that can be modified without effecting the original Blacklight::Configuration instance.

Note: Rails provides ‘#deep_dup`, but it aggressively `#dup`’s class names too, turning them into anonymous class instances.

# File lib/blacklight/configuration.rb, line 490
def deep_copy
  deep_transform_values(&method(:_deep_copy))
end
default_per_page() click to toggle source

@return [Integer]

Calls superclass method
# File lib/blacklight/configuration.rb, line 396
def default_per_page
  super || per_page.first
end
default_search_field() click to toggle source

Returns default search field, used for simpler display in history, etc. if not set, defaults to first defined search field @return [Blacklight::Configuration::SearchField]

Calls superclass method
# File lib/blacklight/configuration.rb, line 412
def default_search_field
  @default_search_field ||= super || search_fields.values.find { |f| f.default == true } || search_fields.values.first
end
default_sort_field() click to toggle source

Returns default sort field, used for simpler display in history, etc. if not set, defaults to first defined sort field @return [Blacklight::Configuration::SortField]

Calls superclass method
# File lib/blacklight/configuration.rb, line 419
def default_sort_field
  field = super || sort_fields.values.find { |f| f.default == true }
  field || sort_fields.values.first
end
default_title_field() click to toggle source

@return [String]

# File lib/blacklight/configuration.rb, line 425
def default_title_field
  document_model.unique_key || 'id'
end
facet_configuration_for_field(field) click to toggle source

@param [String] field Solr facet name @return [Blacklight::Configuration::FacetField] Blacklight facet configuration for the solr field

# File lib/blacklight/configuration.rb, line 431
def facet_configuration_for_field(field)
  # short-circuit on the common case, where the solr field name and the blacklight field name are the same.
  return facet_fields[field] if facet_fields[field] && facet_fields[field].field == field

  # Find the facet field configuration for the solr field, or provide a default.
  facet_fields.values.find { |v| v.field.to_s == field.to_s } ||
    FacetField.new(field: field).normalize!
end
facet_field_names(group = nil) click to toggle source

@param [String] group (nil) a group name of facet fields @return [Array<String>] a list of the facet field names from the configuration

# File lib/blacklight/configuration.rb, line 442
def facet_field_names(group = nil)
  facet_fields_in_group(group).map(&:field)
end
facet_fields_in_group(group) click to toggle source

@param [String] group (nil) a group name of facet fields @return [Array<Blacklight::Configuration::FacetField>] a list of facet fields

# File lib/blacklight/configuration.rb, line 448
def facet_fields_in_group(group)
  facet_fields.values.select { |opts| group == opts[:group] }
end
facet_group_names() click to toggle source

@return [Array<String>] a list of facet groups

# File lib/blacklight/configuration.rb, line 453
def facet_group_names
  facet_fields.map { |_facet, opts| opts[:group] }.uniq
end
for_display_type(display_type) { |conf| ... } click to toggle source

Add a section of config that only applies to documents with a matching display type

# File lib/blacklight/configuration.rb, line 586
def for_display_type display_type, &_block
  fields_for_type[display_type] ||= self.class.new

  fields_for_type[display_type].tap do |conf|
    yield(conf) if block_given?
  end
end
freeze() click to toggle source

@!visibility private

Calls superclass method
# File lib/blacklight/configuration.rb, line 615
def freeze
  each { |_k, v| v.is_a?(OpenStruct) && v.freeze }
  super
end
index_fields_for(display_types) click to toggle source

Return a list of fields for the index display that should be used for the provided document. This respects any configuration made using for_display_type @deprecated

# File lib/blacklight/configuration.rb, line 598
def index_fields_for(display_types)
  Array(display_types).inject(index_fields) do |fields, display_type|
    fields.merge(for_display_type(display_type).index_fields)
  end
end
inheritable_copy(klass)
Alias for: build
repository() click to toggle source

@return [Blacklight::Repository]

# File lib/blacklight/configuration.rb, line 386
def repository
  repository_class.new(self)
end
show_fields_for(display_types) click to toggle source

Return a list of fields for the show page that should be used for the provided document. This respects any configuration made using for_display_type @deprecated

# File lib/blacklight/configuration.rb, line 608
def show_fields_for(display_types)
  Array(display_types).inject(show_fields) do |fields, display_type|
    fields.merge(for_display_type(display_type).show_fields)
  end
end
view_config(view_type = nil, action_name: :index) click to toggle source

Get a view configuration for the given view type + action. The effective view configuration is inherited from:

  • the configuration from blacklight_config.view with the key ‘view_type`

  • the configuration from blacklight_config.action_mapping with the key ‘action_name`

  • any parent config for the action map result above

  • the action_mapping default configuration

  • the top-level index/show view configuration

@param [Symbol,#to_sym] view_type @return [Blacklight::Configuration::ViewConfig]

# File lib/blacklight/configuration.rb, line 513
def view_config(view_type = nil, action_name: :index)
  view_type &&= view_type.to_sym
  action_name &&= action_name.to_sym
  action_name ||= :index

  if view_type == :show
    action_name = view_type
    view_type = nil
  end

  @view_config[[view_type, action_name]] ||= if view_type.nil?
                                               action_config(action_name)
                                             else
                                               base_config = action_config(action_name)
                                               base_config.merge(view.fetch(view_type, {}))
                                             end
end

Private Instance Methods

_deep_copy(value) click to toggle source

Provide custom duplication for certain types of configuration (intended for use in e.g. deep_transform_values)

# File lib/blacklight/configuration.rb, line 631
def _deep_copy(value)
  case value
  when Module then value
  when NestedOpenStructWithHashAccess then value.class.new(value.nested_class, value.to_h.deep_transform_values(&method(:_deep_copy)))
  when OpenStruct then value.class.new(value.to_h.deep_transform_values(&method(:_deep_copy)))
  else
    value.dup
  end
end
action_config(action, default: :index) click to toggle source
# File lib/blacklight/configuration.rb, line 641
def action_config(action, default: :index)
  action_config = action_mapping[action]
  action_config ||= action_mapping[:default]

  if action_config.parent_config && action_config.parent_config != :default
    parent_config = action_mapping[action_config.parent_config]
    raise "View configuration error: the parent configuration of #{action_config.key}, #{parent_config.key}, must not specific its own parent configuration" if parent_config.parent_config

    action_config = action_config.reverse_merge(parent_config)
  end
  action_config = action_config.reverse_merge(action_mapping[:default]) if action_config != action_mapping[:default]

  action_config = action_config.reverse_merge(self[action_config.top_level_config]) if action_config.top_level_config
  action_config = action_config.reverse_merge(show) if default == :show && action_config.top_level_config != :show
  action_config.reverse_merge(index)
end
add_action(config_hash, name, opts) { |config| ... } click to toggle source
# File lib/blacklight/configuration.rb, line 622
def add_action(config_hash, name, opts)
  config = Blacklight::Configuration::ToolConfig.new opts
  config.name ||= name
  config.key = name
  yield(config) if block_given?
  config_hash[name] = config
end