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

pulibrary / pdc_describe / bc426aed-306d-47e9-94c2-7b74ea9c16e2

09 May 2024 06:01PM UTC coverage: 95.922% (+0.03%) from 95.89%
bc426aed-306d-47e9-94c2-7b74ea9c16e2

Pull #1806

circleci

hectorcorrea
Added logging
Pull Request #1806: Fixes provenance bug in the Wizard

5 of 9 new or added lines in 2 files covered. (55.56%)

9 existing lines in 1 file now uncovered.

3246 of 3384 relevant lines covered (95.92%)

221.68 hits per line

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

88.89
/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
# rubocop:disable Metrics/ClassLength
14
class WorksWizardController < ApplicationController
1✔
15
  include ERB::Util
1✔
16
  around_action :rescue_aasm_error, only: [:validate, :new_submission_save]
1✔
17

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

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

29
      prepare_decorators_for_work_form(@work)
13✔
30
    end
31
  end
32

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

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

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

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

51
    if params[:save_only] == "true"
15✔
52
      render :attachment_select
4✔
53
    else
54
      redirect_to file_location_url
11✔
55
    end
56
  end
57

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

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

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

78
    prepare_decorators_for_work_form(@work)
9✔
79
    if params[:save_only] == "true"
9✔
80
      render :file_upload
2✔
81
    else
82
      redirect_to(work_review_path)
7✔
83
    end
84
  rescue StandardError => active_storage_error
85
    Rails.logger.error("Failed to attach the file uploads for the work #{@work.doi}: #{active_storage_error}")
1✔
86
    flash[:notice] = "Failed to attach the file uploads for the work #{@work.doi}: #{active_storage_error}. Please contact rdss@princeton.edu for assistance."
1✔
87

88
    redirect_to work_file_upload_path(@work)
1✔
89
  end
90

91
  # Allow user to indicate where their files are located in the WWW
92
  # GET /works/1/file_other
93
  def file_other; end
1✔
94

95
  # GET /works/1/review
96
  # POST /works/1/review
97
  def review
1✔
98
    if request.method == "POST" || request.method == "PATCH"
13✔
99
      @work.location_notes = params["location_notes"]
7✔
100
      @work.save!
7✔
101
      if params[:save_only] == "true"
7✔
102
        render :file_other
2✔
103
      end
104
    end
105
  end
106

107
  # Validates that the work is ready to be approved
108
  # POST /works/1/validate-wizard
109
  # PATCH /works/1/validate-wizard
110
  def validate
1✔
111
    @work.submission_notes = params["submission_notes"]
9✔
112
    if params[:save_only] == "true"
9✔
113
      @work.save
2✔
114
      render :review
2✔
115
    else
116
      @work.complete_submission!(current_user)
7✔
117
      redirect_to user_url(current_user)
6✔
118
    end
119
  end
120

121
  # Show the user the form to select a readme
122
  # GET /works/1/readme_select
123
  def readme_select
1✔
124
    readme = Readme.new(@work, current_user)
14✔
125
    @readme = readme.file_name
14✔
126
  end
127

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

141
  # Uploads the README file, called by Uppy.
142
  # POST /works/1/readme-uploaded-payload
143
  def readme_uploaded_payload
1✔
144
    readme = Readme.new(@work, current_user)
2✔
145
    readme_file = params["files"].first
2✔
146
    readme_error = readme.attach(readme_file)
2✔
147
    if readme_error.nil?
2✔
148
      render plain: readme.file_name
1✔
149
    else
150
      # Tell Uppy there was an error uploading the README
151
      render plain: readme.file_name, status: :internal_server_error
1✔
152
    end
153
  end
154

155
  def file_location_url
1✔
156
    WorkMetadataService.file_location_url(@work)
22✔
157
  end
158
  helper_method :file_location_url
1✔
159

160
  private
1✔
161

162
    def edit_helper(view_name, redirect_url)
1✔
163
      if validate_modification_permissions(work: @work,
22✔
164
                                           uneditable_message: "Can not update work: #{@work.id} is not editable by #{current_user.uid}",
165
                                           current_state_message: "Can not update work: #{@work.id} is not editable in current state by #{current_user.uid}")
166
        prepare_decorators_for_work_form(@work)
22✔
167
        if WorkCompareService.update_work(work: @work, update_params:, current_user:)
22✔
168
          if params[:save_only] == "true"
21✔
169
            render view_name
3✔
170
          else
171
            redirect_to redirect_url
18✔
172
          end
173
        else
174
          render view_name, status: :unprocessable_entity
1✔
175
        end
176
      end
177
    end
178

179
    def load_work
1✔
180
      @work = Work.find(params[:id])
148✔
181
    end
182

183
    def patch_params
1✔
UNCOV
184
      return {} unless params.key?(:patch)
×
185

UNCOV
186
      params[:patch]
×
187
    end
188

189
    def pre_curation_uploads_param
1✔
UNCOV
190
      return if patch_params.nil?
×
191

UNCOV
192
      patch_params[:pre_curation_uploads]
×
193
    end
194

195
    def deleted_files_param
1✔
196
      deleted_count = (params.dig("work", "deleted_files_count") || "0").to_i
10✔
197
      (1..deleted_count).map { |i| params.dig("work", "deleted_file_#{i}") }.select(&:present?)
13✔
198
    end
199

200
    def readme_file_param
1✔
UNCOV
201
      return if patch_params.nil?
×
202

UNCOV
203
      patch_params[:readme_file]
×
204
    end
205

206
    def rescue_aasm_error
1✔
207
      super
9✔
208
    rescue StandardError => generic_error
UNCOV
209
      redirect_to root_url, notice: "We apologize, an error was encountered: #{generic_error.message}. Please contact the PDC Describe administrators."
×
210
    end
211

212
    def redirect_aasm_error(transition_error_message)
1✔
213
      if @work.persisted?
1✔
214
        redirect_to edit_work_wizard_path(id: @work.id), notice: transition_error_message, params:
1✔
215
      else
UNCOV
216
        redirect_to work_create_new_submission_path(@work), notice: transition_error_message, params:
×
217
      end
218
    end
219
end
220
# rubocop:enable Metrics/ClassLength
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