Skip to content

Commit

Permalink
move repository configuration to a dedicated config class
Browse files Browse the repository at this point in the history
- deprecate Blacklight::Controller accessors for search service and search state classes
- deprecate Blacklight::Configuration properties for repository, builder, connection, response, document and paginator classes
- replace search_state_class access with search_state.reset
- Blacklight::Searchable and Blacklight::SearchService implement search_state_for(user_params)
  • Loading branch information
barmintor committed May 17, 2022
1 parent 7d617ca commit ea2002e
Show file tree
Hide file tree
Showing 15 changed files with 178 additions and 101 deletions.
4 changes: 2 additions & 2 deletions app/controllers/concerns/blacklight/bookmarks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def create
@bookmarks = if params[:bookmarks]
permit_bookmarks[:bookmarks]
else
[{ document_id: params[:id], document_type: blacklight_config.document_model.to_s }]
[{ document_id: params[:id], document_type: blacklight_config.repository_config.document_model.to_s }]
end

current_or_guest_user.save! unless current_or_guest_user.persisted?
Expand Down Expand Up @@ -102,7 +102,7 @@ def destroy
if params[:bookmarks]
permit_bookmarks[:bookmarks]
else
[{ document_id: params[:id], document_type: blacklight_config.document_model.to_s }]
[{ document_id: params[:id], document_type: blacklight_config.repository_config.document_model.to_s }]
end

success = @bookmarks.all? do |bookmark|
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/concerns/blacklight/catalog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def action_documents
end

def action_success_redirect_path
search_state.url_for_document(blacklight_config.document_model.new(id: params[:id]))
search_state.url_for_document(blacklight_config.repository_config.document_model.new(id: params[:id]))
end

##
Expand Down
34 changes: 28 additions & 6 deletions app/controllers/concerns/blacklight/controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,38 @@ module Blacklight::Controller

# Which class to use for the search state. You can subclass SearchState if you
# want to override any of the methods (e.g. SearchState#url_for_document)
# TODO: move to Searchable
class_attribute :search_state_class
self.search_state_class = Blacklight::SearchState
def self.search_state_class
Deprecation.warn(Blacklight::Controller, 'Controller.search_state_class moved to blacklight_config.repository_config')
blacklight_config.repository_config.search_state_class
end

def self.search_state_class=(klass)
Deprecation.warn(Blacklight::Controller, 'Controller.search_state_class= moved to blacklight_config.repository_config')
blacklight_config.repository_config.search_state_class = klass
end

# Which class to use for the search service. You can subclass SearchService if you
# want to override any of the methods (e.g. SearchService#fetch)
# TODO: move to Searchable
class_attribute :search_service_class
self.search_service_class = Blacklight::SearchService
def self.search_service_class
Deprecation.warn(Blacklight::Controller, 'Controller.search_service_class moved to blacklight_config.repository_config')
blacklight_config.repository_config.search_service_class
end

def self.search_service_class=(klass)
Deprecation.warn(Blacklight::Controller, 'Controller.search_service_class= moved to blacklight_config.repository_config')
blacklight_config.repository_config.search_service_class = klass
end
end

def search_state_class
blacklight_config.repository_config.search_state_class
end
deprecation_deprecate search_service_class: 'Controller#search_state_class moved to blacklight_config.repository_config'

def search_service_class
blacklight_config.repository_config.search_service_class
end
deprecation_deprecate search_service_class: 'Controller#search_service_class moved to blacklight_config.repository_config'

# @private
def default_catalog_controller
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/concerns/blacklight/facet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module Facet
# @param [Object] response_data
# @return [Blacklight::FacetPaginator]
def facet_paginator(field_config, response_data)
blacklight_config.facet_paginator_class.new(
blacklight_config.repository_config.facet_paginator_class.new(
response_data.items,
sort: response_data.sort,
offset: response_data.offset,
Expand Down
10 changes: 7 additions & 3 deletions app/controllers/concerns/blacklight/searchable.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
# frozen_string_literal: true

# The Searchable module can be included onto classes that need to initialize a SearchService.
# There are three dependencies you must provide on the including class. Typically these
# There are two dependencies you must provide on the including class. Typically these
# would be provided by Blacklight::Controller
# 1. search_state
# 2. blacklight_config
# 3. search_service_class
#
# Additionally, the including class may override the search_service_context method to provide
# further context to the SearchService. For example you could override this to provide the
# currently signed in user.
module Blacklight::Searchable
# @return [Blacklight::SearchService]
def search_service
search_service_class.new(config: blacklight_config, search_state: search_state, user_params: search_state.to_h, **search_service_context)
blacklight_config.repository_config.search_service_class.new(config: blacklight_config, search_state: search_state, user_params: search_state.to_h, **search_service_context)
end

# @return [Hash] a hash of context information to pass through to the search service
Expand All @@ -25,4 +24,9 @@ def search_service_context
def suggestions_service
Blacklight::SuggestSearch.new(params, search_service.repository).suggestions
end

# @return [SearchState]
def search_state_for(new_params)
search_state.reset(new_params)
end
end
4 changes: 2 additions & 2 deletions app/presenters/blacklight/document_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def field_presenters
def heading
return field_value(view_config.title_field) if view_config.title_field.is_a? Blacklight::Configuration::Field

fields = Array.wrap(view_config.title_field) + [configuration.document_model.unique_key]
fields = Array.wrap(view_config.title_field) + [configuration.repository_config.document_model.unique_key]
f = fields.lazy.map { |field| field_config(field) }.detect { |field_config| field_presenter(field_config).any? }
f ? field_value(f, except_operations: [Rendering::HelperMethod]) : ""
end
Expand All @@ -59,7 +59,7 @@ def html_title
return field_value(view_config.html_title_field) if view_config.html_title_field.is_a? Blacklight::Configuration::Field

if view_config.html_title_field
fields = Array.wrap(view_config.html_title_field) + [configuration.document_model.unique_key]
fields = Array.wrap(view_config.html_title_field) + [configuration.repository_config.document_model.unique_key]
f = fields.lazy.map { |field| field_config(field) }.detect { |field_config| field_presenter(field_config).any? }
field_value(f)
else
Expand Down
17 changes: 9 additions & 8 deletions app/services/blacklight/search_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
# SearchService returns search results from the repository
module Blacklight
class SearchService
def initialize(config:, search_state: nil, user_params: nil, search_builder_class: config.search_builder_class, **context)
def initialize(config:, search_state: nil, user_params: nil, search_builder_class: nil, **context)
@blacklight_config = config
@search_state = search_state || Blacklight::SearchState.new(user_params || {}, config)
@search_state = search_state || config.repository_config.search_state_class.new(user_params || {}, config)
@user_params = @search_state.params
@search_builder_class = search_builder_class
@search_builder_class = search_builder_class || config.repository_config.search_builder_class
@context = context
end

Expand All @@ -17,8 +17,9 @@ def search_builder
search_builder_class.new(self)
end

def search_state_class
@search_state.class
# @return [SearchState]
def search_state_for(new_params)
search_state.reset(new_params)
end

# a solr query method
Expand Down Expand Up @@ -64,7 +65,7 @@ def facet_field_response(facet_field, extra_controller_params = {})
# @return [Blacklight::Solr::Response, Array<Blacklight::SolrDocument>] the solr response and a list of the first and last document
def previous_and_next_documents_for_search(index, request_params, extra_controller_params = {})
p = previous_and_next_document_params(index)
new_state = request_params.is_a?(Blacklight::SearchState) ? request_params : Blacklight::SearchState.new(request_params, blacklight_config)
new_state = request_params.is_a?(Blacklight::SearchState) ? request_params : search_state.reset(request_params)
query = search_builder.with(new_state).start(p.delete(:start)).rows(p.delete(:rows)).merge(extra_controller_params).merge(p)
response = repository.search(query)
document_list = response.documents
Expand Down Expand Up @@ -118,7 +119,7 @@ def previous_and_next_document_params(index, window = 1)
solr_params = blacklight_config.document_pagination_params.dup

if solr_params.empty?
solr_params[:fl] = blacklight_config.document_model.unique_key
solr_params[:fl] = blacklight_config.repository_config.document_model.unique_key
end

if index > 0
Expand All @@ -142,7 +143,7 @@ def fetch_many(ids, extra_controller_params)

query = search_builder
.with(search_state)
.where(blacklight_config.document_model.unique_key => ids)
.where(blacklight_config.repository_config.document_model.unique_key => ids)
.merge(blacklight_config.fetch_many_document_params)
.merge(extra_controller_params)

Expand Down
2 changes: 1 addition & 1 deletion lib/blacklight/abstract_repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def ping
private

def connection_config
blacklight_config.connection_config
blacklight_config.repository_config.connection_config
end

def logger
Expand Down
82 changes: 14 additions & 68 deletions lib/blacklight/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module Blacklight
# Blacklight::Configuration holds the configuration for a Blacklight::Controller, including
# fields to display, facets to show, sort options, and search fields.
class Configuration < OpenStructWithHashAccess
extend Forwardable
extend ActiveSupport::Autoload
eager_autoload do
autoload :Context
Expand All @@ -18,6 +19,7 @@ class Configuration < OpenStructWithHashAccess
autoload :DisplayField
autoload :IndexField
autoload :ShowField
autoload :RepositoryConfig
end

# Set up Blacklight::Configuration.default_values to contain the basic, required Blacklight fields
Expand Down Expand Up @@ -69,73 +71,17 @@ def default_values

##
# == Response models
property :repository_config, default: RepositoryConfig.new

# @!attribute repository_class
# @return [Class] Class for sending and receiving requests from a search index
property :repository_class, default: nil
def repository_class
super || Blacklight::Solr::Repository
end

# @!attribute search_builder_class
# @return [Class] class for converting Blacklight parameters to request parameters for the repository_class
property :search_builder_class, default: nil
def search_builder_class
super || locate_search_builder_class
end

def locate_search_builder_class
::SearchBuilder
end

# @!attribute response_model
# model that maps index responses to the blacklight response model
# @return [Class]
property :response_model, default: nil
def response_model
super || Blacklight::Solr::Response
end

def response_model=(*args)
super
end

# @!attribute document_factory
# the factory that builds document
# @return [Class]
property :document_factory, default: nil
# A class that builds documents
def document_factory
super || Blacklight::DocumentFactory
end
# @!attribute document_model
# the model to use for each response document
# @return [Class]
property :document_model, default: nil
def document_model
super || ::SolrDocument
end

# only here to support alias_method
def document_model=(*args)
super
end

# @!attribute facet_paginator_class
# Class for paginating long lists of facet fields
# @return [Class]
property :facet_paginator_class, default: nil
def facet_paginator_class
super || Blacklight::Solr::FacetPaginator
end

# @!attribute connection_config
# repository connection configuration
# @since v5.13.0
# @return [Class]
property :connection_config, default: nil
def connection_config
super || Blacklight.connection_config
DEPRECATED_REPOSITORY_CONFIG_ACCESSORS = [
:repository_class, :repository_class=, :search_builder_class, :search_builder_class=,
:connection_config, :connection_config=, :response_model, :response_model=,
:document_factory, :document_factory=, :document_model, :document_model=,
:facet_paginator_class, :facet_paginator_class=
].freeze
def_delegators :repository_config, *DEPRECATED_REPOSITORY_CONFIG_ACCESSORS
DEPRECATED_REPOSITORY_CONFIG_ACCESSORS.each do |accessor|
deprecation_deprecate accessor => 'moved to Configuration.repository_config'
end

##
Expand Down Expand Up @@ -358,7 +304,7 @@ def initialize(hash = {})

# @return [Blacklight::Repository]
def repository
repository_class.new(self)
repository_config.repository_class.new(self)
end

# DSL helper
Expand Down Expand Up @@ -388,7 +334,7 @@ def default_sort_field

# @return [String]
def default_title_field
document_model.unique_key || 'id'
repository_config.document_model.unique_key || 'id'
end

# @param [String] field Solr facet name
Expand Down
Loading

0 comments on commit ea2002e

Please sign in to comment.