Skip to content

Commit

Permalink
SearchHelper#search_results takes optional block
Browse files Browse the repository at this point in the history
Used to configure or replace the SearchBuilder
  • Loading branch information
jrochkind committed Aug 27, 2015
1 parent 88c81fe commit 95ee46f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/blacklight/search_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,18 @@ def get_search_results(user_params = params || {}, extra_controller_params = {})

# a solr query method
# @param [Hash,HashWithIndifferentAccess] user_params ({}) the user provided parameters (e.g. query, facets, sort, etc)
# @param [Hash,HashWithIndifferentAccess] extra_controller_params ({}) extra parameters to add to the search
# @param [List<Symbol] processor_chain a list of filter methods to run
# @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.
# @return [Blacklight::SolrResponse] the solr response object
def search_results(user_params, search_params_logic)
builder = search_builder(search_params_logic).with(user_params)
builder.page(user_params[:page]) if user_params[:page]
builder.rows(user_params[:per_page] || user_params[:rows]) if user_params[:per_page] or user_params[:rows]

if block_given?
builder = yield(builder)
end

response = repository.search(builder)

case
Expand Down
22 changes: 22 additions & 0 deletions spec/lib/blacklight/search_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,28 @@ def params
end
end

describe "with SearchBuilder replacement block" do
it "should pass configured SearchBuilder and use returned SearchBuilder" do
# Prevent actual trip to Solr, we don't need it.
allow(blacklight_solr).to receive(:send_and_receive).with('select', kind_of(Hash)).and_return({'response'=>{'docs'=>[]}})

replacement_search_builder = subject.search_builder([:new_chain])

# Sorry, have to use mocks to make sure method really passes the
# block return value to the repository search, couldn't figure
# out a better way to test.
expect(subject.repository).to receive(:search) do |arg_search_builder|
expect(arg_search_builder).to equal(replacement_search_builder)
end

subject.search_results({q: @no_docs_query}, [:one, :two]) do |arg_search_builder|
expect(arg_search_builder.processor_chain).to eq([:one, :two])

replacement_search_builder
end
end
end

describe "#get_search_results " do
it "should be deprecated and return results" do
expect(Deprecation).to receive(:warn)
Expand Down

0 comments on commit 95ee46f

Please sign in to comment.