Skip to content

Commit

Permalink
Extract fluent interfaces for SearchBuilder start/page/rows
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeer committed Mar 20, 2015
1 parent 8588764 commit 48574ff
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 49 deletions.
66 changes: 48 additions & 18 deletions lib/blacklight/search_builder.rb
Expand Up @@ -80,34 +80,64 @@ def blacklight_config
scope.blacklight_config
end

protected
def request
Blacklight::Solr::Request.new
def start start = nil
if start
@start = start.to_i
self
else
@start ||= (page - 1) * (rows || 10)

val = @start || 0
val = 0 if @start < 0
val
end
end
alias_method :padding, :start

def page
if blacklight_params[:page].blank?
1
def page page = nil
if page
@page = page.to_i
@page = 1 if @page < 1
self
else
blacklight_params[:page].to_i
@page ||= begin
page = if blacklight_params[:page].blank?
1
else
blacklight_params[:page].to_i
end

page
end
end
end

def rows default = nil
# default number of rows
rows = default
rows ||= blacklight_config.default_per_page
rows ||= 10
def rows rows = nil
if rows
@rows = rows.to_i
@rows = blacklight_config.max_per_page if @rows > blacklight_config.max_per_page
self
else
@rows ||= begin
rows = blacklight_config.default_per_page

# user-provided parameters should override any default row
rows = blacklight_params[:rows].to_i unless blacklight_params[:rows].blank?
rows = blacklight_params[:per_page].to_i unless blacklight_params[:per_page].blank?
# user-provided parameters should override any default row
rows = blacklight_params[:rows].to_i unless blacklight_params[:rows].blank?
rows = blacklight_params[:per_page].to_i unless blacklight_params[:per_page].blank?

# ensure we don't excede the max page size
rows = blacklight_config.max_per_page if rows.to_i > blacklight_config.max_per_page
# ensure we don't excede the max page size
rows = blacklight_config.max_per_page if rows.to_i > blacklight_config.max_per_page

rows.to_i unless rows.nil?
end
end
end

rows
alias_method :per, :rows

protected
def request
Blacklight::Solr::Request.new
end

def sort
Expand Down
18 changes: 9 additions & 9 deletions lib/blacklight/search_helper.rb
Expand Up @@ -98,8 +98,11 @@ def get_search_results(user_params = params || {}, extra_controller_params = {})
# @param [List<Symbol] processor_chain a list of filter methods to run
# @return [Blacklight::SolrResponse] the solr response object
def search_results(user_params, search_params_logic)
query = search_builder(search_params_logic).with(user_params).query
response = repository.search(query)
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]

response = repository.search(builder.query)

case
when (response.grouped? && grouped_key_for_results)
Expand Down Expand Up @@ -188,21 +191,18 @@ def get_facet_pagination(facet_field, user_params=params || {}, extra_controller
# the Blacklight app-level request params that define the search.
# @return [Blacklight::SolrDocument, nil] the found document or nil if not found
def get_single_doc_via_search(index, request_params)
request_params = search_builder.with(request_params).processed_parameters

request_params[:start] = (index - 1) # start at 0 to get 1st doc, 1 to get 2nd.
request_params[:rows] = 1
request_params[:fl] = '*'
response = repository.search(request_params)
query = search_builder.with(request_params).start(index - 1).rows(1).query(fl: "*")
response = repository.search(query)
response.documents.first
end
deprecation_deprecate :get_single_doc_via_search

# Get the previous and next document from a search result
# @return [Blacklight::SolrResponse, Array<Blacklight::SolrDocument>] the solr response and a list of the first and last document
def get_previous_and_next_documents_for_search(index, request_params, extra_controller_params={})
p = previous_and_next_document_params(index)

query = search_builder.with(request_params).query(extra_controller_params.merge(previous_and_next_document_params(index)))
query = search_builder.with(request_params).start(p.delete(:start)).rows(p.delete(:rows)).query(extra_controller_params.merge(p))
response = repository.search(query)

document_list = response.documents
Expand Down
10 changes: 6 additions & 4 deletions lib/blacklight/solr/search_builder.rb
Expand Up @@ -167,10 +167,12 @@ def add_solr_fields_to_query solr_parameters
# copy paging params from BL app over to solr, changing
# app level per_page and page to Solr rows and start.
def add_paging_to_solr(solr_params)
# user-provided parameters should override any default row
solr_params[:rows] = rows(solr_params[:rows])
if page > 1
solr_params[:start] = solr_params[:rows].to_i * (page - 1)
rows(solr_params[:rows] || 10) if rows.nil?

solr_params[:rows] = rows

if start != 0
solr_params[:start] = start
end
end

Expand Down
28 changes: 15 additions & 13 deletions spec/lib/blacklight/search_builder_spec.rb
Expand Up @@ -100,31 +100,33 @@
end

describe "#rows" do
it "should be the per_page parameter" do
expect(subject.with(per_page: 5).send(:rows)).to eq 5

it "should be nil if no value is set" do
blacklight_config.default_per_page = nil
blacklight_config.per_page = []
expect(subject.rows).to be_nil
end

it "should support the legacy 'rows' parameter" do
expect(subject.with(rows: 10).send(:rows)).to eq 10
it "should set the number of rows" do
expect(subject.rows(17).rows).to eq 17
end

it "should use the provided default" do
expect(subject.send(:rows, 17)).to eq 17
it "should be the per_page parameter" do
expect(subject.with(per_page: 5).rows).to eq 5
end

it "should be set to the configured default" do
blacklight_config.default_per_page = 42
expect(subject.send(:rows)).to eq 42
it "should support the legacy 'rows' parameter" do
expect(subject.with(rows: 10).rows).to eq 10
end

it "should default to 10" do
blacklight_config.default_per_page = nil
expect(subject.send(:rows)).to eq 10
it "should be set to the configured default" do
blacklight_config.default_per_page = 42
expect(subject.rows).to eq 42
end

it "should limit the number of rows to the configured maximum" do
blacklight_config.max_per_page = 1000
expect(subject.send(:rows, 1001)).to eq 1000
expect(subject.rows(1001).rows).to eq 1000
end
end

Expand Down
5 changes: 0 additions & 5 deletions spec/lib/blacklight/solr/search_builder_spec.rb
Expand Up @@ -40,7 +40,6 @@
:qf => "fieldOne^2.3 fieldTwo fieldThree^0.4",
:pf => "",
:spellcheck => 'false',
:rows => "55",
:sort => "request_params_sort" }
)
end
Expand All @@ -62,10 +61,6 @@
expect(subject[:qt]).to eq blacklight_config[:default_solr_params][:qt]
end

it "should take rows from search field definition where specified" do
expect(subject[:rows]).to eq "55"
end

it "should take q from request params" do
expect(subject[:q]).to eq "test query"
end
Expand Down

0 comments on commit 48574ff

Please sign in to comment.