-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
🎁 IiifPrint Enhancements to clean up split works as PDF fileset is deleted #288
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
cff0d69
add parent_model and child_model to iiif_print_pending_relationships
ShanaLMoore 1c9daac
:gift: pass parent and child model to PendingRelationship initialization
ShanaLMoore eed6795
:gift: add file_id to pending_relationships table
ShanaLMoore a41f52a
Merge branch 'main'
904477b
Require file_id
458053c
Add PendingRelationships attributes to spec
890c76b
Add split_from_pdf_id term
be326d5
Remove PendingRelationships entries
6c7f1ee
Add split_from_pdf_id term to solr document
3ded9fb
Shorten class to appease Rubocop
a686bc5
Create service to destroy child works of PDFs
a23582d
Appease rubocop
ddbeab5
Delete spawned child works as a work is deleted
1b85c3f
Add spec for destroy_pdf_child_works_service
65df06b
Rubocop fixes
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
app/actors/iiif_print/actors/cleanup_file_sets_actor_decorator.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# frozen_string_literal: true | ||
|
||
# override Hyrax to remove splitting upon work delete | ||
module IiifPrint | ||
module Actors | ||
# Responsible for removing FileSets related to the given curation concern. | ||
module CleanupFileSetsActorDecorator | ||
# @param [Hyrax::Actors::Environment] env | ||
# @return [Boolean] true if destroy was successful | ||
def destroy(env) | ||
file_sets = env.curation_concern.file_sets | ||
file_sets.each do |file_set| | ||
# we destroy the children before the file_set, because we need the parent relationship | ||
IiifPrint::SplitPdfs::DestroyPdfChildWorksService.conditionally_destroy_spawned_children_of( | ||
file_set: file_set, | ||
work: env.curation_concern | ||
) | ||
end | ||
# and now back to your regularly scheduled programming | ||
super | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
db/migrate/20231110163052_add_model_details_to_iiif_print_pending_relationships.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class AddModelDetailsToIiifPrintPendingRelationships < ActiveRecord::Migration[5.2] | ||
def change | ||
add_column :iiif_print_pending_relationships, :parent_model, :string | ||
add_column :iiif_print_pending_relationships, :child_model, :string | ||
add_column :iiif_print_pending_relationships, :file_id, :string | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
lib/iiif_print/split_pdfs/destroy_pdf_child_works_service.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# frozen_string_literal: true | ||
|
||
module IiifPrint | ||
module SplitPdfs | ||
## Encapsulates logic for cleanup when the PDF is destroyed after pdf splitting into child works | ||
class DestroyPdfChildWorksService | ||
## @api public | ||
# @param file_set [FileSet] What is the containing file set for the provided file. | ||
# @param work [Hydra::PCDM::Work] Parent of the fileset being deleted | ||
def self.conditionally_destroy_spawned_children_of(file_set:, work:) | ||
child_model = work.try(:iiif_print_config)&.pdf_split_child_model | ||
return unless child_model | ||
return unless file_set.class.pdf_mime_types.include?(file_set.mime_type) | ||
|
||
IiifPrint::PendingRelationship.where(parent_id: work.id, file_id: file_set.id).each(&:destroy) | ||
destroy_spawned_children(model: child_model, file_set: file_set, work: work) | ||
end | ||
|
||
private_class_method def self.destroy_spawned_children(model:, file_set:, work:) | ||
# look first for children by the file set id they were split from | ||
children = model.where(split_from_pdf_id: file_set.id) | ||
if children.blank? | ||
# find works where file name and work `to_param` are both in the title | ||
children = model.where(title: file_set.label).where(title: work.to_param) | ||
end | ||
return if children.blank? | ||
children.each do |rcd| | ||
rcd.destroy(eradicate: true) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
spec/iiif_print/split_pdfs/destroy_pdf_child_works_service_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
RSpec.describe IiifPrint::SplitPdfs::DestroyPdfChildWorksService do | ||
let(:subject) { described_class.conditionally_destroy_spawned_children_of(file_set: fileset, work: work) } | ||
|
||
let(:work) { WorkWithIiifPrintConfig.new(title: ['required title'], id: '123') } | ||
let(:fileset) { FileSet.new.tap { |fs| fs.save!(validate: false) } } | ||
let(:child_work) { WorkWithIiifPrintConfig.new(title: ["Child of #{work.id} file.pdf page 01"], id: '456', is_child: true) } | ||
let(:pending_rel1) do | ||
IiifPrint::PendingRelationship.new( | ||
parent_id: work.id, | ||
child_title: "Child of #{work.id} file.pdf page 01", | ||
child_order: "Child of #{work.id} file.pdf page 01", | ||
parent_model: WorkWithIiifPrintConfig, | ||
child_model: WorkWithIiifPrintConfig, | ||
file_id: fileset.id | ||
) | ||
end | ||
let(:pending_rel2) do | ||
IiifPrint::PendingRelationship.new( | ||
parent_id: work.id, | ||
child_title: "Child of #{work.id} another.pdf page 01", | ||
child_order: "Child of #{work.id} another.pdf page 01", | ||
parent_model: WorkWithIiifPrintConfig, | ||
child_model: WorkWithIiifPrintConfig, | ||
file_id: 'another' | ||
) | ||
end | ||
# let(:uploaded_pdf_file) { create(:uploaded_pdf_file) } | ||
# let(:uploaded_file_ids) { [uploaded_pdf_file.id] } | ||
|
||
before do | ||
allow(fileset).to receive(:parent).and_return(work) | ||
allow(fileset).to receive(:label).and_return('file.pdf') | ||
allow(fileset).to receive(:mime_type).and_return('application/pdf') | ||
end | ||
|
||
describe 'class' do | ||
subject { described_class } | ||
|
||
it { is_expected.to respond_to(:conditionally_destroy_spawned_children_of) } | ||
it { is_expected.not_to respond_to(:destroy_spawned_children) } | ||
end | ||
|
||
describe '#conditionally_destroy_spawned_children_of' do | ||
context 'with child works by fileset id' do | ||
before do | ||
allow(WorkWithIiifPrintConfig).to receive(:where).with(split_from_pdf_id: fileset.id).and_return([child_work]) | ||
end | ||
|
||
it 'destroys the child works' do | ||
expect(child_work).to receive(:destroy) | ||
subject | ||
end | ||
end | ||
|
||
context 'with child works by title' do | ||
before do | ||
allow(WorkWithIiifPrintConfig).to receive(:where).with(split_from_pdf_id: fileset.id).and_return([]) | ||
allow(WorkWithIiifPrintConfig).to receive(:where).and_return([child_work]) | ||
end | ||
|
||
it 'destroys the child works' do | ||
expect(child_work).to receive(:destroy) | ||
subject | ||
end | ||
end | ||
|
||
context 'when fileset is not a PDF mimetype' do | ||
before do | ||
allow(fileset).to receive(:mime_type).and_return('not_pdf') | ||
end | ||
|
||
it 'returns with no changes' do | ||
expect(IiifPrint::PendingRelationship).not_to receive(:where) | ||
end | ||
end | ||
|
||
context 'when IiifPrint::PendingRelationship records exist' do | ||
before do | ||
pending_rel1.save | ||
pending_rel2.save | ||
end | ||
|
||
it 'deletes only records associated with the specific fileset PDF file' do | ||
expect { subject }.to change(IiifPrint::PendingRelationship, :count).by(-1) | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe consider renaming this file since it's doing more than setting the child flag now. I suppose that would be a breaking change so we could also cut a new release?
Now that I say that, maybe that can be a TODO for later given the urgency 😅
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lol yeah I considered that but since it's included in EVERY repo that uses IiifPrint, it's def a breaking change which is why I left it. Plus it's still a type of child flag if you stretch hard enough. ;)