• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pulibrary / pdc_describe / cace366a-ffad-45f1-9b60-678e607fa527

14 May 2024 02:21PM UTC coverage: 60.862% (-35.0%) from 95.908%
cace366a-ffad-45f1-9b60-678e607fa527

push

circleci

jrgriffiniii
wip

1 of 3 new or added lines in 2 files covered. (33.33%)

1194 existing lines in 57 files now uncovered.

2076 of 3411 relevant lines covered (60.86%)

22.71 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

33.33
/app/controllers/works_wizard_controller.rb
1
# frozen_string_literal: true
2

3
require "nokogiri"
1✔
4
require "open-uri"
1✔
5

6
# Controller to handle wizard Mode when editing an work
7
#
8
# The wizard flow is as follows:
9
# new_submission -> new_submission_save -> edit_wizard -> update_wizard -> update_additional -> update_additional_save ->readme_select -> readme_uploaded -> attachment_select ->
10
#     attachment_selected -> file_other ->                  review -> validate -> [ work controller ] show & file_list
11
#                         \> file_upload -> file_uploaded -^
12

13
class WorksWizardController < ApplicationController
1✔
14
  include ERB::Util
1✔
15
  around_action :rescue_aasm_error, only: [:validate, :new_submission_save]
1✔
16

17
  before_action :load_work, only: [:edit_wizard, :update_wizard, :attachment_select, :attachment_selected,
1✔
18
                                   :file_upload, :file_uploaded, :file_other, :review, :validate,
19
                                   :readme_select, :readme_uploaded, :readme_uploaded_payload]
20

21
  # GET /works/1/edit-wizard
22
  def edit_wizard
1✔
23
    @wizard_mode = true
1✔
24
    if validate_modification_permissions(work: @work,
1✔
25
                                         uneditable_message: "Can not edit work: #{@work.id} is not editable by #{current_user.uid}",
26
                                         current_state_message: "Can not edit work: #{@work.id} is not editable in current state by #{current_user.uid}")
27

28
      prepare_decorators_for_work_form(@work)
1✔
29
    end
30
  end
31

32
  # PATCH /works/1/update-wizard
33
  def update_wizard
1✔
UNCOV
34
    edit_helper(:edit_wizard, work_update_additional_path(@work))
×
35
  end
36

37
  # Prompt to select how to submit their files
38
  # GET /works/1/attachment_select
39
  def attachment_select; end
1✔
40

41
  # User selected a specific way to submit their files
42
  # POST /works/1/attachment_selected
43
  def attachment_selected
1✔
UNCOV
44
    @work.files_location = params["attachment_type"]
×
UNCOV
45
    @work.save!
×
46

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

UNCOV
50
    if params[:save_only] == "true"
×
UNCOV
51
      render :attachment_select
×
52
    else
UNCOV
53
      redirect_to file_location_url
×
54
    end
55
  end
56

57
  # Allow user to upload files directly
58
  # GET /works/1/file_upload
59
  def file_upload
1✔
UNCOV
60
    @work_decorator = WorkDecorator.new(@work, current_user)
×
61
  end
62

63
  # POST /works/1/upload-files-wizard (called via Uppy)
64
  def upload_files
1✔
65
    @work = Work.find(params[:id])
×
66
    upload_service = WorkUploadsEditService.new(@work, current_user)
×
67
    upload_service.update_precurated_file_list(params["files"], [])
×
68
  end
69

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

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

UNCOV
92
    redirect_to work_file_upload_path(@work)
×
93
  end
94

95
  # Allow user to indicate where their files are located in the WWW
96
  # GET /works/1/file_other
97
  def file_other; end
1✔
98

99
  # GET /works/1/review
100
  # POST /works/1/review
101
  def review
1✔
UNCOV
102
    if request.method == "POST" || request.method == "PATCH"
×
UNCOV
103
      @work.location_notes = params["location_notes"]
×
UNCOV
104
      @work.save!
×
UNCOV
105
      if params[:save_only] == "true"
×
UNCOV
106
        render :file_other
×
107
      end
108
    end
109
  end
110

111
  # Validates that the work is ready to be approved
112
  # POST /works/1/validate-wizard
113
  # PATCH /works/1/validate-wizard
114
  def validate
1✔
UNCOV
115
    @work.submission_notes = params["submission_notes"]
×
116

UNCOV
117
    if params[:save_only] == "true"
×
UNCOV
118
      @work.save
×
UNCOV
119
      render :review
×
120
    else
UNCOV
121
      @work.complete_submission!(current_user)
×
UNCOV
122
      redirect_to user_url(current_user)
×
123
    end
124
  end
125

126
  # Show the user the form to select a readme
127
  # GET /works/1/readme_select
128
  def readme_select
1✔
UNCOV
129
    readme = Readme.new(@work, current_user)
×
UNCOV
130
    @readme = readme.file_name
×
131
  end
132

133
  # Hit when the user clicks "Save" or "Next" on the README upload process.
134
  # Notice that this does not really uploads the file, that happens in readme_uploaded_payload.
135
  # PATCH /works/1/readme_uploaded
136
  def readme_uploaded
1✔
UNCOV
137
    readme = Readme.new(@work, current_user)
×
UNCOV
138
    if params[:save_only] == "true"
×
UNCOV
139
      @readme = readme.file_name
×
UNCOV
140
      render :readme_select
×
141
    else
UNCOV
142
      redirect_to work_attachment_select_url(@work)
×
143
    end
144
  end
145

146
  def files_param
1✔
UNCOV
147
    params["files"]
×
148
  end
149

150
  # Uploads the README file, called by Uppy.
151
  # POST /works/1/readme-uploaded-payload
152
  def readme_uploaded_payload
1✔
UNCOV
153
    raise StandardError("Only one README file can be uploaded.") if files_param.empty?
×
UNCOV
154
    raise StandardError("Only one README file can be uploaded.") if files_param.length > 1
×
155

UNCOV
156
    readme_file = files_param.first
×
UNCOV
157
    readme = Readme.new(@work, current_user)
×
158

UNCOV
159
    readme_error = readme.attach(readme_file)
×
UNCOV
160
    if readme_error.nil?
×
UNCOV
161
      render plain: readme.file_name
×
162
    else
163
      # Tell Uppy there was an error uploading the README
UNCOV
164
      render plain: readme.file_name, status: :internal_server_error
×
165
    end
166
  end
167

168
  def file_location_url
1✔
UNCOV
169
    WorkMetadataService.file_location_url(@work)
×
170
  end
171
  helper_method :file_location_url
1✔
172

173
  private
1✔
174

175
    def edit_helper(view_name, redirect_url)
1✔
UNCOV
176
      if validate_modification_permissions(work: @work,
×
177
                                           uneditable_message: "Can not update work: #{@work.id} is not editable by #{current_user.uid}",
178
                                           current_state_message: "Can not update work: #{@work.id} is not editable in current state by #{current_user.uid}")
UNCOV
179
        prepare_decorators_for_work_form(@work)
×
UNCOV
180
        if WorkCompareService.update_work(work: @work, update_params:, current_user:)
×
UNCOV
181
          if params[:save_only] == "true"
×
UNCOV
182
            render view_name
×
183
          else
UNCOV
184
            redirect_to redirect_url
×
185
          end
186
        else
UNCOV
187
          render view_name, status: :unprocessable_entity
×
188
        end
189
      end
190
    end
191

192
    def load_work
1✔
193
      @work = Work.find(params[:id])
1✔
194
    end
195

196
    def patch_params
1✔
197
      return {} unless params.key?(:patch)
×
198

199
      params[:patch]
×
200
    end
201

202
    def pre_curation_uploads_param
1✔
203
      return if patch_params.nil?
×
204

205
      patch_params[:pre_curation_uploads]
×
206
    end
207

208
    def deleted_files_param
1✔
UNCOV
209
      deleted_count = (params.dig("work", "deleted_files_count") || "0").to_i
×
UNCOV
210
      (1..deleted_count).map { |i| params.dig("work", "deleted_file_#{i}") }.select(&:present?)
×
211
    end
212

213
    def readme_file_param
1✔
214
      return if patch_params.nil?
×
215

216
      patch_params[:readme_file]
×
217
    end
218

219
    def rescue_aasm_error
1✔
UNCOV
220
      super
×
221
    rescue StandardError => generic_error
222
      redirect_to root_url, notice: "We apologize, an error was encountered: #{generic_error.message}. Please contact the PDC Describe administrators."
×
223
    end
224

225
    def redirect_aasm_error(transition_error_message)
1✔
UNCOV
226
      if @work.persisted?
×
UNCOV
227
        redirect_to edit_work_wizard_path(id: @work.id), notice: transition_error_message, params:
×
228
      else
229
        redirect_to work_create_new_submission_path(@work), notice: transition_error_message, params:
×
230
      end
231
    end
232
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc