-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3f6e66c
commit 144d9f4
Showing
4 changed files
with
83 additions
and
41 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,76 @@ | ||
# frozen_string_literal: true | ||
class WorkUploadsEditService | ||
class << self | ||
def update_precurated_file_list(work, work_params) | ||
if work_params.key?(:deleted_uploads) || work_params.key?(:pre_curation_uploads) || work_params.key?(:replaced_uploads) | ||
if work_params.key?(:deleted_uploads) | ||
delete_pre_curation_uploads(work.pre_curation_uploads, work_params[:deleted_uploads]) | ||
elsif work_params.key?(:pre_curation_uploads) | ||
work.pre_curation_uploads.each(&:purge) | ||
work.reload # reload the work to pick up the changes in the attachments | ||
Array(work_params[:pre_curation_uploads]).each { |new_upload| work.pre_curation_uploads.attach(new_upload) } | ||
elsif work_params.key?(:replaced_uploads) | ||
replace_uploads(work, work_params[:replaced_uploads]) | ||
def initialize(work, current_user) | ||
@work = work | ||
@current_user = current_user | ||
@changes = [] | ||
end | ||
|
||
def work | ||
@work | ||
end | ||
|
||
def update_precurated_file_list(work_params) | ||
if work_params.key?(:deleted_uploads) || work_params.key?(:pre_curation_uploads) || work_params.key?(:replaced_uploads) | ||
if work_params.key?(:deleted_uploads) | ||
# delete the files indicated in the parameters | ||
delete_pre_curation_uploads(work_params[:deleted_uploads]) | ||
elsif work_params.key?(:pre_curation_uploads) | ||
# delete all existing uploads and then and then add the ones indicated in the parameters | ||
work.pre_curation_uploads.each do |existing_upload| | ||
track_change(:deleted, existing_upload.filename.to_s) | ||
existing_upload.purge | ||
end | ||
work.reload # reload the work to pick up the changes in the attachments | ||
|
||
else # no changes in the parameters, just return the original work | ||
work | ||
Array(work_params[:pre_curation_uploads]).each do |new_upload| | ||
track_change(:added, new_upload.original_filename) | ||
work.pre_curation_uploads.attach(new_upload) | ||
end | ||
elsif work_params.key?(:replaced_uploads) | ||
# replace the files indicated in the parameters | ||
replace_uploads(work_params[:replaced_uploads]) | ||
end | ||
|
||
work.log_file_changes(@changes, @current_user) | ||
work.reload # reload the work to pick up the changes in the attachments | ||
else # no changes in the parameters, just return the original work | ||
work | ||
end | ||
end | ||
|
||
def find_post_curation_uploads(work:, upload_keys: []) | ||
return [] unless work.approved? && !upload_keys.empty? | ||
def find_post_curation_uploads(upload_keys: []) | ||
return [] unless work.approved? && !upload_keys.empty? | ||
work.post_curation_uploads.select { |upload| upload_keys.include?(upload.key) } | ||
end | ||
|
||
work.post_curation_uploads.select { |upload| upload_keys.include?(upload.key) } | ||
private | ||
|
||
def replace_uploads(replaced_uploads_params) | ||
new_uploads = [] | ||
work.pre_curation_uploads.each_with_index do |existing, i| | ||
key = i.to_s | ||
next unless replaced_uploads_params.key?(key) | ||
new_uploads << replaced_uploads_params[key] | ||
track_change(:deleted, existing.filename.to_s) | ||
existing.purge | ||
end | ||
work.reload | ||
new_uploads.each do |new_upload| | ||
track_change(:added, new_upload.original_filename) | ||
work.pre_curation_uploads.attach(new_upload) | ||
end | ||
end | ||
|
||
private | ||
|
||
def replace_uploads(work, replaced_uploads_params) | ||
new_uploads = [] | ||
work.pre_curation_uploads.each_with_index do |existing, i| | ||
key = i.to_s | ||
next unless replaced_uploads_params.key?(key) | ||
new_uploads << replaced_uploads_params[key] | ||
existing.purge | ||
end | ||
work.reload | ||
new_uploads.each { |new_upload| work.pre_curation_uploads.attach(new_upload) } | ||
def delete_pre_curation_uploads(deleted_uploads_params) | ||
work.pre_curation_uploads.each do |existing| | ||
if deleted_uploads_params.key?(existing.key) && deleted_uploads_params[existing.key] == "1" | ||
track_change(:deleted, existing.filename.to_s) | ||
existing.purge | ||
end | ||
end | ||
end | ||
|
||
def delete_pre_curation_uploads(persisted_pre_curation_uploads, deleted_uploads_params) | ||
persisted_pre_curation_uploads.each do |existing| | ||
if deleted_uploads_params.key?(existing.key) && deleted_uploads_params[existing.key] == "1" | ||
existing.purge | ||
end | ||
end | ||
end | ||
end | ||
def track_change(action, filename) | ||
@changes << { action: action, filename: filename } | ||
end | ||
end |