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

pulibrary / pdc_describe / bf5b0460-23cf-4c32-8a80-40068abc5735

12 Apr 2024 04:33PM UTC coverage: 95.8% (-0.06%) from 95.857%
bf5b0460-23cf-4c32-8a80-40068abc5735

Pull #1756

circleci

web-flow
Merge branch 'main' into 1751-delete-file
Pull Request #1756: Handle file deletes through the Wizard

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

2 existing lines in 1 file now uncovered.

3262 of 3405 relevant lines covered (95.8%)

207.84 hits per line

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

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

80
    redirect_to work_file_upload_path(@work)
1✔
81
  end
82

83
  # Allow user to indicate where their files are located in the WWW
84
  # GET /works/1/file_other
85
  def file_other; end
1✔
86

87
  # GET /works/1/review
88
  # POST /works/1/review
89
  def review
1✔
90
    if request.method == "POST" || request.method == "PATCH"
13✔
91
      @work.location_notes = params["location_notes"]
7✔
92
      @work.save!
7✔
93
      if params[:save_only] == "true"
7✔
94
        render :file_other
2✔
95
      end
96
    end
97
  end
98

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

112
  # Show the user the form to select a readme
113
  # GET /works/1/readme_select
114
  def readme_select
1✔
115
    readme = Readme.new(@work, current_user)
14✔
116
    @readme = readme.file_name
14✔
117
  end
118

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

137
  def file_location_url
1✔
138
    WorkMetadataService.file_location_url(@work)
22✔
139
  end
140
  helper_method :file_location_url
1✔
141

142
  private
1✔
143

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

161
    def load_work
1✔
162
      @work = Work.find(params[:id])
169✔
163
    end
164

165
    def patch_params
1✔
166
      return {} unless params.key?(:patch)
26✔
167

168
      params[:patch]
16✔
169
    end
170

171
    def pre_curation_uploads_param
1✔
UNCOV
172
      return if patch_params.nil?
×
173

UNCOV
174
      patch_params[:pre_curation_uploads]
×
175
    end
176

177
    def deleted_files_param
1✔
178
      deleted_count = (params.dig("work", "deleted_files_count") || "0").to_i
10✔
179
      (1..deleted_count).map { |i| params.dig("work", "deleted_file_#{i}") }.select(&:present?)
13✔
180
    end
181

182
    def readme_file_param
1✔
183
      return if patch_params.nil?
13✔
184

185
      patch_params[:readme_file]
13✔
186
    end
187

188
    def rescue_aasm_error
1✔
189
      super
33✔
190
    rescue StandardError => generic_error
191
      redirect_to root_url, notice: "We apologize, an error was encountered: #{generic_error.message}. Please contact the PDC Describe administrators."
×
192
    end
193

194
    def redirect_aasm_error(transition_error_message)
1✔
195
      if @work.persisted?
1✔
196
        redirect_to edit_work_wizard_path(id: @work.id), notice: transition_error_message, params:
1✔
197
      else
198
        redirect_to work_create_new_submission_path(@work), notice: transition_error_message, params:
×
199
      end
200
    end
201
end
202
# 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