Skip to content

Commit

Permalink
Upgrade to Blacklight 6
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoyne committed Jan 22, 2016
1 parent 98a900e commit 4544469
Show file tree
Hide file tree
Showing 32 changed files with 186 additions and 95 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ if File.exists?(file)
else
Bundler.ui.warn "[EngineCart] Unable to find test application dependencies in #{file}, using placeholder dependencies"
gem 'rails', ENV['RAILS_VERSION'] if ENV['RAILS_VERSION']
gem 'hydra-collections', github: 'projecthydra/hydra-collections'

if ENV['RAILS_VERSION'].nil? || ENV['RAILS_VERSION'] =~ /^4.2/
gem 'responders', "~> 2.0"
gem 'sass-rails', ">= 5.0"
Expand Down
19 changes: 0 additions & 19 deletions app/controllers/concerns/curation_concerns/catalog_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ module CurationConcerns::CatalogController
helper CurationConcerns::CatalogHelper
# These before_filters apply the hydra access controls
before_action :enforce_show_permissions, only: :show
# This applies appropriate access controls to all solr queries
Hydra::SearchBuilder.default_processor_chain += [:add_access_controls_to_solr_params, :filter_models]
end

module ClassMethods
Expand All @@ -33,21 +31,4 @@ def search_config
{ 'qf' => %w(title_tesim name_tesim), 'qt' => 'search', 'rows' => 10 }
end
end

protected

# Overriding Blacklight so that the search results can be displayed in a way compatible with
# tokenInput javascript library. This is used for suggesting "Related Works" to attach.
def render_search_results_as_json
{ 'docs' => @response['response']['docs'].map { |solr_doc| serialize_work_from_solr(solr_doc) } }
end

def serialize_work_from_solr(solr_doc)
title = solr_doc['title_tesim'].first
title << " (#{solr_doc['human_readable_type_tesim'].first})" if solr_doc['human_readable_type_tesim'].present?
{
pid: solr_doc['id'],
title: title
}
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ module CollectionsControllerBehavior

included do
before_action :filter_docs_with_read_access!, except: :show
self.search_params_logic += [:add_access_controls_to_solr_params, :add_advanced_parse_q_to_solr]
layout 'curation_concerns/1_column'
skip_load_and_authorize_resource only: :show
end
Expand Down Expand Up @@ -51,19 +50,34 @@ def filter_docs_with_read_access!

def presenter
@presenter ||= begin
_, document_list = search_results(params, self.class.search_params_logic + [:find_one])
curation_concern = document_list.first
# Query Solr for the collection.
# run the solr query to find the collection members
query = collection_search_builder.query
response = repository.search(query)
curation_concern = response.documents.first
# _, document_list = search_results(params, self.class.search_params_logic + [:find_one])
# curation_concern = document_list.first
raise CanCan::AccessDenied unless curation_concern
presenter_class.new(curation_concern, current_ability)
end
end

def collection_search_builder
collection_search_builder_class.new(self).with(params).tap do |builder|
builder.current_ability = current_ability
end
end

def presenter_class
CurationConcerns::CollectionPresenter
end

def collection_search_builder_class
CurationConcerns::WorkSearchBuilder
end

def collection_member_search_builder_class
CurationConcerns::SearchBuilder
CurationConcerns::CollectionMemberSearchBuilder
end

def collection_params
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def after_update_response
def after_destroy_response(title)
flash[:notice] = "Deleted #{title}"
respond_to do |wants|
wants.html { redirect_to main_app.catalog_index_path }
wants.html { redirect_to main_app.search_catalog_path }
wants.json { render_json_response(response_type: :deleted, message: "Deleted #{curation_concern.id}") }
end
end
Expand All @@ -164,10 +164,14 @@ def additional_response_formats(_)
# nop
end

def search_builder_class
CurationConcerns::WorkSearchBuilder
end

private

def curation_concern_from_search_results
_, document_list = search_results(params, CatalogController.search_params_logic + [:find_one])
_, document_list = search_results(params)
raise CanCan::AccessDenied.new(nil, :show) if document_list.empty?
document_list.first
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
module CurationConcerns
module FileSetsControllerBehavior
extend ActiveSupport::Concern
include Blacklight::Base
include Blacklight::AccessControls::Catalog

included do
include CurationConcerns::ThemedLayoutController
with_themed_layout '1_column'
load_and_authorize_resource class: ::FileSet, except: :show
helper_method :curation_concern
include CurationConcerns::ParentContainer
include Blacklight::Base
include Blacklight::AccessControls::Catalog
copy_blacklight_config_from(::CatalogController)
end

Expand Down Expand Up @@ -134,13 +134,17 @@ def update_metadata

def presenter
@presenter ||= begin
_, document_list = search_results(params, [:add_access_controls_to_solr_params, :find_one, :only_file_sets])
_, document_list = search_results(params)
curation_concern = document_list.first
raise CanCan::AccessDenied unless curation_concern
show_presenter.new(curation_concern, current_ability)
end
end

def search_builder_class
CurationConcerns::FileSetSearchBuilder
end

def initialize_edit_form
@groups = current_user.groups
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def download
end

def show
_, document_list = search_results({ id: single_use_link.itemId }, [:find_one])
_, document_list = search_results(id: single_use_link.itemId)
curation_concern = document_list.first

# Authorize using SingleUseLinksViewerController::Ability
Expand All @@ -41,6 +41,10 @@ def show

protected

def search_builder_class
CurationConcerns::SingleUseLinkSearchBuilder
end

def content_options
super.tap do |options|
options[:disposition] = 'attachment' if action_name == 'download'
Expand Down
4 changes: 2 additions & 2 deletions app/helpers/curation_concerns/search_paths_helper.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
module CurationConcerns::SearchPathsHelper
def search_path_for_my_works(opts = {})
params_for_my_works = { 'f[generic_type_sim][]' => 'Work', works: 'mine' }
main_app.catalog_index_path(params_for_my_works.merge(opts))
main_app.search_catalog_path(params_for_my_works.merge(opts))
end

def search_path_for_my_collections(opts = {})
params_for_my_collections = { 'f[generic_type_sim][]' => 'Collection', works: 'mine' }
main_app.catalog_index_path(params_for_my_collections.merge(opts))
main_app.search_catalog_path(params_for_my_collections.merge(opts))
end
end
2 changes: 1 addition & 1 deletion app/renderers/curation_concerns/attribute_renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def li_value(value)
end

def search_path(value)
Rails.application.routes.url_helpers.catalog_index_path(
Rails.application.routes.url_helpers.search_catalog_path(
search_field: search_field, q: ERB::Util.h(value))
end

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module CurationConcerns
class CollectionMemberSearchBuilder < Hydra::Collections::MemberSearchBuilder
include CurationConcerns::FilterByType
end
end
10 changes: 10 additions & 0 deletions app/search_builders/curation_concerns/file_set_search_builder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module CurationConcerns
class FileSetSearchBuilder < Hydra::SearchBuilder
include CurationConcerns::SingleResult
self.default_processor_chain += [:only_file_sets]

def only_file_sets(solr_parameters)
solr_parameters[:fq] << ActiveFedora::SolrQueryBuilder.construct_query_for_rel(has_model: ::FileSet.to_class_uri)
end
end
end
29 changes: 29 additions & 0 deletions app/search_builders/curation_concerns/filter_by_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module CurationConcerns
module FilterByType
extend ActiveSupport::Concern

included do
self.default_processor_chain += [:filter_models]
end

# Add queries that excludes everything except for works and collections
def filter_models(solr_parameters)
solr_parameters[:fq] ||= []
solr_parameters[:fq] << '(' + (work_clauses + collection_clauses).join(' OR ') + ')'
end

private

def work_clauses
return [] if blacklight_params.key?(:f) && Array(blacklight_params[:f][:generic_type_sim]).include?('Collection')
CurationConcerns.config.registered_curation_concern_types.map(&:constantize).map do |klass|
ActiveFedora::SolrQueryBuilder.construct_query_for_rel(has_model: klass.to_class_uri)
end
end

def collection_clauses
return [] if blacklight_params.key?(:f) && Array(blacklight_params[:f][:generic_type_sim]).include?('Work')
[ActiveFedora::SolrQueryBuilder.construct_query_for_rel(has_model: ::Collection.to_class_uri)]
end
end
end
3 changes: 0 additions & 3 deletions app/search_builders/curation_concerns/lease_search_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ module CurationConcerns
# Finds objects under lease
class LeaseSearchBuilder < Blacklight::SearchBuilder
self.default_processor_chain = [:with_pagination, :with_sorting, :only_active_leases]
def initialize(scope)
super(true, scope)
end

# TODO: add more complex pagination
def with_pagination(solr_params)
Expand Down
28 changes: 1 addition & 27 deletions app/search_builders/curation_concerns/search_builder.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
class CurationConcerns::SearchBuilder < Hydra::SearchBuilder
include BlacklightAdvancedSearch::AdvancedSearchBuilder
include Hydra::Collections::SearchBehaviors

def only_file_sets(solr_parameters)
solr_parameters[:fq] << ActiveFedora::SolrQueryBuilder.construct_query_for_rel(has_model: FileSet.to_class_uri)
end

def find_one(solr_parameters)
solr_parameters[:fq] << "_query_:\"{!raw f=id}#{blacklight_params.fetch(:id)}\""
end
include CurationConcerns::FilterByType

# Override Hydra::AccessControlsEnforcement (or Hydra::PolicyAwareAccessControlsEnforcement)
# Allows admin users to see everything (don't apply any gated_discovery_filters for those users)
Expand All @@ -22,22 +14,4 @@ def discovery_permissions
return ['edit'] if blacklight_params[:works] == 'mine'
super
end

# This is included as part of blacklight search solr params logic
def filter_models(solr_parameters)
solr_parameters[:fq] ||= []
solr_parameters[:fq] << '(' + (work_clauses + collection_clauses).join(' OR ') + ')'
end

def work_clauses
return [] if blacklight_params.key?(:f) && Array(blacklight_params[:f][:generic_type_sim]).include?('Collection')
CurationConcerns.config.registered_curation_concern_types.map(&:constantize).map do |klass|
ActiveFedora::SolrQueryBuilder.construct_query_for_rel(has_model: klass.to_class_uri)
end
end

def collection_clauses
return [] if blacklight_params.key?(:f) && Array(blacklight_params[:f][:generic_type_sim]).include?('Work')
[ActiveFedora::SolrQueryBuilder.construct_query_for_rel(has_model: ::Collection.to_class_uri)]
end
end
13 changes: 13 additions & 0 deletions app/search_builders/curation_concerns/single_result.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module CurationConcerns
module SingleResult
extend ActiveSupport::Concern

included do
self.default_processor_chain += [:find_one]
end

def find_one(solr_parameters)
solr_parameters[:fq] << "_query_:\"{!raw f=id}#{blacklight_params.fetch(:id)}\""
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module CurationConcerns
class SingleUseLinkSearchBuilder < CurationConcerns::SearchBuilder
include CurationConcerns::SingleResult
self.default_processor_chain = [:find_one]
end
end
5 changes: 5 additions & 0 deletions app/search_builders/curation_concerns/work_search_builder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module CurationConcerns
class WorkSearchBuilder < CurationConcerns::SearchBuilder
include CurationConcerns::SingleResult
end
end
9 changes: 9 additions & 0 deletions app/views/catalog/index.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Overriding Blacklight so that the search results can be displayed in a way compatible with
# tokenInput javascript library. This is used for suggesting "Related Works" to attach.

json.docs @presenter.documents do |solr_document|
title = solr_document['title_tesim'].first
title << " (#{solr_document['human_readable_type_tesim'].first})" if solr_document['human_readable_type_tesim'].present?
json.pid solr_document['id']
json.title title
end
2 changes: 1 addition & 1 deletion app/views/shared/_site_search.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<%= form_tag main_app.catalog_index_path, method: :get, class: "search-form" do %>
<%= form_tag main_app.search_catalog_path, method: :get, class: "search-form" do %>
<fieldset>
<legend class="accessible-hidden">Search <%= t('curation_concerns.product_name') %></legend>
<%= label_tag :catalog_search, t('curation_concerns.search.form.q.label'), class: "accessible-hidden" %>
Expand Down
3 changes: 2 additions & 1 deletion curation_concerns.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ Gem::Specification.new do |spec|
spec.require_paths = ["lib"]

spec.add_dependency 'hydra-head', '~> 9.6'
spec.add_dependency 'blacklight', '~> 6.0'
spec.add_dependency "breadcrumbs_on_rails", "~> 2.3"
spec.add_dependency "jquery-ui-rails"
spec.add_dependency "simple_form", '~> 3.1'
spec.add_dependency 'curation_concerns-models', version
spec.add_dependency 'hydra-editor', '~> 1.1'
spec.add_dependency 'blacklight_advanced_search', ['>= 5.1.4', '< 6.0']
spec.add_dependency 'blacklight_advanced_search', '~> 6.0'
spec.add_dependency 'rails_autolink'

spec.add_development_dependency "devise", "~> 3.0"
Expand Down
1 change: 1 addition & 0 deletions spec/controllers/catalog_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@

context 'when json is requested for autosuggest of related works' do
let!(:work) { FactoryGirl.create(:generic_work, user: user, title: ["All my #{srand}"]) }
render_views
it 'returns json' do
xhr :get, :index, format: :json, q: work.title
json = JSON.parse(response.body)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@

it 'deletes the work' do
delete :destroy, id: work_to_be_deleted
expect(response).to redirect_to main_app.catalog_index_path
expect(response).to redirect_to main_app.search_catalog_path
expect(GenericWork).not_to exist(work_to_be_deleted.id)
end

Expand Down
2 changes: 1 addition & 1 deletion spec/features/catalog_search_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
let!(:coll) { create(:collection, :public, title: 'My Collection') }

scenario 'Browse the catalog using filter tabs' do
visit catalog_index_path
visit search_catalog_path

# Filter on Works
within '#type-tabs' do
Expand Down
2 changes: 1 addition & 1 deletion spec/features/collection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def create_collection(title, description)
@collection.apply_depositor_metadata(user_key)
@collection.save
sign_in user
visit main_app.catalog_index_path('f[generic_type_sim][]' => 'Collection', works: 'mine')
visit main_app.search_catalog_path('f[generic_type_sim][]' => 'Collection', works: 'mine')
end

it 'deletes a collection' do
Expand Down
Loading

0 comments on commit 4544469

Please sign in to comment.