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

pulibrary / pdc_describe / 5575969d-37fa-4bec-8c5a-c81ee1a91e0a

12 Apr 2024 03:45PM UTC coverage: 95.782% (-0.08%) from 95.857%
5575969d-37fa-4bec-8c5a-c81ee1a91e0a

Pull #1756

circleci

hectorcorrea
Added functionality to handle file deletes through the Wizard
Pull Request #1756: Handle file deletes through the Wizard

4 of 4 new or added lines in 1 file covered. (100.0%)

4 existing lines in 1 file now uncovered.

3247 of 3390 relevant lines covered (95.78%)

208.99 hits per line

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

96.12
/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 Renders the "step 0" information page before creating a new dataset
22
  # GET /works/new_submission
23
  def new_submission
1✔
24
    @work = WorkMetadataService.new(params:, current_user:).work_for_new_submission
14✔
25
    prepare_decorators_for_work_form(@work)
14✔
26
  end
27

28
  # Creates the new dataset or update the dataset is save only was done previously
29
  # POST /works/new_submission or POST /works/1/new_submission
30
  def new_submission_save
1✔
31
    @work = WorkMetadataService.new(params:, current_user:).new_submission
10✔
32
    @errors = @work.errors.to_a
10✔
33
    if @errors.count.positive?
10✔
34
      prepare_decorators_for_work_form(@work)
1✔
35
      render :new_submission
1✔
36
    else
37
      redirect_to edit_work_wizard_path(@work)
9✔
38
    end
39
  end
40

41
  # GET /works/1/edit-wizard
42
  def edit_wizard
1✔
43
    @wizard_mode = true
11✔
44
    if validate_modification_permissions(work: @work,
11✔
45
                                         uneditable_message: "Can not edit work: #{@work.id} is not editable by #{current_user.uid}",
46
                                         current_state_message: "Can not edit work: #{@work.id} is not editable in current state by #{current_user.uid}")
47

48
      prepare_decorators_for_work_form(@work)
11✔
49
    end
50
  end
51

52
  # PATCH /works/1/update-wizard
53
  def update_wizard
1✔
54
    edit_helper(:edit_wizard, work_update_additional_path(@work))
12✔
55
  end
56

57
  # Prompt to select how to submit their files
58
  # GET /works/1/attachment_select
59
  def attachment_select; end
1✔
60

61
  # User selected a specific way to submit their files
62
  # POST /works/1/attachment_selected
63
  def attachment_selected
1✔
64
    @work.files_location = params["attachment_type"]
14✔
65
    @work.save!
14✔
66

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

70
    if params[:save_only] == "true"
14✔
71
      render :attachment_select
3✔
72
    else
73
      redirect_to file_location_url
11✔
74
    end
75
  end
76

77
  # Allow user to upload files directly
78
  # GET /works/1/file_upload
79
  def file_upload
1✔
80
    @work_decorator = WorkDecorator.new(@work, current_user)
8✔
81
  end
82

83
  # POST /works/1/file_upload
84
  def file_uploaded
1✔
85
    upload_service = WorkUploadsEditService.new(@work, current_user)
10✔
86
    # By the time we hit this endpoint files have been uploaded by Uppy submmitting POST requests
87
    # to /works/1/upload-files therefore we only need to handle deleted files here.
88
    @work = upload_service.update_precurated_file_list([], deleted_files_param)
10✔
89
    @work.reload_snapshots
9✔
90
    prepare_decorators_for_work_form(@work)
9✔
91
    if params[:save_only] == "true"
9✔
92
      render :file_upload
2✔
93
    else
94
      redirect_to(work_review_path)
7✔
95
    end
96
  rescue StandardError => active_storage_error
97
    Rails.logger.error("Failed to attach the file uploads for the work #{@work.doi}: #{active_storage_error}")
1✔
98
    flash[:notice] = "Failed to attach the file uploads for the work #{@work.doi}: #{active_storage_error}. Please contact rdss@princeton.edu for assistance."
1✔
99

100
    redirect_to work_file_upload_path(@work)
1✔
101
  end
102

103
  # Allow user to indicate where their files are located in the WWW
104
  # GET /works/1/file_other
105
  def file_other; end
1✔
106

107
  # GET /works/1/review
108
  # POST /works/1/review
109
  def review
1✔
110
    if request.method == "POST" || request.method == "PATCH"
13✔
111
      @work.location_notes = params["location_notes"]
7✔
112
      @work.save!
7✔
113
      if params[:save_only] == "true"
7✔
114
        render :file_other
2✔
115
      end
116
    end
117
  end
118

119
  # Validates that the work is ready to be approved
120
  # GET /works/1/validate
121
  def validate
1✔
122
    @work.submission_notes = params["submission_notes"]
33✔
123
    if params[:save_only] == "true"
33✔
124
      @work.save
2✔
125
      render :review
2✔
126
    else
127
      @work.complete_submission!(current_user)
31✔
128
      redirect_to user_url(current_user)
30✔
129
    end
130
  end
131

132
  # Show the user the form to select a readme
133
  # GET /works/1/readme_select
134
  def readme_select
1✔
135
    readme = Readme.new(@work, current_user)
14✔
136
    @readme = readme.file_name
14✔
137
  end
138

139
  # Uploads the readme the user selects
140
  # GET /works/1/readme_uploaded
141
  def readme_uploaded
1✔
142
    readme = Readme.new(@work, current_user)
13✔
143
    readme_error = readme.attach(readme_file_param)
13✔
144
    if readme_error.nil?
13✔
145
      if params[:save_only] == "true"
12✔
146
        @readme = readme.file_name
2✔
147
        render :readme_select
2✔
148
      else
149
        redirect_to work_attachment_select_url(@work)
10✔
150
      end
151
    else
152
      flash[:notice] = readme_error
1✔
153
      redirect_to work_readme_select_url(@work)
1✔
154
    end
155
  end
156

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

162
  private
1✔
163

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

181
    def load_work
1✔
182
      @work = Work.find(params[:id])
168✔
183
    end
184

185
    def patch_params
1✔
186
      return {} unless params.key?(:patch)
26✔
187

188
      params[:patch]
16✔
189
    end
190

191
    def pre_curation_uploads_param
1✔
UNCOV
192
      return if patch_params.nil?
×
193

UNCOV
194
      patch_params[:pre_curation_uploads]
×
195
    end
196

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

202
    def readme_file_param
1✔
203
      return if patch_params.nil?
13✔
204

205
      patch_params[:readme_file]
13✔
206
    end
207

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

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