Skip to content

Commit 663277f

Browse files
authored
Merge pull request #2027 from projectblacklight/search-builder
Allow search_builder_class to be passed to SearchService
2 parents 7253213 + bf728db commit 663277f

File tree

3 files changed

+26
-11
lines changed

3 files changed

+26
-11
lines changed

app/controllers/concerns/blacklight/catalog.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ def render_sms_action?(_config, _options)
165165
end
166166

167167
def search_service
168-
search_service_class.new(blacklight_config, search_state.to_h)
168+
search_service_class.new(config: blacklight_config, user_params: search_state.to_h)
169169
end
170170

171171
##

app/services/blacklight/search_service.rb

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,15 @@
22
# SearchService returns search results from the repository
33
module Blacklight
44
class SearchService
5-
def initialize(blacklight_config, user_params = {})
6-
@blacklight_config = blacklight_config
5+
def initialize(config:, user_params: {}, search_builder_class: config.search_builder_class)
6+
@blacklight_config = config
77
@user_params = user_params
8+
@search_builder_class = search_builder_class
89
end
910

10-
# TODO: Can this be private?
11+
# The blacklight_config is accessed by the search_builder
1112
attr_reader :blacklight_config
1213

13-
# Override this method to use a search builder other than the one in the config
14-
delegate :search_builder_class, to: :blacklight_config
15-
1614
def search_builder
1715
search_builder_class.new(self)
1816
end
@@ -86,11 +84,10 @@ def opensearch_response(field = nil, extra_controller_params = {})
8684
[user_params[:q], response.documents.flat_map { |doc| doc[field] }.uniq]
8785
end
8886

89-
delegate :repository, to: :blacklight_config
90-
9187
private
9288

93-
attr_reader :user_params
89+
attr_reader :search_builder_class, :user_params
90+
delegate :repository, to: :blacklight_config
9491

9592
##
9693
# The key to use to retrieve the grouped field to display

spec/services/blacklight/search_service_spec.rb

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
RSpec.describe Blacklight::SearchService, api: true do
1212
subject { service }
1313

14-
let(:service) { described_class.new(blacklight_config, user_params) }
14+
let(:service) { described_class.new(config: blacklight_config, user_params: user_params) }
1515
let(:repository) { Blacklight::Solr::Repository.new(blacklight_config) }
1616
let(:user_params) { {} }
1717

@@ -29,6 +29,24 @@
2929
service.repository.connection = blacklight_solr
3030
end
3131

32+
describe '#search_builder_class' do
33+
subject { service.send(:search_builder_class) }
34+
35+
it 'defaults to the value in the config' do
36+
expect(subject).to eq SearchBuilder
37+
end
38+
39+
context 'when the search_builder_class is passed in' do
40+
let(:klass) { double("Search builder") }
41+
42+
let(:service) { described_class.new(config: blacklight_config, user_params: user_params, search_builder_class: klass) }
43+
44+
it 'uses the passed value' do
45+
expect(subject).to eq klass
46+
end
47+
end
48+
end
49+
3250
# SPECS FOR SEARCH RESULTS FOR QUERY
3351
describe 'Search Results', integration: true do
3452
let(:blacklight_config) { copy_of_catalog_config }

0 commit comments

Comments
 (0)