Skip to content

Commit

Permalink
Consolidate RequestBuilders into SearchService
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoyne committed Oct 17, 2017
1 parent 50c2a4f commit 3544eb8
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 44 deletions.
42 changes: 0 additions & 42 deletions app/controllers/concerns/blacklight/request_builders.rb

This file was deleted.

40 changes: 38 additions & 2 deletions app/services/blacklight/search_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
# SearchService returns search results from the repository
module Blacklight
class SearchService
include Blacklight::RequestBuilders

def initialize(blacklight_config, user_params = {})
@blacklight_config = blacklight_config
@user_params = user_params
Expand All @@ -12,6 +10,44 @@ def initialize(blacklight_config, user_params = {})
# TODO: Can this be private?
attr_reader :blacklight_config

# Override this method to use a search builder other than the one in the config
delegate :search_builder_class, to: :blacklight_config

def search_builder
search_builder_class.new(self)
end

##
# Opensearch autocomplete parameters for plucking a field's value from the results
def solr_opensearch_params(field)
solr_params = {}
solr_params[:rows] ||= 10
solr_params[:fl] = field || blacklight_config.view_config(:opensearch).title_field
solr_params
end

##
# Pagination parameters for selecting the previous and next documents
# out of a result set.
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
end

if index > 0
solr_params[:start] = index - window # get one before
solr_params[:rows] = 2 * window + 1 # and one after
else
solr_params[:start] = 0 # there is no previous doc
solr_params[:rows] = 2 * window # but there should be one after
end

solr_params[:facet] = false
solr_params
end

# a solr query method
# @param [Hash] user_params ({}) the user provided parameters (e.g. query, facets, sort, etc)
# @yield [search_builder] optional block yields configured SearchBuilder, caller can modify or create new SearchBuilder to be used. Block should return SearchBuilder to be used.
Expand Down

0 comments on commit 3544eb8

Please sign in to comment.