-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add edge case handling to file rename feature
Why are these changes being introduced: * Creating duplicate filenames or renaming the wrong file should be detected and prevented Relevant ticket(s): * https://mitlibraries.atlassian.net/browse/ETD-643 How does this address that need: * Adds checks to prevent creating a duplicate filename when renaming a thesis attachment * Ensures the attachment being renamed is correctly associated with the thesis in the URL path or form submission or prevents renaming Document any side effects to this change: * report related test numbers changed when number of theses changed * some rubocop autoformatting in thesis_controller_test
- Loading branch information
Showing
7 changed files
with
205 additions
and
24 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,74 @@ | ||
class FileController < ApplicationController | ||
before_action :require_user | ||
before_action :authenticate_user! | ||
before_action :load_thesis_and_attachment | ||
load_and_authorize_resource | ||
protect_from_forgery with: :exception | ||
|
||
def rename_form | ||
@thesis = Thesis.find(params[:thesis_id]) | ||
@attachment = ActiveStorage::Attachment.find(params[:attachment_id]) | ||
return if appropriate_attachment_for_thesis? | ||
|
||
error_nonmatching_thesis_message | ||
redirect_to thesis_process_path(@thesis) | ||
end | ||
|
||
def rename | ||
thesis = Thesis.find(params[:thesis_id]) | ||
attachment = ActiveStorage::Attachment.find(params[:attachment_id]) | ||
prerename_validations | ||
rename_attachment unless flash.key?(:error) | ||
|
||
redirect_to thesis_process_path(@thesis) | ||
end | ||
|
||
private | ||
|
||
def prerename_validations | ||
error_nonmatching_thesis_message unless appropriate_attachment_for_thesis? | ||
error_duplicate_name_message if new_duplicate? | ||
end | ||
|
||
def new_duplicate? | ||
return false if @thesis.files.count == 1 | ||
return true if attachment_names_except_current.include?(params[:attachment][:filename]) | ||
|
||
false | ||
end | ||
|
||
def attachment_names_except_current | ||
@thesis.files.map { |x| x.filename if x != @attachment }.compact | ||
end | ||
|
||
attachment.blob.filename = params[:attachment][:filename] | ||
def rename_attachment | ||
@attachment.blob.filename = params[:attachment][:filename] | ||
|
||
if attachment.blob.save | ||
flash[:success] = "#{thesis.title} file #{attachment.filename} been updated." | ||
if @attachment.blob.save | ||
flash[:success] = success_message | ||
else | ||
flash[:error] = "#{thesis.title} file was unable to be updated" | ||
flash[:error] = error_rename_failed_message | ||
end | ||
end | ||
|
||
def appropriate_attachment_for_thesis? | ||
@thesis.files.include?(@attachment) | ||
end | ||
|
||
def load_thesis_and_attachment | ||
@thesis = Thesis.find(params[:thesis_id]) | ||
@attachment = ActiveStorage::Attachment.find(params[:attachment_id]) | ||
end | ||
|
||
def error_duplicate_name_message | ||
flash[:error] = 'The new name you chose is the same as an existing name of a file attached to this thesis.' | ||
end | ||
|
||
def success_message | ||
"#{@thesis.title} file #{@attachment.filename} been updated." | ||
end | ||
|
||
def error_rename_failed_message | ||
"#{@thesis.title} file was unable to be updated" | ||
end | ||
|
||
redirect_to thesis_process_path(thesis) | ||
def error_nonmatching_thesis_message | ||
flash[:error] = 'The file to be renamed was not associated with the thesis being edited.' | ||
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
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