Skip to content

Commit

Permalink
Introduce Hyrax::SearchService to mirror Blacklight::SearchService co…
Browse files Browse the repository at this point in the history
…ming in Blacklight 7
  • Loading branch information
cbeer authored and jeremyf committed Nov 17, 2020
1 parent 6a69155 commit aebd41f
Show file tree
Hide file tree
Showing 28 changed files with 353 additions and 173 deletions.
4 changes: 4 additions & 0 deletions .rubocop_fixme.yml
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ RSpec/AnyInstance:
- 'spec/services/hyrax/workflow/sipity_actions_generator_spec.rb'
- 'spec/services/hyrax/workflow/state_machine_generator_spec.rb'
- 'spec/services/hyrax/workflow/workflow_permissions_generator_spec.rb'
- 'spec/controllers/hyrax/homepage_controller_spec.rb'
- 'spec/controllers/hyrax/my/collections_controller_spec.rb'
- 'spec/controllers/hyrax/my/works_controller_spec.rb'
- 'spec/presenters/hyrax/admin/repository_object_presenter_spec.rb'

# Offense count: 51
RSpec/ExpectInHook:
Expand Down
26 changes: 18 additions & 8 deletions app/authorities/qa/authorities/collections.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ class Collections < Qa::Authorities::Base
def search(_q, controller)
# The Hyrax::CollectionSearchBuilder expects a current_user
return [] unless controller.current_user
repo = CatalogController.new.repository
builder = search_builder(controller)
response = repo.search(builder)
response, _docs = search_response(controller)
docs = response.documents
docs.map do |doc|
id = doc.id
Expand All @@ -20,12 +18,24 @@ def search(_q, controller)

private

def search_builder(controller)
def search_service(controller)
@search_service ||= Hyrax::SearchService.new(
config: controller.blacklight_config,
user_params: controller.params,
search_builder_class: search_builder_class,
scope: controller,
current_ability: controller.current_ability
)
end

def search_response(controller)
access = controller.params[:access] || 'read'
search_builder_class.new(controller)
.where(controller.params[:q])
.with_access(access)
.rows(100)

search_service(controller).search_results do |builder|
builder.where(controller.params[:q])
.with_access(access)
.rows(100)
end
end
end
end
25 changes: 20 additions & 5 deletions app/authorities/qa/authorities/find_works.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ class FindWorks < Qa::Authorities::Base
def search(_q, controller)
# The My::FindWorksSearchBuilder expects a current_user
return [] unless controller.current_user
repo = CatalogController.new.repository
builder = search_builder(controller)
response = repo.search(builder)

response, _docs = search_response(controller)
docs = response.documents
docs.map do |doc|
id = doc.id
Expand All @@ -20,8 +19,24 @@ def search(_q, controller)

private

def search_builder(controller)
search_builder_class.new(controller)
def search_service(controller)
@search_service ||= Hyrax::SearchService.new(
config: controller.blacklight_config,
user_params: controller.params,
search_builder_class: search_builder_class,
scope: controller,
current_ability: controller.current_ability
)
end

def search_response(controller)
access = controller.params[:access] || 'read'

search_service(controller).search_results do |builder|
builder.where(controller.params[:q])
.with_access(access)
.rows(100)
end
end
end
end
21 changes: 15 additions & 6 deletions app/controllers/concerns/hyrax/collections_controller_behavior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,29 @@ def collection

def presenter
@presenter ||= begin
# Query Solr for the collection.
# run the solr query to find the collection members
response = repository.search(single_item_search_builder.query)
curation_concern = response.documents.first
raise CanCan::AccessDenied unless curation_concern
presenter_class.new(curation_concern, current_ability)
end
end

def curation_concern
# Query Solr for the collection.
# run the solr query to find the collection members
response, _docs = search_service.search_results
curation_concern = response.documents.first
raise CanCan::AccessDenied unless curation_concern
curation_concern
end

def search_service
Hyrax::SearchService.new(config: blacklight_config, user_params: params.except(:q, :page), scope: self, search_builder_class: single_item_search_builder_class)
end

# Instantiates the search builder that builds a query for a single item
# this is useful in the show view.
def single_item_search_builder
single_item_search_builder_class.new(self).with(params.except(:q, :page))
search_service.search_builder
end
deprecation_deprecate :single_item_search_builder

def collection_params
form_class.model_attributes(params[:collection])
Expand Down
30 changes: 20 additions & 10 deletions app/controllers/hyrax/dashboard/collections_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ module Dashboard
class CollectionsController < Hyrax::My::CollectionsController
include Blacklight::AccessControls::Catalog
include Blacklight::Base

configure_blacklight do |config|
config.search_builder_class = Hyrax::Dashboard::CollectionsSearchBuilder
end

include BreadcrumbsForCollections
with_themed_layout 'dashboard'

Expand Down Expand Up @@ -192,10 +197,6 @@ def files
render json: result
end

def search_builder_class
Hyrax::Dashboard::CollectionsSearchBuilder
end

private

def default_collection_type
Expand Down Expand Up @@ -315,20 +316,29 @@ def remove_select_something_first_flash

def presenter
@presenter ||= begin
# Query Solr for the collection.
# run the solr query to find the collection members
response = repository.search(single_item_search_builder.query)
curation_concern = response.documents.first
raise CanCan::AccessDenied unless curation_concern
presenter_class.new(curation_concern, current_ability)
end
end

def curation_concern
# Query Solr for the collection.
# run the solr query to find the collection members
response, _docs = single_item_search_service.search_results
curation_concern = response.documents.first
raise CanCan::AccessDenied unless curation_concern
curation_concern
end

def single_item_search_service
Hyrax::SearchService.new(config: blacklight_config, user_params: params.except(:q, :page), scope: self, search_builder_class: single_item_search_builder_class)
end

# Instantiates the search builder that builds a query for a single item
# this is useful in the show view.
def single_item_search_builder
single_item_search_builder_class.new(self).with(params.except(:q, :page))
search_service.search_builder
end
deprecation_deprecate :single_item_search_builder

def collection_params
@participants = extract_old_style_permission_attributes(params[:collection])
Expand Down
7 changes: 3 additions & 4 deletions app/controllers/hyrax/dashboard/works_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ module Hyrax
module Dashboard
## Shows a list of all works to the admins
class WorksController < Hyrax::My::WorksController
# Search builder for a list of works
# Override of Blacklight::RequestBuilders
def search_builder_class
Hyrax::Dashboard::WorksSearchBuilder
# Define collection specific filter facets.
configure_blacklight do |config|
config.search_builder_class = Hyrax::Dashboard::WorksSearchBuilder
end

private
Expand Down
27 changes: 18 additions & 9 deletions app/controllers/hyrax/file_sets_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ class FileSetsController < ApplicationController

helper_method :curation_concern
copy_blacklight_config_from(::CatalogController)
# Define collection specific filter facets.
configure_blacklight do |config|
config.search_builder_class = Hyrax::FileSetSearchBuilder
end

class_attribute :show_presenter, :form_class
self.show_presenter = Hyrax::FileSetPresenter
Expand Down Expand Up @@ -140,11 +144,6 @@ def add_breadcrumb_for_action
end
end

# Override of Blacklight::RequestBuilders
def search_builder_class
Hyrax::FileSetSearchBuilder
end

def initialize_edit_form
@parent = @file_set.in_objects.first
original = @file_set.original_file
Expand All @@ -162,13 +161,23 @@ def attributes

def presenter
@presenter ||= begin
_, document_list = search_results(params)
curation_concern = document_list.first
raise CanCan::AccessDenied unless curation_concern
show_presenter.new(curation_concern, current_ability, request)
show_presenter.new(curation_concern_document, current_ability, request)
end
end

def curation_concern_document
# Query Solr for the collection.
# run the solr query to find the collection members
response, _docs = single_item_search_service.search_results
curation_concern = response.documents.first
raise CanCan::AccessDenied unless curation_concern
curation_concern
end

def single_item_search_service
Hyrax::SearchService.new(config: blacklight_config, user_params: params.except(:q, :page), scope: self, search_builder_class: search_builder_class)
end

def wants_to_revert?
params.key?(:revision) && params[:revision] != curation_concern.latest_content_version.label
end
Expand Down
22 changes: 11 additions & 11 deletions app/controllers/hyrax/homepage_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,6 @@ class Hyrax::HomepageController < ApplicationController
include Blacklight::SearchHelper
include Blacklight::AccessControls::Catalog

# The search builder for finding recent documents
# Override of Blacklight::RequestBuilders
def search_builder_class
Hyrax::HomepageSearchBuilder
end

class_attribute :presenter_class
self.presenter_class = Hyrax::HomepagePresenter
layout 'homepage'
Expand All @@ -29,21 +23,27 @@ def index

# Return 5 collections
def collections(rows: 5)
builder = Hyrax::CollectionSearchBuilder.new(self)
.rows(rows)
response = repository.search(builder)
response.documents
Hyrax::CollectionsService.new(self).search_results do |builder|
builder.rows(rows)
end
rescue Blacklight::Exceptions::ECONNREFUSED, Blacklight::Exceptions::InvalidRequest
[]
end

def recent
# grab any recent documents
(_, @recent_documents) = search_results(q: '', sort: sort_field, rows: 4)
(_, @recent_documents) = search_service.search_results do |builder|
builder.rows(4)
builder.merge(sort: sort_field)
end
rescue Blacklight::Exceptions::ECONNREFUSED, Blacklight::Exceptions::InvalidRequest
@recent_documents = []
end

def search_service
Hyrax::SearchService.new(config: blacklight_config, user_params: { q: '' }, scope: self, search_builder_class: Hyrax::HomepageSearchBuilder)
end

def sort_field
"date_uploaded_dtsi desc"
end
Expand Down
7 changes: 3 additions & 4 deletions app/controllers/hyrax/my/collections_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
module Hyrax
module My
class CollectionsController < MyController
configure_blacklight do |config|
config.search_builder_class = Hyrax::My::CollectionsSearchBuilder
end
# Define collection specific filter facets.
def self.configure_facets
configure_blacklight do |config|
Expand All @@ -18,10 +21,6 @@ def self.configure_facets
end
configure_facets

def search_builder_class
Hyrax::My::CollectionsSearchBuilder
end

def index
add_breadcrumb t(:'hyrax.controls.home'), root_path
add_breadcrumb t(:'hyrax.dashboard.breadcrumbs.admin'), hyrax.dashboard_path
Expand Down
5 changes: 2 additions & 3 deletions app/controllers/hyrax/my/highlights_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
module Hyrax
module My
class HighlightsController < MyController
# Override of Blacklight::RequestBuilders
def search_builder_class
Hyrax::My::HighlightsSearchBuilder
configure_blacklight do |config|
config.search_builder_class = Hyrax::My::HighlightsSearchBuilder
end

def index
Expand Down
5 changes: 2 additions & 3 deletions app/controllers/hyrax/my/shares_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
module Hyrax
module My
class SharesController < MyController
# Override of Blacklight::RequestBuilders
def search_builder_class
Hyrax::My::SharesSearchBuilder
configure_blacklight do |config|
config.search_builder_class = Hyrax::My::SharesSearchBuilder
end

def index
Expand Down
7 changes: 1 addition & 6 deletions app/controllers/hyrax/my/works_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class WorksController < MyController
# Define collection specific filter facets.
def self.configure_facets
configure_blacklight do |config|
config.search_builder_class = Hyrax::My::WorksSearchBuilder
config.add_facet_field "admin_set_sim", limit: 5
config.add_facet_field "member_of_collections_ssim", limit: 5
end
Expand All @@ -14,12 +15,6 @@ def self.configure_facets
class_attribute :create_work_presenter_class
self.create_work_presenter_class = Hyrax::SelectTypeListPresenter

# Search builder for a list of works that belong to me
# Override of Blacklight::RequestBuilders
def search_builder_class
Hyrax::My::WorksSearchBuilder
end

def index
# The user's collections for the "add to collection" form
@user_collections = collections_service.search_results(:deposit)
Expand Down
12 changes: 10 additions & 2 deletions app/controllers/hyrax/my_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true
module Hyrax
class MyController < ApplicationController
extend Deprecation
include Hydra::Catalog
include Hyrax::Collections::AcceptsBatches

Expand Down Expand Up @@ -35,7 +36,9 @@ def self.configure_facets

def index
@user = current_user
(@response, @document_list) = query_solr
Deprecation.silence(Hyrax::MyController) do
(@response, @document_list) = query_solr
end
prepare_instance_variables_for_batch_control_display

respond_to do |format|
Expand All @@ -61,7 +64,12 @@ def prepare_instance_variables_for_batch_control_display
end

def query_solr
search_results(params)
search_service.search_results
end
deprecation_deprecate :query_solr

def search_service
Hyrax::SearchService.new(config: blacklight_config, user_params: params, scope: self)
end
end
end
Loading

0 comments on commit aebd41f

Please sign in to comment.