Class: WorksWizardController

Inherits:
ApplicationController show all
Includes:
ERB::Util
Defined in:
app/controllers/works_wizard_controller.rb

Overview

Controller to handle wizard Mode when editing an work

The wizard flow is shown in the mermaid diagram here.

Direct Known Subclasses

WorksWizardUpdateAdditionalController

Instance Method Summary collapse

Methods inherited from ApplicationController

#new_session_path

Instance Method Details

#attachment_selectObject

Prompt to select how to submit their files GET /works/1/attachment_select



36
# File 'app/controllers/works_wizard_controller.rb', line 36

def attachment_select; end

#attachment_selectedObject

User selected a specific way to submit their files POST /works/1/attachment_selected



40
41
42
43
44
45
46
47
48
49
# File 'app/controllers/works_wizard_controller.rb', line 40

def attachment_selected
  @work.files_location = params["attachment_type"]
  @work.save!

  if params[:save_only] == "true"
    render :attachment_select
  else
    redirect_to file_location_url
  end
end

#edit_wizardObject

GET /works/1/edit-wizard



19
20
21
22
23
24
25
26
27
# File 'app/controllers/works_wizard_controller.rb', line 19

def edit_wizard
  @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}")

    prepare_decorators_for_work_form(@work)
  end
end

#file_location_urlObject



163
164
165
# File 'app/controllers/works_wizard_controller.rb', line 163

def file_location_url
  WorkMetadataService.file_location_url(@work)
end

#file_otherObject

Allow user to indicate where their files are located in the WWW GET /works/1/file_other



92
# File 'app/controllers/works_wizard_controller.rb', line 92

def file_other; end

#file_uploadObject

Allow user to upload files directly GET /works/1/file_upload



53
54
55
# File 'app/controllers/works_wizard_controller.rb', line 53

def file_upload
  @work_decorator = WorkDecorator.new(@work, current_user)
end

#file_uploadedObject

POST /works/1/file_upload



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'app/controllers/works_wizard_controller.rb', line 66

def file_uploaded
  upload_service = WorkUploadsEditService.new(@work, current_user)
  # By the time we hit this endpoint files have been uploaded by Uppy submmitting POST requests
  # to /works/1/upload-files-wizard therefore we only need to delete files here and update the upload snapshot.
  @work = upload_service.snapshot_uppy_and_delete_files(deleted_files_param)

  prepare_decorators_for_work_form(@work)
  if params[:save_only] == "true"
    render :file_upload
  else
    redirect_to(work_review_path)
  end
rescue => ex
  # Notice that we log the URL (rather than @work.doi) because sometimes we are getting a nil @work.
  # The URL will include the ID and might help us troubleshoot the issue further if it happens again.
  # See https://github.com/pulibrary/pdc_describe/issues/1801
  error_message = "Failed to update work snapshot, URL: #{request.url}: #{ex}"
  Rails.logger.error(error_message)
  Honeybadger.notify(error_message)
  flash[:notice] = "Failed to update work snapshot, work: #{@work&.doi}: #{ex}. Please contact rdss@princeton.edu for assistance."

  redirect_to work_file_upload_path(@work)
end

#files_paramObject



141
142
143
# File 'app/controllers/works_wizard_controller.rb', line 141

def files_param
  params["files"]
end

#readme_selectObject

Show the user the form to select a readme GET /works/1/readme_select



123
124
125
126
# File 'app/controllers/works_wizard_controller.rb', line 123

def readme_select
  readme = Readme.new(@work, current_user)
  @readme = readme.file_name
end

#readme_uploadedObject

Hit when the user clicks "Save" or "Next" on the README upload process. Notice that this does not really uploads the file, that happens in readme_uploaded_payload. PATCH /works/1/readme_uploaded



131
132
133
134
135
136
137
138
139
# File 'app/controllers/works_wizard_controller.rb', line 131

def readme_uploaded
  readme = Readme.new(@work, current_user)
  if params[:save_only] == "true"
    @readme = readme.file_name
    render :readme_select
  else
    redirect_to work_attachment_select_url(@work)
  end
end

#readme_uploaded_payloadObject

Uploads the README file, called by Uppy. POST /works/1/readme-uploaded-payload



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'app/controllers/works_wizard_controller.rb', line 147

def readme_uploaded_payload
  raise StandardError("Only one README file can be uploaded.") if files_param.empty?
  raise StandardError("Only one README file can be uploaded.") if files_param.length > 1

  readme_file = files_param.first
  readme = Readme.new(@work, current_user)

  readme_error = readme.attach(readme_file)
  if readme_error.nil?
    render plain: readme.file_name
  else
    # Tell Uppy there was an error uploading the README
    render plain: readme.file_name, status: :internal_server_error
  end
end

#reviewObject

GET /works/1/review POST /works/1/review



96
97
98
99
100
101
102
103
104
# File 'app/controllers/works_wizard_controller.rb', line 96

def review
  if request.method == "POST" || request.method == "PATCH"
    @work.location_notes = params["location_notes"]
    @work.save!
    if params[:save_only] == "true"
      render :file_other
    end
  end
end

#update_wizardObject

PATCH /works/1/update-wizard



30
31
32
# File 'app/controllers/works_wizard_controller.rb', line 30

def update_wizard
  edit_helper(:edit_wizard, work_update_additional_path(@work))
end

#upload_filesObject

POST /works/1/upload-files-wizard (called via Uppy)



58
59
60
61
62
63
# File 'app/controllers/works_wizard_controller.rb', line 58

def upload_files
  @work = Work.find(params[:id])
  upload_service = WorkUploadsEditService.new(@work, current_user)
  upload_service.update_precurated_file_list(params["files"], [])
  render plain: params["files"].map(&:original_filename).join(",")
end

#validateObject

Validates that the work is ready to be approved POST /works/1/validate-wizard PATCH /works/1/validate-wizard



109
110
111
112
113
114
115
116
117
118
119
# File 'app/controllers/works_wizard_controller.rb', line 109

def validate
  @work.submission_notes = params["submission_notes"]

  if params[:save_only] == "true"
    @work.save
    render :review
  else
    @work.complete_submission!(current_user)
    redirect_to work_complete_path(@work.id)
  end
end