Skip to content

Commit

Permalink
Added save_only parameter processing to Wizard Controller update meth…
Browse files Browse the repository at this point in the history
…ods (#1721)

* Added save_only parameter processing to update_wizard
save_only returns the user to the edit screen after saving instead of redirecting to the next step

* adding a save check for new submissions that keeps the user on the current page

* Updating test to pass valid paramaters

* Adding save only to readme_uploaded

* Updating to allow for save_only in review and file location select

* Letting rubocop fix some of the violations

* Allow file uploaded to stay on page on save

* added save only to validate
This completes the work, except for the fact that rubocop is not happy with the class now

* Moving decorator creation to application controller so wizard and work form can share

* Moving update process into compare service
fixes a rubocop violation

* fixing rubocop issue for file upload in the works wizard controller

* fixing  rubocop errors

* Fixing code to make tests pass

* add test for the WorkCompareServie

* Updatign for rubocop

---------

Co-authored-by: HyppoliteJ <[email protected]>
  • Loading branch information
carolyncole and JaymeeH authored Mar 29, 2024
1 parent 08381e8 commit 5ba0354
Show file tree
Hide file tree
Showing 6 changed files with 279 additions and 128 deletions.
5 changes: 5 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,9 @@ def update_params
resource: FormToResourceService.convert(params, @work)
}
end

def prepare_decorators_for_work_form(work)
@work_decorator = WorkDecorator.new(work, current_user)
@form_resource_decorator = FormResourceDecorator.new(work, current_user)
end
end
6 changes: 1 addition & 5 deletions app/controllers/works_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -369,11 +369,7 @@ def deleted_files_param
end

def process_updates
work_before = @work.dup
if @work.update(update_params)
work_compare = WorkCompareService.new(work_before, @work)
@work.log_changes(work_compare, current_user.id)

if WorkCompareService.update_work(work: @work, update_params:, current_user:)
redirect_to work_url(@work), notice: "Work was successfully updated."
else
# This is needed for rendering HTML views with validation errors
Expand Down
79 changes: 45 additions & 34 deletions app/controllers/works_wizard_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,12 @@ class WorksWizardController < ApplicationController
:readme_select, :readme_uploaded]

# get Renders the "step 0" information page before creating a new dataset
# GET /works/1/new_submission
# GET /works/new_submission
def new_submission
group = Group.find_by(code: params[:group_code]) || current_user.default_group
group_id = group.id
@work = Work.new(created_by_user_id: current_user.id, group_id:)
@work_decorator = WorkDecorator.new(@work, current_user)
@form_resource_decorator = FormResourceDecorator.new(@work, current_user)
prepare_decorators_for_work_form(@work)
end

# Creates the new dataset
Expand All @@ -36,18 +35,22 @@ def new_submission_save
@work = Work.new(created_by_user_id: current_user.id, group_id:)
@work.resource = FormToResourceService.convert(params, @work)
@work.draft!(current_user)
redirect_to edit_work_wizard_path(@work)
if params[:save_only] == "true"
prepare_decorators_for_work_form(@work)
render :new_submission
else
redirect_to edit_work_wizard_path(@work)
end
end

# GET /works/1/edit_wizard
# GET /works/1/edit-wizard
def edit_wizard
@work_decorator = WorkDecorator.new(@work, current_user)
@wizard_mode = true
if validate_modification_permissions(work: @work,
uneditable_message: "Can not edit work: #{@work.id} is not editable by #{current_user.uid}",
current_state_message: "Can not edit work: #{@work.id} is not editable in current state by #{current_user.uid}")

@form_resource_decorator = FormResourceDecorator.new(@work, current_user)
prepare_decorators_for_work_form(@work)
end
end

Expand All @@ -56,44 +59,39 @@ def update_wizard
if validate_modification_permissions(work: @work,
uneditable_message: "Can not update work: #{@work.id} is not editable by #{current_user.uid}",
current_state_message: "Can not update work: #{@work.id} is not editable in current state by #{current_user.uid}")
work_before = @work.dup
if @work.update(update_params)
work_compare = WorkCompareService.new(work_before, @work)
@work.log_changes(work_compare, current_user.id)

redirect_to work_readme_select_url(@work)
prepare_decorators_for_work_form(@work)
if WorkCompareService.update_work(work: @work, update_params:, current_user:)
if params[:save_only] == "true"
render :edit_wizard
else
redirect_to work_readme_select_url(@work)
end
else
# This is needed for rendering HTML views with validation errors
@form_resource_decorator = FormResourceDecorator.new(@work, current_user)

render :edit_wizard, status: :unprocessable_entity
end
end
end

# Prompt to select how to submit their files
# GET /works/1/attachment_select
def attachment_select
@wizard_mode = true
end
def attachment_select; end

# User selected a specific way to submit their files
# POST /works/1/attachment_selected
def attachment_selected
@wizard_mode = true
@work.files_location = params["attachment_type"]
@work.save!

# create a directory for the work if the curator will need to move files by hand
@work.s3_query_service.create_directory if @work.files_location != "file_upload"

next_url = case @work.files_location
when "file_upload"
work_file_upload_url(@work)
else
work_file_other_url(@work)
end
redirect_to next_url
if params[:save_only] == "true"
render :attachment_select
elsif @work.files_location == "file_upload"
redirect_to work_file_upload_url(@work)
else
redirect_to work_file_other_url(@work)
end
end

# Allow user to upload files directly
Expand All @@ -106,10 +104,13 @@ def file_uploaded
if files.count > 0
upload_service = WorkUploadsEditService.new(@work, current_user)
@work = upload_service.update_precurated_file_list(files, [])
@work.save!
@work.reload_snapshots
end
redirect_to(work_review_path)
if params[:save_only] == "true"
render :file_upload
else
redirect_to(work_review_path)
end
rescue StandardError => active_storage_error
Rails.logger.error("Failed to attach the file uploads for the work #{@work.doi}: #{active_storage_error}")
flash[:notice] = "Failed to attach the file uploads for the work #{@work.doi}: #{active_storage_error}. Please contact [email protected] for assistance."
Expand All @@ -127,6 +128,9 @@ def review
if request.method == "POST"
@work.location_notes = params["location_notes"]
@work.save!
if params[:save_only] == "true"
render :file_other
end
end
end

Expand All @@ -135,25 +139,32 @@ def review
def validate
@work.submission_notes = params["submission_notes"]
@work.complete_submission!(current_user)
redirect_to user_url(current_user)
if params[:save_only] == "true"
render :review
else
redirect_to user_url(current_user)
end
end

# Show the user the form to select a readme
# GET /works/1/readme_select
def readme_select
readme = Readme.new(@work, current_user)
@readme = readme.file_name
@wizard = true
end

# Uploads the readme the user selects
# GET /works/1/readme_uploaded
def readme_uploaded
@wizard = true
readme = Readme.new(@work, current_user)
readme_error = readme.attach(readme_file_param)
if readme_error.nil?
redirect_to work_attachment_select_url(@work)
if params[:save_only] == "true"
@readme = readme.file_name
render :readme_select
else
redirect_to work_attachment_select_url(@work)
end
else
flash[:notice] = readme_error
redirect_to work_readme_select_url(@work)
Expand Down Expand Up @@ -192,11 +203,11 @@ def rescue_aasm_error
Honeybadger.notify("Invalid #{@work.current_transition}: #{error.message} errors: #{message}")
transition_error_message = "We apologize, the following errors were encountered: #{message}. Please contact the PDC Describe administrators for any assistance."
@errors = [transition_error_message]
prepare_decorators_for_work_form(@work)

if @work.persisted?
redirect_to edit_work_wizard_path(id: @work.id), notice: transition_error_message, params:
else
@form_resource_decorator = FormResourceDecorator.new(@work, current_user)
redirect_to work_create_new_submission_path, notice: transition_error_message, params:
end
rescue StandardError => generic_error
Expand Down
18 changes: 18 additions & 0 deletions app/services/work_compare_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,24 @@
class WorkCompareService
attr_reader :differences

class << self
# Updates a work with the parameters and logs any changes
# @param [Work] work work to be updated
# @param [HashWithIndifferentAccess] update_params values to update the work with
# @param [] current_user user currently logged into the system
# @return [Boolean] true if update occured; false if update had errors
def update_work(work:, update_params:, current_user:)
work_before = work.dup
if work.update(update_params)
work_compare = new(work_before, work)
work.log_changes(work_compare, current_user.id)
true
else
false
end
end
end

def initialize(before, after)
@before = before
@after = after
Expand Down
Loading

0 comments on commit 5ba0354

Please sign in to comment.