Skip to content

Commit

Permalink
Refactor SearchHelper module to SearchService class
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoyne committed Jan 31, 2017
1 parent df238eb commit c064a0f
Show file tree
Hide file tree
Showing 12 changed files with 297 additions and 357 deletions.
2 changes: 0 additions & 2 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ Style/Documentation:
# SupportedStyles: leading, trailing
Style/DotPosition:
Exclude:
- 'app/controllers/concerns/blacklight/search_helper.rb'
- 'lib/blacklight/search_builder.rb'

# Offense count: 1
Expand Down Expand Up @@ -240,7 +239,6 @@ Style/MultilineIfModifier:
# SupportedStyles: aligned, indented, indented_relative_to_receiver
Style/MultilineMethodCallIndentation:
Exclude:
- 'app/controllers/concerns/blacklight/search_helper.rb'
- 'lib/blacklight/search_builder.rb'

# Offense count: 12
Expand Down
1 change: 0 additions & 1 deletion app/controllers/concerns/blacklight/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@ module Blacklight::Base
extend ActiveSupport::Concern

include Blacklight::Configurable
include Blacklight::SearchHelper
include Blacklight::SearchContext
end
10 changes: 6 additions & 4 deletions app/controllers/concerns/blacklight/bookmarks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ module Blacklight::Bookmarks
##
# Give Bookmarks access to the CatalogController configuration
include Blacklight::Configurable
include Blacklight::SearchHelper
include Blacklight::TokenBasedUser

copy_blacklight_config_from(CatalogController)
Expand All @@ -25,7 +24,7 @@ module Blacklight::Bookmarks
def action_documents
bookmarks = token_or_current_or_guest_user.bookmarks
bookmark_ids = bookmarks.collect { |b| b.document_id.to_s }
fetch(bookmark_ids)
search_service.fetch(bookmark_ids, search_state.page_params)
end

def action_success_redirect_path
Expand All @@ -41,8 +40,7 @@ def search_action_url *args
def index
@bookmarks = token_or_current_or_guest_user.bookmarks
bookmark_ids = @bookmarks.collect { |b| b.document_id.to_s }

@response, deprecated_document_list = fetch(bookmark_ids)
@response, deprecated_document_list = search_service.fetch(bookmark_ids, search_state.page_params)
@document_list = ActiveSupport::Deprecation::DeprecatedObjectProxy.new(deprecated_document_list, "The @document_list instance variable is now deprecated and will be removed in Blacklight 8.0")

respond_to do |format|
Expand Down Expand Up @@ -136,6 +134,10 @@ def clear

protected

def search_service
Blacklight::SearchService.new(blacklight_config)
end

def verify_user
unless current_or_guest_user || (action == "index" && token_or_current_or_guest_user)
flash[:notice] = I18n.t('blacklight.bookmarks.need_login')
Expand Down
45 changes: 38 additions & 7 deletions app/controllers/concerns/blacklight/catalog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module Blacklight::Catalog
# The following code is executed when someone includes blacklight::catalog in their
# own controller.
included do
helper_method :sms_mappings, :has_search_parameters?
helper_method :sms_mappings, :has_search_parameters?, :facet_limit_for

helper Blacklight::Facet

Expand All @@ -22,7 +22,7 @@ module Blacklight::Catalog

# get search results from the solr index
def index
(@response, deprecated_document_list) = search_results(params)
(@response, deprecated_document_list) = search_service.search_results(params)

@document_list = ActiveSupport::Deprecation::DeprecatedObjectProxy.new(deprecated_document_list, 'The @document_list instance variable is deprecated; use @response.documents instead.')

Expand All @@ -43,7 +43,7 @@ def index
# get a single document from the index
# to add responses for formats other than html or json see _Blacklight::Document::Export_
def show
deprecated_response, @document = fetch params[:id]
deprecated_response, @document = search_service.fetch(params[:id])
@response = ActiveSupport::Deprecation::DeprecatedObjectProxy.new(deprecated_response, 'The @response instance variable is deprecated; use @document.response instead.')

respond_to do |format|
Expand All @@ -70,7 +70,7 @@ def track
# displays values and pagination links for a single facet field
def facet
@facet = blacklight_config.facet_fields[params[:id]]
@response = get_facet_field_response(@facet.key, params)
@response = search_service.facet_field_response(@facet.key, params)
@display_facet = @response.aggregations[@facet.key]
@pagination = facet_paginator(@facet, @display_facet)
respond_to do |format|
Expand All @@ -86,7 +86,7 @@ def facet
def opensearch
respond_to do |format|
format.xml { render layout: false }
format.json { render json: get_opensearch_response }
format.json { render json: search_service.opensearch_response(params) }
end
end

Expand All @@ -99,7 +99,7 @@ def suggest
end

def action_documents
fetch(Array(params[:id]))
search_service.fetch(Array(params[:id]))
end

def action_success_redirect_path
Expand All @@ -113,12 +113,43 @@ def has_search_parameters?
!params[:q].blank? || !params[:f].blank? || !params[:search_field].blank?
end

DEFAULT_FACET_LIMIT = 10

# Look up facet limit for given facet_field. Will look at config, and
# if config is 'true' will look up from Solr @response if available. If
# no limit is avaialble, returns nil. Used from #add_facetting_to_solr
# to supply f.fieldname.facet.limit values in solr request (no @response
# available), and used in display (with @response available) to create
# a facet paginator with the right limit.
def facet_limit_for(facet_field)
facet = blacklight_config.facet_fields[facet_field]
return if facet.blank?

if facet.limit && @response && @response.aggregations[facet_field]
limit = @response.aggregations[facet_field].limit

if limit.nil? # we didn't get or a set a limit, so infer one.
facet.limit if facet.limit != true
elsif limit == -1 # limit -1 is solr-speak for unlimited
nil
else
limit.to_i - 1 # we added 1 to find out if we needed to paginate
end
elsif facet.limit
facet.limit == true ? DEFAULT_FACET_LIMIT : facet.limit
end
end

protected

#
# non-routable methods ->
#

def search_service
Blacklight::SearchService.new(blacklight_config)
end

##
# If the params specify a view, then store it in the session. If the params
# do not specifiy the view, set the view parameter to the value stored in the
Expand Down Expand Up @@ -247,7 +278,7 @@ def start_new_search_session?
end

def suggestions_service
Blacklight::SuggestSearch.new(params, repository).suggestions
Blacklight::SuggestSearch.new(params, search_service.repository).suggestions
end

def determine_layout
Expand Down
35 changes: 0 additions & 35 deletions app/controllers/concerns/blacklight/request_builders.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
# frozen_string_literal: true
module Blacklight
module RequestBuilders
extend ActiveSupport::Concern

included do
if respond_to?(:helper_method)
helper_method(:facet_limit_for)
end
end

# Override this method to use a search builder other than the one in the config
delegate :search_builder_class, to: :blacklight_config

Expand Down Expand Up @@ -43,32 +35,5 @@ def previous_and_next_document_params(index, window = 1)
solr_params[:facet] = false
solr_params
end

DEFAULT_FACET_LIMIT = 10

# Look up facet limit for given facet_field. Will look at config, and
# if config is 'true' will look up from Solr @response if available. If
# no limit is avaialble, returns nil. Used from #add_facetting_to_solr
# to supply f.fieldname.facet.limit values in solr request (no @response
# available), and used in display (with @response available) to create
# a facet paginator with the right limit.
def facet_limit_for(facet_field)
facet = blacklight_config.facet_fields[facet_field]
return if facet.blank?

if facet.limit && @response && @response.aggregations[facet_field]
limit = @response.aggregations[facet_field].limit

if limit.nil? # we didn't get or a set a limit, so infer one.
facet.limit if facet.limit != true
elsif limit == -1 # limit -1 is solr-speak for unlimited
nil
else
limit.to_i - 1 # we added 1 to find out if we needed to paginate
end
elsif facet.limit
facet.limit == true ? DEFAULT_FACET_LIMIT : facet.limit
end
end
end
end
2 changes: 1 addition & 1 deletion app/controllers/concerns/blacklight/search_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def blacklisted_search_session_params
def setup_next_and_previous_documents
if search_session['counter'] && current_search_session
index = search_session['counter'].to_i - 1
response, documents = get_previous_and_next_documents_for_search index, ActiveSupport::HashWithIndifferentAccess.new(current_search_session.query_params)
response, documents = search_service.previous_and_next_documents_for_search index, ActiveSupport::HashWithIndifferentAccess.new(current_search_session.query_params)

search_session['total'] = response.total
@search_context_response = response
Expand Down
155 changes: 0 additions & 155 deletions app/controllers/concerns/blacklight/search_helper.rb

This file was deleted.

0 comments on commit c064a0f

Please sign in to comment.