Skip to content

Commit

Permalink
First pass at logging file changes
Browse files Browse the repository at this point in the history
  • Loading branch information
hectorcorrea committed Oct 21, 2022
1 parent 3f6e66c commit 144d9f4
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 41 deletions.
6 changes: 3 additions & 3 deletions app/controllers/works_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -287,14 +287,14 @@ def wizard_mode?

def update_work
@wizard_mode = wizard_mode?

upload_service = WorkUploadsEditService.new(@work, current_user)
if @work.approved?
upload_keys = work_params[:deleted_uploads] || []
deleted_uploads = WorkUploadsEditService.find_post_curation_uploads(work: @work, upload_keys: upload_keys)
deleted_uploads = upload_service.find_post_curation_uploads(upload_keys: upload_keys)

return head(:forbidden) unless deleted_uploads.empty?
else
@work = WorkUploadsEditService.update_precurated_file_list(@work, work_params)
@work = upload_service.update_precurated_file_list(work_params)
end

process_updates
Expand Down
5 changes: 5 additions & 0 deletions app/models/work.rb
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,11 @@ def log_changes(resource_compare, current_user)
WorkActivity.add_system_activity(id, resource_compare.differences.to_json, current_user.id, activity_type: "CHANGES")
end

def log_file_changes(changes, current_user)
return if changes.count == 0
WorkActivity.add_system_activity(id, changes.to_json, current_user.id, activity_type: "FILE-CHANGES")
end

def activities
WorkActivity.where(work_id: id).sort_by(&:updated_at).reverse
end
Expand Down
13 changes: 12 additions & 1 deletion app/models/work_activity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ def created_by_user
def message_html
if activity_type == "CHANGES"
changes_html
elsif activity_type == "FILE-CHANGES"
file_changes_html
else
comment_html
end
Expand All @@ -62,7 +64,16 @@ def comment_html
Kramdown::Document.new(text).to_html
end

# Returns the message formatted to display changes that were logged as an activity
# Returns the message formatted to display _file_ changes that were logged as an activity
def file_changes_html
changes = JSON.parse(message)
changes_html = changes.map do |change|
"<i>#{change['action']}</i> <span>#{change['filename']}<span><br/>"
end
"<p><b>Files</b>: #{changes_html.join}</p>"
end

# Returns the message formatted to display _metadata_ changes that were logged as an activity
def changes_html
text = ""
changes = JSON.parse(message)
Expand Down
100 changes: 63 additions & 37 deletions app/services/work_uploads_edit_service.rb
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

0 comments on commit 144d9f4

Please sign in to comment.