Skip to content
This repository has been archived by the owner on Jan 5, 2021. It is now read-only.

Commit

Permalink
Delete file sets using the management search
Browse files Browse the repository at this point in the history
Enables search and retrieval of file sets via the management search
interface so the user may select one or more file sets to delete. File
sets could come from any work.
  • Loading branch information
awead committed Jan 10, 2019
1 parent 10397ee commit 41bf4a0
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 56 deletions.
33 changes: 26 additions & 7 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,20 +1,40 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2018-10-15 21:54:28 -0400 using RuboCop version 0.52.1.
# on 2019-01-09 10:52:14 -0500 using RuboCop version 0.52.1.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 5
# Offense count: 1
# Cop supports --auto-correct.
Layout/EmptyLines:
Exclude:
- 'spec/valkyrie/change_set_persister_spec.rb'

# Offense count: 1
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle.
# SupportedStyles: empty_lines, no_empty_lines
Layout/EmptyLinesAroundBlockBody:
Exclude:
- 'spec/valkyrie/change_set_persister_spec.rb'

# Offense count: 4
# Configuration parameters: CountComments, ExcludedMethods.
Metrics/BlockLength:
Max: 36
Max: 35

# Offense count: 1
# Configuration parameters: CountComments.
Metrics/ClassLength:
Max: 102

# Offense count: 42
# Offense count: 50
RSpec/ExpectActual:
Exclude:
- 'spec/routing/**/*'
- 'spec/cho/agent/routing_spec.rb'
- 'spec/cho/batch/delete_routing_spec.rb'
- 'spec/cho/batch/select_routing_spec.rb'
- 'spec/cho/collection/archival_collections/archival_collections_routing_spec.rb'
Expand All @@ -23,7 +43,6 @@ RSpec/ExpectActual:
- 'spec/cho/data_dictionary/routing_spec.rb'
- 'spec/cho/work/import/routing_spec.rb'
- 'spec/cho/work/submissions/routing_spec.rb'
- 'spec/cho/agent/routing_spec.rb'

# Offense count: 3
RSpec/LetSetup:
Expand All @@ -36,16 +55,16 @@ RSpec/LetSetup:
RSpec/MessageSpies:
EnforcedStyle: receive

# Offense count: 20
# Offense count: 22
# Configuration parameters: Max.
RSpec/NestedGroups:
Exclude:
- 'spec/cho/agent/controller_spec.rb'
- 'spec/cho/data_dictionary/field_change_set_spec.rb'
- 'spec/cho/data_dictionary/fields_controller_spec.rb'
- 'spec/cho/schema/metadata_field_change_set_spec.rb'
- 'spec/cho/shared/item_factory_spec.rb'
- 'spec/cho/work/submissions/change_set_spec.rb'
- 'spec/cho/agent/controller_spec.rb'

# Offense count: 1
# Cop supports --auto-correct.
Expand Down
54 changes: 54 additions & 0 deletions app/blacklight/catalog_search_behavior.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# frozen_string_literal: true

module CatalogSearchBehavior
extend ActiveSupport::Concern

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

# show both works that match the query and works that contain files that match the query
# copied from ScholarSphere from an updated Hyrax
# version: https://github.com/psu-stewardship/scholarsphere/blob/develop/app/search_builders/search_builder.rb#L12
def show_works_or_works_that_contain_files(solr_parameters)
return if blacklight_params[:q].blank?

solr_parameters[:user_query] = blacklight_params[:q]
solr_parameters[:q] = new_query
solr_parameters[:defType] = 'lucene'
end

private

# the {!lucene} gives us the OR syntax
def new_query
"{!lucene}#{internal_query(query_all_query_fields)} "\
"#{internal_query(search_metadata_from_file_sets)} "\
"#{internal_query(search_extracted_text_from_files)}"
end

# @note The _query_ allows for another parser, i.e. dismax.
def internal_query(query_value)
"_query_:\"#{query_value}\""
end

# @note The {!dismax} causes the query to go against the query fields. This is functionally equivalent
# to q=*:user_query where user_query is the query string submitted by the user, and the fields being
# searched are specified in the qf parameter.
def query_all_query_fields
'{!dismax v=$user_query}'
end

# @note Extends the search to include the fields from file sets attached to work.
def search_metadata_from_file_sets
"{!join from=join_id_ssi to=file_set_ids_ssim}#{query_all_query_fields}"
end

# @note Extends to search to include text content extracted from files in the work. This is functionally
# equivalent to:
# q={!join from=join_id_ssi to=file_set_ids_ssim}all_text_timv:user_query
# where user_query is the query string submitted by the user.
def search_extracted_text_from_files
'{!join from=join_id_ssi to=file_set_ids_ssim}{!dismax qf=all_text_timv v=$user_query}'
end
end
47 changes: 2 additions & 45 deletions app/blacklight/search_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,57 +2,14 @@

class SearchBuilder < Blacklight::SearchBuilder
include Blacklight::Solr::SearchBuilderBehavior
self.default_processor_chain += [:show_works_or_works_that_contain_files, :show_only_works_and_collections]
include CatalogSearchBehavior

# show both works that match the query and works that contain files that match the query
# copied from ScholarSphere from an updated Hyrax
# version: https://github.com/psu-stewardship/scholarsphere/blob/develop/app/search_builders/search_builder.rb#L12
def show_works_or_works_that_contain_files(solr_parameters)
return if blacklight_params[:q].blank?

solr_parameters[:user_query] = blacklight_params[:q]
solr_parameters[:q] = new_query
solr_parameters[:defType] = 'lucene'
end
self.default_processor_chain += [:show_only_works_and_collections]

# Do not include files and file sets in the search results
def show_only_works_and_collections(solr_parameters)
solr_parameters[:fq] ||= []
solr_parameters[:fq] << 'internal_resource_ssim:("Collection::Archival" OR "Collection::Library"' \
'OR "Collection::Curated" OR "Work::Submission")'
end

private

# the {!lucene} gives us the OR syntax
def new_query
"{!lucene}#{internal_query(query_all_query_fields)} "\
"#{internal_query(search_metadata_from_file_sets)} "\
"#{internal_query(search_extracted_text_from_files)}"
end

# @note The _query_ allows for another parser, i.e. dismax.
def internal_query(query_value)
"_query_:\"#{query_value}\""
end

# @note The {!dismax} causes the query to go against the query fields. This is functionally equivalent
# to q=*:user_query where user_query is the query string submitted by the user, and the fields being
# searched are specified in the qf parameter.
def query_all_query_fields
'{!dismax v=$user_query}'
end

# @note Extends the search to include the fields from file sets attached to work.
def search_metadata_from_file_sets
"{!join from=join_id_ssi to=file_set_ids_ssim}#{query_all_query_fields}"
end

# @note Extends to search to include text content extracted from files in the work. This is functionally
# equivalent to:
# q={!join from=join_id_ssi to=file_set_ids_ssim}all_text_timv:user_query
# where user_query is the query string submitted by the user.
def search_extracted_text_from_files
'{!join from=join_id_ssi to=file_set_ids_ssim}{!dismax qf=all_text_timv v=$user_query}'
end
end
17 changes: 17 additions & 0 deletions app/cho/batch/search_builder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

module Batch
class SearchBuilder < Blacklight::SearchBuilder
include Blacklight::Solr::SearchBuilderBehavior
include CatalogSearchBehavior

self.default_processor_chain += [:show_everything]

# Do not include files and file sets in the search results
def show_everything(solr_parameters)
solr_parameters[:fq] ||= []
solr_parameters[:fq] << 'internal_resource_ssim:("Collection::Archival" OR "Collection::Library"' \
'OR "Collection::Curated" OR "Work::Submission" OR "Work::FileSet")'
end
end
end
4 changes: 4 additions & 0 deletions app/cho/batch/select_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,9 @@ class SelectController < ApplicationController
def self._prefixes
['application', 'batch', 'catalog']
end

configure_blacklight do |config|
config.search_builder_class = Batch::SearchBuilder
end
end
end
4 changes: 4 additions & 0 deletions app/valkyrie/change_set_persister.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ def delete_children(change_set:)
persister.delete(resource: member)
end
end

if change_set.resource.try(:files)
change_set.resource.files.each { |file| delete_file(resource: file) }
end
end

def delete_file_set(resource:)
Expand Down
13 changes: 9 additions & 4 deletions spec/cho/batch/select_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,22 @@
it_behaves_like 'a search form', '/select'

context 'when deleting items' do
before { create(:work, title: 'Resource to delete') }
let!(:work) { create(:work, :with_file, title: 'Resource to delete') }

it 'selects items from the repository and removes them' do
visit(root_path)
click_link('Select Resources')
fill_in('q', with: 'Resource to delete')
fill_in('q', with: '')
click_button('Search')
find("input[type='checkbox']").click
within('.select-resources') do
expect(page).to have_link('Sample Archival Collection')
expect(page).to have_link('hello_world.txt')
expect(page).to have_link('Resource to delete')
end
find("input[id='delete_ids_#{work.id}']").click
click_button('Delete Selected Resources')
expect(page).to have_content('The following resources will be deleted')
expect(page).to have_selector('h2', text: 'Resource to delete (0 items)')
expect(page).to have_selector('h2', text: 'Resource to delete (3 items)')
click_button('Continue')
expect(page).to have_content('You have successfully deleted the following items: Resource to delete ')
end
Expand Down
16 changes: 16 additions & 0 deletions spec/valkyrie/change_set_persister_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,22 @@
expect(metadata_adapter.index_adapter.query_service.find_all.count).to eq(0)
end
end

context 'with a file set containing files' do
let!(:change_set) { Work::FileSetChangeSet.new(create(:file_set, :with_member_file)) }

it 'deletes the file set and its files' do
expect(Work::FileSet.all.count).to eq(1)
expect(Work::File.all.count).to eq(1)
expect(metadata_adapter.index_adapter.query_service.find_all.count).to eq(2)
change_set_persister.delete(change_set: change_set)
expect(Work::FileSet.all.count).to eq(0)
expect(Work::File.all.count).to eq(0)
expect(metadata_adapter.index_adapter.query_service.find_all.count).to eq(0)
end


end
end

describe '#update_or_create' do
Expand Down

0 comments on commit 41bf4a0

Please sign in to comment.