Skip to content

Commit

Permalink
add a proc mechanism to Blacklight::Configuration#property to allow d…
Browse files Browse the repository at this point in the history
…eferred resolution of some default values
  • Loading branch information
cbeer committed Oct 6, 2022
1 parent 388dd82 commit d2fb198
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
28 changes: 18 additions & 10 deletions lib/blacklight/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,13 @@ class Configuration < OpenStructWithHashAccess
autoload :ShowField
end

class_attribute :default_values, default: {}

# Set up Blacklight::Configuration.default_values to contain the basic, required Blacklight fields
class << self
def property(key, default: nil)
default_values[key] = default
end

def default_values
@default_values ||= {}
end
end
# === Search request configuration

Expand Down Expand Up @@ -77,28 +75,28 @@ def default_values
property :repository_class, default: Blacklight::Solr::Repository
# @!attribute search_builder_class
# @return [Class] class for converting Blacklight parameters to request parameters for the repository_class
property :search_builder_class, default: ::SearchBuilder
property :search_builder_class, default: -> { ::SearchBuilder }
# @!attribute response_model
# model that maps index responses to the blacklight response model
# @return [Class]
property :response_model, default: Blacklight::Solr::Response
# @!attribute document_model
# the model to use for each response document
# @return [Class]
property :document_model, default: ::SolrDocument
property :document_model, default: -> { ::SolrDocument }
# @!attribute document_factory
# the factory that builds document
# @return [Class]
property :document_factory, default: Blacklight::DocumentFactory
property :document_factory, default: -> { Blacklight::DocumentFactory }
# @!attribute facet_paginator_class
# Class for paginating long lists of facet fields
# @return [Class]
property :facet_paginator_class, default: Blacklight::Solr::FacetPaginator
property :facet_paginator_class, default: -> { Blacklight::Solr::FacetPaginator }
# @!attribute connection_config
# repository connection configuration
# @since v5.13.0
# @return [Class]
property :connection_config, default: Blacklight.connection_config
property :connection_config, default: -> { Blacklight.connection_config }

##
# == Blacklight view configuration
Expand Down Expand Up @@ -315,7 +313,7 @@ def default_values
define_field_access :email_field, Blacklight::Configuration::DisplayField

def initialize(hash = {})
super(self.class.default_values.deep_transform_values(&method(:_deep_copy)).merge(hash))
super(self.class.default_values.transform_values(&method(:process_dynamic_config)).deep_transform_values(&method(:_deep_copy)).merge(hash))
yield(self) if block_given?

@view_config ||= {}
Expand Down Expand Up @@ -562,6 +560,16 @@ def _deep_copy(value)
end
end

# Resolve default values that need to be evaluated at run-time
def process_dynamic_config(value)
case value
when Proc
value.call
else
value
end
end

def action_config(action, default: :index)
action_config = action_mapping[action]
action_config ||= action_mapping[:default]
Expand Down
4 changes: 4 additions & 0 deletions spec/models/blacklight/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@

expect(config.a).to eq 3
end

it "resolves default properties given as procs" do
expect(config.document_model).to eq(SolrDocument)
end
end

describe "defaults" do
Expand Down

0 comments on commit d2fb198

Please sign in to comment.