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

pulibrary / pdc_describe / 6d3ba70e-74ee-40ef-a5be-d2cda3dbf2d7

15 Apr 2024 06:35PM UTC coverage: 95.333% (-0.5%) from 95.804%
6d3ba70e-74ee-40ef-a5be-d2cda3dbf2d7

Pull #1762

circleci

carolyncole
Added an explicit endpoint to the Wizard controlller to upload files. Moved the logic to upload a file to the controller instead of my hack via the Readme class.
Pull Request #1762: Fixes Wizard delay in displaying uploaded files

3 of 22 new or added lines in 2 files covered. (13.64%)

34 existing lines in 3 files now uncovered.

3268 of 3428 relevant lines covered (95.33%)

210.97 hits per line

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

88.46
/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]
20

21
  # GET /works/1/edit-wizard
22
  def edit_wizard
1✔
23
    @wizard_mode = true
12✔
24
    if validate_modification_permissions(work: @work,
12✔
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)
12✔
29
    end
30
  end
31

32
  # PATCH /works/1/update-wizard
33
  def update_wizard
1✔
34
    edit_helper(:edit_wizard, work_update_additional_path(@work))
12✔
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✔
44
    @work.files_location = params["attachment_type"]
15✔
45
    @work.save!
15✔
46

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

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

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

63
  # POST /works/1/upload-files-wizard (called via Uppy)
64
  def upload_files
1✔
NEW
65
    @work = Work.find(params[:id])
×
NEW
66
    params["files"].each { |file| upload_file(file) }
×
67
  end
68

69
  # POST /works/1/file_upload
70
  def file_uploaded
1✔
71
    upload_service = WorkUploadsEditService.new(@work, current_user)
10✔
72
    # By the time we hit this endpoint files have been uploaded by Uppy submmitting POST requests
73
    # to /works/1/upload-files-wizard therefore we only need to handle deleted files here.
74
    @work = upload_service.update_precurated_file_list([], deleted_files_param)
10✔
75
    @work.reload_snapshots
9✔
76
    prepare_decorators_for_work_form(@work)
9✔
77
    if params[:save_only] == "true"
9✔
78
      render :file_upload
2✔
79
    else
80
      redirect_to(work_review_path)
7✔
81
    end
82
  rescue StandardError => active_storage_error
83
    Rails.logger.error("Failed to attach the file uploads for the work #{@work.doi}: #{active_storage_error}")
1✔
84
    flash[:notice] = "Failed to attach the file uploads for the work #{@work.doi}: #{active_storage_error}. Please contact rdss@princeton.edu for assistance."
1✔
85

86
    redirect_to work_file_upload_path(@work)
1✔
87
  end
88

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

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

105
  # Validates that the work is ready to be approved
106
  # GET /works/1/validate
107
  def validate
1✔
108
    @work.submission_notes = params["submission_notes"]
33✔
109
    if params[:save_only] == "true"
33✔
110
      @work.save
2✔
111
      render :review
2✔
112
    else
113
      @work.complete_submission!(current_user)
31✔
114
      redirect_to user_url(current_user)
30✔
115
    end
116
  end
117

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

125
  # Uploads the readme the user selects
126
  # GET /works/1/readme_uploaded
127
  def readme_uploaded
1✔
128
    readme = Readme.new(@work, current_user)
13✔
129
    readme_error = readme.attach(readme_file_param)
13✔
130
    if readme_error.nil?
13✔
131
      if params[:save_only] == "true"
12✔
132
        @readme = readme.file_name
2✔
133
        @work.reload_snapshots
2✔
134
        render :readme_select
2✔
135
      else
136
        redirect_to work_attachment_select_url(@work)
10✔
137
      end
138
    else
139
      flash[:notice] = readme_error
1✔
140
      redirect_to work_readme_select_url(@work)
1✔
141
    end
142
  end
143

144
  def file_location_url
1✔
145
    WorkMetadataService.file_location_url(@work)
22✔
146
  end
147
  helper_method :file_location_url
1✔
148

149
  private
1✔
150

151
    def edit_helper(view_name, redirect_url)
1✔
152
      if validate_modification_permissions(work: @work,
22✔
153
                                           uneditable_message: "Can not update work: #{@work.id} is not editable by #{current_user.uid}",
154
                                           current_state_message: "Can not update work: #{@work.id} is not editable in current state by #{current_user.uid}")
155
        prepare_decorators_for_work_form(@work)
22✔
156
        if WorkCompareService.update_work(work: @work, update_params:, current_user:)
22✔
157
          if params[:save_only] == "true"
21✔
158
            render view_name
3✔
159
          else
160
            redirect_to redirect_url
18✔
161
          end
162
        else
163
          render view_name, status: :unprocessable_entity
1✔
164
        end
165
      end
166
    end
167

168
    def load_work
1✔
169
      @work = Work.find(params[:id])
170✔
170
    end
171

172
    def patch_params
1✔
173
      return {} unless params.key?(:patch)
26✔
174

175
      params[:patch]
16✔
176
    end
177

178
    def pre_curation_uploads_param
1✔
UNCOV
179
      return if patch_params.nil?
×
180

UNCOV
181
      patch_params[:pre_curation_uploads]
×
182
    end
183

184
    def deleted_files_param
1✔
185
      deleted_count = (params.dig("work", "deleted_files_count") || "0").to_i
10✔
186
      (1..deleted_count).map { |i| params.dig("work", "deleted_file_#{i}") }.select(&:present?)
13✔
187
    end
188

189
    def readme_file_param
1✔
190
      return if patch_params.nil?
13✔
191

192
      patch_params[:readme_file]
13✔
193
    end
194

195
    def rescue_aasm_error
1✔
196
      super
33✔
197
    rescue StandardError => generic_error
UNCOV
198
      redirect_to root_url, notice: "We apologize, an error was encountered: #{generic_error.message}. Please contact the PDC Describe administrators."
×
199
    end
200

201
    def redirect_aasm_error(transition_error_message)
1✔
202
      if @work.persisted?
1✔
203
        redirect_to edit_work_wizard_path(id: @work.id), notice: transition_error_message, params:
1✔
204
      else
UNCOV
205
        redirect_to work_create_new_submission_path(@work), notice: transition_error_message, params:
×
206
      end
207
    end
208

209
    def upload_file(file)
1✔
NEW
210
      key = @work.s3_query_service.upload_file(io: file.to_io, filename: file.original_filename, size: file.size)
×
NEW
211
      if key
×
NEW
212
        UploadSnapshot.create(work: @work, files: [{ "filename" => key, "checksum" => @work.s3_query_service.last_response.etag.delete('"') }])
×
NEW
213
        @work.track_change(:added, key)
×
NEW
214
        @work.log_file_changes(current_user.id)
×
215
      else
NEW
216
        Rails.logger.error("Error uploading #{file.original_filename} to work #{@work.id}")
×
217
      end
218
    end
219
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