Skip to content

Commit fb638d6

Browse files
committed
Extract a search_builder for a find one search
This gives us parity with the find many search. It allows us to always go to the search_builder for queries about the request.
1 parent de5ddbb commit fb638d6

File tree

3 files changed

+29
-6
lines changed

3 files changed

+29
-6
lines changed

lib/blacklight/solr/repository.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ class Repository < Blacklight::AbstractRepository
77
# @param [String] id document's unique key value
88
# @param [Hash] params additional solr query parameters
99
def find id, params = {}
10-
doc_params = params.reverse_merge(blacklight_config.default_document_solr_params)
11-
.reverse_merge(qt: blacklight_config.document_solr_request_handler)
12-
.merge(blacklight_config.document_unique_id_param => id)
10+
doc_params = SingleDocSearchBuilder.new(self, id, params)
1311

1412
solr_response = send_and_receive blacklight_config.document_solr_path || blacklight_config.solr_path, doc_params
1513
raise Blacklight::Exceptions::RecordNotFound if solr_response.documents.empty?
@@ -25,7 +23,7 @@ def find_many(params)
2523

2624
##
2725
# Execute a search query against solr
28-
# @param [Hash] params solr query parameters
26+
# @param [Hash,Blacklight::SearchBuilder] params solr query parameters
2927
# @param [String] path solr request handler path
3028
def search pos_params = nil, path: nil, params: nil, **kwargs
3129
if pos_params
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# frozen_string_literal: true
2+
3+
module Blacklight::Solr
4+
class SingleDocSearchBuilder < SearchBuilder
5+
self.default_processor_chain = [:add_defaults, :add_qt, :add_unique_id]
6+
7+
def initialize(scope, id, other_params)
8+
@other_params = other_params
9+
@id = id
10+
super(scope)
11+
end
12+
13+
def add_defaults(request)
14+
request.reverse_merge!(blacklight_config.default_document_solr_params).reverse_merge!(@other_params)
15+
end
16+
17+
def add_qt(request)
18+
request[:qt] ||= blacklight_config.document_solr_request_handler if blacklight_config.document_solr_request_handler
19+
end
20+
21+
def add_unique_id(request)
22+
request[blacklight_config.document_unique_id_param] = @id
23+
end
24+
end
25+
end

spec/models/blacklight/solr/repository_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,14 @@
5252

5353
it "uses the provided :qt param" do
5454
blacklight_config.document_solr_request_handler = 'xyz'
55-
allow(subject.connection).to receive(:send_and_receive).with('select', hash_including(params: { id: '123', qt: 'abc' })).and_return(mock_response)
55+
allow(subject.connection).to receive(:send_and_receive).with('select', hash_including(params: { 'id' => '123', 'qt' => 'abc' })).and_return(mock_response)
5656
expect(subject.find("123", qt: 'abc')).to be_a Blacklight::Solr::Response
5757
end
5858

5959
it "uses the :qt parameter from the default_document_solr_params" do
6060
blacklight_config.default_document_solr_params[:qt] = 'abc'
6161
blacklight_config.document_solr_request_handler = 'xyz'
62-
allow(subject.connection).to receive(:send_and_receive).with('select', hash_including(params: { id: '123', qt: 'abc' })).and_return(mock_response)
62+
allow(subject.connection).to receive(:send_and_receive).with('select', hash_including(params: { 'id' => '123', 'qt' => 'abc' })).and_return(mock_response)
6363
expect(subject.find("123")).to be_a Blacklight::Solr::Response
6464
end
6565
end

0 commit comments

Comments
 (0)