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
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
Source
# File lib/blacklight/configuration.rb, line 28 def initialize_default_configuration @default_configurations&.map(&:call) @default_configuration_initialized = true end
Source
# File lib/blacklight/configuration.rb, line 33 def initialized_default_configuration? @default_configuration_initialized end
Source
# File lib/blacklight/configuration.rb, line 378 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
Source
# File lib/blacklight/configuration.rb, line 12 def property(key, default: nil) default_values[key] = default end
Public Instance Methods
Source
# File lib/blacklight/configuration.rb, line 464 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 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
Source
# File lib/blacklight/configuration.rb, line 477 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 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
Source
# File lib/blacklight/configuration.rb, line 570 def add_results_collection_tool(name, opts = {}) add_action(index.collection_actions, name, opts) end
Add a tool for the search result list itself @!macro partial_if_unless
Source
# File lib/blacklight/configuration.rb, line 576 def add_results_document_tool(name, opts = {}) add_action(index.document_actions, name, opts) end
Add a partial to the tools for each document in the search results. @!macro partial_if_unless
Source
# File lib/blacklight/configuration.rb, line 559 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 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.
Source
# File lib/blacklight/configuration.rb, line 544 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
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.
Source
# File lib/blacklight/configuration.rb, line 498 def build(klass) deep_copy.tap do |conf| conf.klass = klass end end
builds a copy for the provided controller class @param [Class] klass configuration host class
Source
# File lib/blacklight/configuration.rb, line 406 def configure yield self if block_given? self end
DSL helper @yield [config] @yieldparam [Blacklight::Configuration] @return [Blacklight::Configuration]
Source
# File lib/blacklight/configuration.rb, line 492 def deep_copy deep_transform_values(&method(:_deep_copy)) end
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.
Source
# File lib/blacklight/configuration.rb, line 398 def default_per_page super || per_page.first end
@return [Integer]
Source
# File lib/blacklight/configuration.rb, line 414 def default_search_field @default_search_field ||= super || search_fields.values.find { |f| f.default == true } || search_fields.values.first end
Returns default search field, used for simpler display in history, etc. if not set, defaults to first defined search field @return [Blacklight::Configuration::SearchField]
Source
# File lib/blacklight/configuration.rb, line 421 def default_sort_field field = super || sort_fields.values.find { |f| f.default == true } field || sort_fields.values.first end
Returns default sort field, used for simpler display in history, etc. if not set, defaults to first defined sort field @return [Blacklight::Configuration::SortField]
Source
# File lib/blacklight/configuration.rb, line 427 def default_title_field document_model.unique_key || 'id' end
@return [String]
Source
# File lib/blacklight/configuration.rb, line 433 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
@param [String] field Solr facet name @return [Blacklight::Configuration::FacetField] Blacklight
facet configuration for the solr field
Source
# File lib/blacklight/configuration.rb, line 444 def facet_field_names(group = nil) facet_fields_in_group(group).map(&:field) end
@param [String] group (nil) a group name of facet fields @return [Array<String>] a list of the facet field names from the configuration
Source
# File lib/blacklight/configuration.rb, line 450 def facet_fields_in_group(group) facet_fields.values.select { |opts| group == opts[:group] } end
@param [String] group (nil) a group name of facet fields @return [Array<Blacklight::Configuration::FacetField>] a list of facet fields
Source
# File lib/blacklight/configuration.rb, line 455 def facet_group_names facet_fields.map { |_facet, opts| opts[:group] }.uniq end
@return [Array<String>] a list of facet groups
Source
# File lib/blacklight/configuration.rb, line 588 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
Add a section of config that only applies to documents with a matching display type
Source
# File lib/blacklight/configuration.rb, line 617 def freeze each { |_k, v| v.is_a?(OpenStruct) && v.freeze } super end
@!visibility private
Source
# File lib/blacklight/configuration.rb, line 600 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
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
Source
# File lib/blacklight/configuration.rb, line 393 def logo_link super || Rails.application.routes.url_helpers.root_path end
@return [String] The destination for the link around the logo in the header
Source
# File lib/blacklight/configuration.rb, line 388 def repository repository_class.new(self) end
@return [Blacklight::Repository]
Source
# File lib/blacklight/configuration.rb, line 610 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
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
Source
# File lib/blacklight/configuration.rb, line 515 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
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]
Private Instance Methods
Source
# File lib/blacklight/configuration.rb, line 633 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
Provide custom duplication for certain types of configuration (intended for use in e.g. deep_transform_values)
Source
# File lib/blacklight/configuration.rb, line 643 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
Source
# File lib/blacklight/configuration.rb, line 624 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