Class: Blacklight::Configuration
- Inherits:
-
OpenStructWithHashAccess
- Object
- OpenStruct
- OpenStructWithHashAccess
- Blacklight::Configuration
- Includes:
- Fields
- Defined in:
- lib/blacklight/configuration.rb,
lib/blacklight/configuration/fields.rb,
lib/blacklight/configuration/context.rb,
lib/blacklight/configuration/view_config.rb,
lib/blacklight/configuration/session_tracking_config.rb
Overview
Blacklight::Configuration holds the configuration for a Blacklight::Controller, including fields to display, facets to show, sort options, and search fields.
Defined Under Namespace
Modules: Fields Classes: ActionConfigMapEntry, Context, DisplayField, FacetField, Field, IndexField, NullDisplayField, NullField, SearchField, SessionTrackingConfig, ShowField, SortField, ToolConfig, ViewConfig
Constant Summary collapse
- BASIC_SEARCH_PARAMETERS =
[:q, :qt, :page, :per_page, :search_field, :sort, :controller, :action, :'facet.page', :'facet.prefix', :'facet.sort', :rows, :format, :view, :id, :facet_id, :query_fragment, :only_values].freeze
- ADVANCED_SEARCH_PARAMETERS =
[{ clause: {} }, :op].freeze
Instance Attribute Summary collapse
- #email_fields ⇒ Hash{Symbol=>Blacklight::Configuration::DisplayField}
- #facet_fields ⇒ Hash{Symbol=>Blacklight::Configuration::FacetField}
- #index_fields ⇒ Hash{Symbol=>Blacklight::Configuration::IndexField}
- #search_fields ⇒ Hash{Symbol=>Blacklight::Configuration::SearchField}
- #show_fields ⇒ Hash{Symbol=>Blacklight::Configuration::ShowField}
- #sms_fields ⇒ Hash{Symbol=>Blacklight::Configuration::DisplayField}
- #sort_fields ⇒ Hash{Symbol=>Blacklight::Configuration::SortField}
Class Method Summary collapse
- .default_configuration(&block) ⇒ Object
- .initialize_default_configuration ⇒ Object
- .initialized_default_configuration? ⇒ Boolean
- .property(key, default: nil) ⇒ Object
Instance Method Summary collapse
- #add_email_field(config_key, hash_or_field_or_array) ⇒ Blacklight::Configuration::DisplayField
- #add_facet_field(config_key, hash_or_field_or_array) ⇒ Blacklight::Configuration::FacetField
-
#add_facet_fields_to_solr_request!(*fields) ⇒ Object
Add any configured facet fields to the default solr parameters hash.
-
#add_field_configuration_to_solr_request!(*fields) ⇒ Object
Add any configured facet fields to the default solr parameters hash.
- #add_index_field(config_key, hash_or_field_or_array) ⇒ Blacklight::Configuration::IndexField
-
#add_nav_action(name, opts = {}) ⇒ Object
Add a partial to the header navbar.
-
#add_results_collection_tool(name, opts = {}) ⇒ Object
Add a tool for the search result list itself.
-
#add_results_document_tool(name, opts = {}) ⇒ Object
Add a partial to the tools for each document in the search results.
- #add_search_field(config_key, hash_or_field_or_array) ⇒ Blacklight::Configuration::SearchField
- #add_show_field(config_key, hash_or_field_or_array) ⇒ Blacklight::Configuration::ShowField
-
#add_show_header_tools_partial(name, opts = {}) ⇒ Object
Add a partial to the show page header when rendering a document.
-
#add_show_tools_partial(name, opts = {}) ⇒ Object
Add a partial to the tools when rendering a document.
- #add_sms_field(config_key, hash_or_field_or_array) ⇒ Blacklight::Configuration::DisplayField
- #add_sort_field(config_key, hash_or_field_or_array) ⇒ Blacklight::Configuration::SortField
-
#build(klass) ⇒ Object
(also: #inheritable_copy)
builds a copy for the provided controller class.
-
#configure {|config| ... } ⇒ Blacklight::Configuration
DSL helper.
-
#copy_facet_field_config_to_advanced! ⇒ Object
Use existing facet field configs to automatically prepare fields for use by the advanced search form.
-
#copy_search_field_config_to_advanced! ⇒ Object
Use existing search field configs to automatically prepare fields for use by the advanced search form.
-
#deep_copy ⇒ Object
Provide a 'deep copy' of Blacklight::Configuration that can be modified without effecting the original Blacklight::Configuration instance.
- #default_per_page ⇒ Integer
-
#default_search_field ⇒ Blacklight::Configuration::SearchField
Returns default search field, used for simpler display in history, etc.
-
#default_sort_field ⇒ Blacklight::Configuration::SortField
Returns default sort field, used for simpler display in history, etc.
- #default_title_field ⇒ String
-
#facet_configuration_for_field(field) ⇒ Blacklight::Configuration::FacetField
Blacklight facet configuration for the solr field.
-
#facet_field_names(group = nil) ⇒ Array<String>
A list of the facet field names from the configuration.
-
#facet_fields_in_group(group) ⇒ Array<Blacklight::Configuration::FacetField>
A list of facet fields.
-
#facet_group_names ⇒ Array<String>
A list of facet groups.
-
#for_display_type(display_type) ⇒ Object
Add a section of config that only applies to documents with a matching display type.
- #index_fields_for(display_types) ⇒ Object deprecated Deprecated.
-
#initialize(hash = {}) {|_self| ... } ⇒ Configuration
constructor
A new instance of Configuration.
-
#logo_link ⇒ String
The destination for the link around the logo in the header.
- #repository ⇒ Blacklight::Repository
- #show_fields_for(display_types) ⇒ Object deprecated Deprecated.
-
#view_config(view_type = nil, action_name: :index) ⇒ Blacklight::Configuration::ViewConfig
Get a view configuration for the given view type + action.
Methods included from Fields
Methods inherited from OpenStructWithHashAccess
#deep_dup, #deep_transform_values, #merge, #merge!, #reverse_merge, #select, #sort_by, #sort_by!, #to_h, #try
Constructor Details
#initialize(hash = {}) {|_self| ... } ⇒ Configuration
Returns a new instance of Configuration.
396 397 398 399 400 401 402 403 |
# File 'lib/blacklight/configuration.rb', line 396 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 |
Instance Attribute Details
#email_fields ⇒ Hash{Symbol=>Blacklight::Configuration::DisplayField}
394 |
# File 'lib/blacklight/configuration.rb', line 394 define_field_access :email_field, Blacklight::Configuration::DisplayField |
#facet_fields ⇒ Hash{Symbol=>Blacklight::Configuration::FacetField}
376 |
# File 'lib/blacklight/configuration.rb', line 376 define_field_access :facet_field, Blacklight::Configuration::FacetField |
#index_fields ⇒ Hash{Symbol=>Blacklight::Configuration::IndexField}
379 |
# File 'lib/blacklight/configuration.rb', line 379 define_field_access :index_field, Blacklight::Configuration::IndexField |
#search_fields ⇒ Hash{Symbol=>Blacklight::Configuration::SearchField}
385 |
# File 'lib/blacklight/configuration.rb', line 385 define_field_access :search_field, Blacklight::Configuration::SearchField |
#show_fields ⇒ Hash{Symbol=>Blacklight::Configuration::ShowField}
382 |
# File 'lib/blacklight/configuration.rb', line 382 define_field_access :show_field, Blacklight::Configuration::ShowField |
#sms_fields ⇒ Hash{Symbol=>Blacklight::Configuration::DisplayField}
391 |
# File 'lib/blacklight/configuration.rb', line 391 define_field_access :sms_field, Blacklight::Configuration::DisplayField |
#sort_fields ⇒ Hash{Symbol=>Blacklight::Configuration::SortField}
388 |
# File 'lib/blacklight/configuration.rb', line 388 define_field_access :sort_field, Blacklight::Configuration::SortField |
Class Method Details
.default_configuration(&block) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 |
# 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 ⇒ Object
28 29 30 31 |
# File 'lib/blacklight/configuration.rb', line 28 def initialize_default_configuration @default_configurations&.map(&:call) @default_configuration_initialized = true end |
.initialized_default_configuration? ⇒ Boolean
33 34 35 |
# File 'lib/blacklight/configuration.rb', line 33 def initialized_default_configuration? @default_configuration_initialized end |
.property(key, default: nil) ⇒ Object
12 13 14 |
# File 'lib/blacklight/configuration.rb', line 12 def property(key, default: nil) default_values[key] = default end |
Instance Method Details
#add_email_field(config_key, options) ⇒ Blacklight::Configuration::DisplayField #add_email_field(config_key, field) ⇒ Blacklight::Configuration::DisplayField #add_email_field(config_key, array) ⇒ Blacklight::Configuration::DisplayField
394 |
# File 'lib/blacklight/configuration.rb', line 394 define_field_access :email_field, Blacklight::Configuration::DisplayField |
#add_facet_field(config_key, options) ⇒ Blacklight::Configuration::FacetField #add_facet_field(config_key, field) ⇒ Blacklight::Configuration::FacetField #add_facet_field(config_key, array) ⇒ Blacklight::Configuration::FacetField
376 |
# File 'lib/blacklight/configuration.rb', line 376 define_field_access :facet_field, Blacklight::Configuration::FacetField |
#add_facet_fields_to_solr_request! ⇒ Object #add_facet_fields_to_solr_request!(field, field, etc) ⇒ Object
Add any configured facet fields to the default solr parameters hash
518 519 520 521 522 523 524 |
# File 'lib/blacklight/configuration.rb', line 518 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! ⇒ Object #add_field_configuration_to_solr_request!(field, field, etc) ⇒ Object
Add any configured facet fields to the default solr parameters hash
531 532 533 534 535 536 537 538 539 |
# File 'lib/blacklight/configuration.rb', line 531 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_index_field(config_key, options) ⇒ Blacklight::Configuration::IndexField #add_index_field(config_key, field) ⇒ Blacklight::Configuration::IndexField #add_index_field(config_key, array) ⇒ Blacklight::Configuration::IndexField
379 |
# File 'lib/blacklight/configuration.rb', line 379 define_field_access :index_field, Blacklight::Configuration::IndexField |
#add_nav_action(name, opts = {}) ⇒ Object
Add a partial to the header navbar
631 632 633 |
# File 'lib/blacklight/configuration.rb', line 631 def add_nav_action(name, opts = {}) add_action(.partials, name, opts) end |
#add_results_collection_tool(name, opts = {}) ⇒ Object
Add a tool for the search result list itself
619 620 621 |
# File 'lib/blacklight/configuration.rb', line 619 def add_results_collection_tool(name, opts = {}) add_action(index.collection_actions, name, opts) end |
#add_results_document_tool(name, opts = {}) ⇒ Object
Add a partial to the tools for each document in the search results.
625 626 627 |
# File 'lib/blacklight/configuration.rb', line 625 def add_results_document_tool(name, opts = {}) add_action(index.document_actions, name, opts) end |
#add_search_field(config_key, options) ⇒ Blacklight::Configuration::SearchField #add_search_field(config_key, field) ⇒ Blacklight::Configuration::SearchField #add_search_field(config_key, array) ⇒ Blacklight::Configuration::SearchField
385 |
# File 'lib/blacklight/configuration.rb', line 385 define_field_access :search_field, Blacklight::Configuration::SearchField |
#add_show_field(config_key, options) ⇒ Blacklight::Configuration::ShowField #add_show_field(config_key, field) ⇒ Blacklight::Configuration::ShowField #add_show_field(config_key, array) ⇒ Blacklight::Configuration::ShowField
382 |
# File 'lib/blacklight/configuration.rb', line 382 define_field_access :show_field, Blacklight::Configuration::ShowField |
#add_show_header_tools_partial(name, opts = {}) ⇒ Object
Add a partial to the show page header when rendering a document.
608 609 610 611 612 613 |
# File 'lib/blacklight/configuration.rb', line 608 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 = {}) ⇒ Object
Add a partial to the tools when rendering a document.
593 594 595 596 597 598 |
# File 'lib/blacklight/configuration.rb', line 593 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_sms_field(config_key, options) ⇒ Blacklight::Configuration::DisplayField #add_sms_field(config_key, field) ⇒ Blacklight::Configuration::DisplayField #add_sms_field(config_key, array) ⇒ Blacklight::Configuration::DisplayField
391 |
# File 'lib/blacklight/configuration.rb', line 391 define_field_access :sms_field, Blacklight::Configuration::DisplayField |
#add_sort_field(config_key, options) ⇒ Blacklight::Configuration::SortField #add_sort_field(config_key, field) ⇒ Blacklight::Configuration::SortField #add_sort_field(config_key, array) ⇒ Blacklight::Configuration::SortField
388 |
# File 'lib/blacklight/configuration.rb', line 388 define_field_access :sort_field, Blacklight::Configuration::SortField |
#build(klass) ⇒ Object Also known as: inheritable_copy
builds a copy for the provided controller class
552 553 554 555 556 |
# File 'lib/blacklight/configuration.rb', line 552 def build(klass) deep_copy.tap do |conf| conf.klass = klass end end |
#configure {|config| ... } ⇒ Blacklight::Configuration
DSL helper
424 425 426 427 |
# File 'lib/blacklight/configuration.rb', line 424 def configure yield self if block_given? self end |
#copy_facet_field_config_to_advanced! ⇒ Object
Use existing facet field configs to automatically prepare fields for use by the advanced search form. If more specific config is needed, e.g., to set the facet limit for one field to a value other than -1, it can be expressed in the configuration in advanced_search.form_solr_parameters
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 |
# File 'lib/blacklight/configuration.rb', line 493 def copy_facet_field_config_to_advanced! advanced_search.form_solr_parameters ||= {} advanced_search.form_solr_parameters['facet.sort'] ||= 'count' advanced_search.form_solr_parameters['facet.field'] ||= [] facet_fields.each_value do |field| next if field.include_in_advanced_search == false # Skip query-type and pivot facets that can't be requested via facet.field next if field.query || field.pivot || field.range # Add the facet field to the advanced search configuration advanced_search.form_solr_parameters['facet.field'] << field.field unless advanced_search.form_solr_parameters['facet.field'].include?(field.field) # Set the facet field's limit to -1 to show ALL values in advanced search advanced_search.form_solr_parameters["f.#{field.field}.facet.limit"] = -1 unless advanced_search.form_solr_parameters["f.#{field.field}.facet.limit"] end end |
#copy_search_field_config_to_advanced! ⇒ Object
Use existing search field configs to automatically prepare fields for use by the advanced search form.
479 480 481 482 483 484 485 486 487 |
# File 'lib/blacklight/configuration.rb', line 479 def copy_search_field_config_to_advanced! search_fields.each_value do |field| next if field.include_in_advanced_search == false next if field.clause_params field.clause_params = {} field.clause_params[:edismax] = field.solr_parameters.dup || {} end end |
#deep_copy ⇒ Object
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.
546 547 548 |
# File 'lib/blacklight/configuration.rb', line 546 def deep_copy deep_transform_values(&method(:_deep_copy)) end |
#default_per_page ⇒ Integer
416 417 418 |
# File 'lib/blacklight/configuration.rb', line 416 def default_per_page super || per_page.first end |
#default_search_field ⇒ Blacklight::Configuration::SearchField
Returns default search field, used for simpler display in history, etc. if not set, defaults to first defined search field
432 433 434 |
# File 'lib/blacklight/configuration.rb', line 432 def default_search_field @default_search_field ||= super || search_fields.values.find { |f| f.default == true } || search_fields.values.first end |
#default_sort_field ⇒ Blacklight::Configuration::SortField
Returns default sort field, used for simpler display in history, etc. if not set, defaults to first defined sort field
439 440 441 442 |
# File 'lib/blacklight/configuration.rb', line 439 def default_sort_field field = super || sort_fields.values.find { |f| f.default == true } field || sort_fields.values.first end |
#default_title_field ⇒ String
445 446 447 |
# File 'lib/blacklight/configuration.rb', line 445 def default_title_field document_model.unique_key || 'id' end |
#facet_configuration_for_field(field) ⇒ Blacklight::Configuration::FacetField
Returns Blacklight facet configuration for the solr field.
451 452 453 454 455 456 457 458 |
# File 'lib/blacklight/configuration.rb', line 451 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) ⇒ Array<String>
Returns a list of the facet field names from the configuration.
462 463 464 |
# File 'lib/blacklight/configuration.rb', line 462 def facet_field_names(group = nil) facet_fields_in_group(group).map(&:field) end |
#facet_fields_in_group(group) ⇒ Array<Blacklight::Configuration::FacetField>
Returns a list of facet fields.
468 469 470 |
# File 'lib/blacklight/configuration.rb', line 468 def facet_fields_in_group(group) facet_fields.values.select { |opts| group == opts[:group] } end |
#facet_group_names ⇒ Array<String>
Returns a list of facet groups.
473 474 475 |
# File 'lib/blacklight/configuration.rb', line 473 def facet_group_names facet_fields.map { |_facet, opts| opts[:group] }.uniq end |
#for_display_type(display_type) ⇒ Object
Add a section of config that only applies to documents with a matching display type
637 638 639 640 641 642 643 |
# File 'lib/blacklight/configuration.rb', line 637 def for_display_type(display_type, &) fields_for_type[display_type] ||= self.class.new fields_for_type[display_type].tap do |conf| yield(conf) if block_given? end end |
#index_fields_for(display_types) ⇒ Object
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
649 650 651 652 653 |
# File 'lib/blacklight/configuration.rb', line 649 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 |
#logo_link ⇒ String
Returns The destination for the link around the logo in the header.
411 412 413 |
# File 'lib/blacklight/configuration.rb', line 411 def logo_link super || Rails.application.routes.url_helpers.root_path end |
#repository ⇒ Blacklight::Repository
406 407 408 |
# File 'lib/blacklight/configuration.rb', line 406 def repository repository_class.new(self) end |
#show_fields_for(display_types) ⇒ Object
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
659 660 661 662 663 |
# File 'lib/blacklight/configuration.rb', line 659 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) ⇒ Blacklight::Configuration::ViewConfig
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
569 570 571 572 573 574 575 576 577 578 579 580 |
# File 'lib/blacklight/configuration.rb', line 569 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]] ||= action_config(action_name, (view.fetch(view_type, nil) if view_type)) end |