Skip to content

Commit

Permalink
Merge pull request #2027 from projectblacklight/search-builder
Browse files Browse the repository at this point in the history
Allow search_builder_class to be passed to SearchService
  • Loading branch information
jcoyne committed Nov 20, 2018
2 parents 7253213 + bf728db commit 663277f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 11 deletions.
2 changes: 1 addition & 1 deletion app/controllers/concerns/blacklight/catalog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ def render_sms_action?(_config, _options)
end

def search_service
search_service_class.new(blacklight_config, search_state.to_h)
search_service_class.new(config: blacklight_config, user_params: search_state.to_h)
end

##
Expand Down
15 changes: 6 additions & 9 deletions app/services/blacklight/search_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@
# SearchService returns search results from the repository
module Blacklight
class SearchService
def initialize(blacklight_config, user_params = {})
@blacklight_config = blacklight_config
def initialize(config:, user_params: {}, search_builder_class: config.search_builder_class)
@blacklight_config = config
@user_params = user_params
@search_builder_class = search_builder_class
end

# TODO: Can this be private?
# The blacklight_config is accessed by the search_builder
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
Expand Down Expand Up @@ -86,11 +84,10 @@ def opensearch_response(field = nil, extra_controller_params = {})
[user_params[:q], response.documents.flat_map { |doc| doc[field] }.uniq]
end

delegate :repository, to: :blacklight_config

private

attr_reader :user_params
attr_reader :search_builder_class, :user_params
delegate :repository, to: :blacklight_config

##
# The key to use to retrieve the grouped field to display
Expand Down
20 changes: 19 additions & 1 deletion spec/services/blacklight/search_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
RSpec.describe Blacklight::SearchService, api: true do
subject { service }

let(:service) { described_class.new(blacklight_config, user_params) }
let(:service) { described_class.new(config: blacklight_config, user_params: user_params) }
let(:repository) { Blacklight::Solr::Repository.new(blacklight_config) }
let(:user_params) { {} }

Expand All @@ -29,6 +29,24 @@
service.repository.connection = blacklight_solr
end

describe '#search_builder_class' do
subject { service.send(:search_builder_class) }

it 'defaults to the value in the config' do
expect(subject).to eq SearchBuilder
end

context 'when the search_builder_class is passed in' do
let(:klass) { double("Search builder") }

let(:service) { described_class.new(config: blacklight_config, user_params: user_params, search_builder_class: klass) }

it 'uses the passed value' do
expect(subject).to eq klass
end
end
end

# SPECS FOR SEARCH RESULTS FOR QUERY
describe 'Search Results', integration: true do
let(:blacklight_config) { copy_of_catalog_config }
Expand Down

0 comments on commit 663277f

Please sign in to comment.