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

pulibrary / pdc_describe / e47956b1-8671-448d-bca6-1d06dec32474

28 Mar 2024 12:09PM UTC coverage: 95.909% (+0.02%) from 95.886%
e47956b1-8671-448d-bca6-1d06dec32474

Pull #1721

circleci

carolyncole
added save only to validate
This completes the work, except for the fact that rubocop is not happy with the class now
Pull Request #1721: Added save_only parameter processing to update_wizard

33 of 33 new or added lines in 3 files covered. (100.0%)

1 existing line in 1 file now uncovered.

3212 of 3349 relevant lines covered (95.91%)

206.1 hits per line

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

99.16
/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 -> 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
    group = Group.find_by(code: params[:group_code]) || current_user.default_group
13✔
25
    group_id = group.id
13✔
26
    @work = Work.new(created_by_user_id: current_user.id, group_id:)
13✔
27
    @work_decorator = WorkDecorator.new(@work, current_user)
13✔
28
    @form_resource_decorator = FormResourceDecorator.new(@work, current_user)
13✔
29
  end
30

31
  # Creates the new dataset
32
  # POST /works/1/new_submission
33
  def new_submission_save
1✔
34
    group = Group.find_by(code: params[:group_code]) || current_user.default_group
10✔
35
    group_id = group.id
10✔
36
    @work = Work.new(created_by_user_id: current_user.id, group_id:)
10✔
37
    @work.resource = FormToResourceService.convert(params, @work)
10✔
38
    @work.draft!(current_user)
10✔
39
    if params[:save_only] == "true"
9✔
40
      @form_resource_decorator = FormResourceDecorator.new(@work, current_user)
1✔
41
      @work_decorator = WorkDecorator.new(@work, current_user)
1✔
42
      render :new_submission
1✔
43
    else
44
      redirect_to edit_work_wizard_path(@work)
8✔
45
    end
46
  end
47

48
  # GET /works/1/edit-wizard
49
  def edit_wizard
1✔
50
    @work_decorator = WorkDecorator.new(@work, current_user)
8✔
51
    @wizard_mode = true
8✔
52
    if validate_modification_permissions(work: @work,
8✔
53
                                         uneditable_message: "Can not edit work: #{@work.id} is not editable by #{current_user.uid}",
54
                                         current_state_message: "Can not edit work: #{@work.id} is not editable in current state by #{current_user.uid}")
55

56
      @form_resource_decorator = FormResourceDecorator.new(@work, current_user)
8✔
57
    end
58
  end
59

60
  # PATCH /works/1/update-wizard
61
  def update_wizard
1✔
62
    if validate_modification_permissions(work: @work,
8✔
63
                                         uneditable_message: "Can not update work: #{@work.id} is not editable by #{current_user.uid}",
64
                                         current_state_message: "Can not update work: #{@work.id} is not editable in current state by #{current_user.uid}")
65
      work_before = @work.dup
8✔
66
      if @work.update(update_params)
8✔
67
        work_compare = WorkCompareService.new(work_before, @work)
7✔
68
        @work.log_changes(work_compare, current_user.id)
7✔
69

70
        if params[:save_only] == "true"
7✔
71
          @form_resource_decorator = FormResourceDecorator.new(@work, current_user)
1✔
72
          render :edit_wizard
1✔
73
        else
74
          redirect_to work_readme_select_url(@work)
6✔
75
        end
76
      else
77
        # This is needed for rendering HTML views with validation errors
78
        @form_resource_decorator = FormResourceDecorator.new(@work, current_user)
1✔
79

80
        render :edit_wizard, status: :unprocessable_entity
1✔
81
      end
82
    end
83
  end
84

85
  # Prompt to select how to submit their files
86
  # GET /works/1/attachment_select
87
  def attachment_select
1✔
88
    @wizard_mode = true
7✔
89
  end
90

91
  # User selected a specific way to submit their files
92
  # POST /works/1/attachment_selected
93
  def attachment_selected
1✔
94
    @wizard_mode = true
8✔
95
    @work.files_location = params["attachment_type"]
8✔
96
    @work.save!
8✔
97

98
    # create a directory for the work if the curator will need to move files by hand
99
    @work.s3_query_service.create_directory if @work.files_location != "file_upload"
8✔
100

101
    if params[:save_only] == "true"
8✔
102
      render :attachment_select
2✔
103
    else
104

105
      next_url = case @work.files_location
6✔
106
                 when "file_upload"
107
                   work_file_upload_url(@work)
2✔
108
                 else
109
                   work_file_other_url(@work)
4✔
110
                 end
111
      redirect_to next_url
6✔
112
    end
113
  end
114

115
  # Allow user to upload files directly
116
  # GET /works/1/file_upload
117
  def file_upload; end
1✔
118

119
  # POST /works/1/file_upload
120
  def file_uploaded
1✔
121
    files = pre_curation_uploads_param || []
6✔
122
    if files.count > 0
6✔
123
      upload_service = WorkUploadsEditService.new(@work, current_user)
4✔
124
      @work = upload_service.update_precurated_file_list(files, [])
4✔
125
      @work.save!
3✔
126
      @work.reload_snapshots
3✔
127
    end
128
    if params[:save_only] == "true"
5✔
129
      render :file_upload
1✔
130
    else
131
      redirect_to(work_review_path)
4✔
132
    end
133
  rescue StandardError => active_storage_error
134
    Rails.logger.error("Failed to attach the file uploads for the work #{@work.doi}: #{active_storage_error}")
1✔
135
    flash[:notice] = "Failed to attach the file uploads for the work #{@work.doi}: #{active_storage_error}. Please contact rdss@princeton.edu for assistance."
1✔
136

137
    redirect_to work_file_upload_path(@work)
1✔
138
  end
139

140
  # Allow user to indicate where their files are located in the WWW
141
  # GET /works/1/file_other
142
  def file_other; end
1✔
143

144
  # GET /works/1/review
145
  # POST /works/1/review
146
  def review
1✔
147
    if request.method == "POST"
7✔
148
      @work.location_notes = params["location_notes"]
5✔
149
      @work.save!
5✔
150
      if params[:save_only] == "true"
5✔
151
        render :file_other
1✔
152
      end
153
    end
154
  end
155

156
  # Validates that the work is ready to be approved
157
  # GET /works/1/validate
158
  def validate
1✔
159
    @work.submission_notes = params["submission_notes"]
31✔
160
    @work.complete_submission!(current_user)
31✔
161
    if params[:save_only] == "true"
30✔
162
      render :review
1✔
163
    else
164
      redirect_to user_url(current_user)
29✔
165
    end
166
  end
167

168
  # Show the user the form to select a readme
169
  # GET /works/1/readme_select
170
  def readme_select
1✔
171
    readme = Readme.new(@work, current_user)
9✔
172
    @readme = readme.file_name
9✔
173
    @wizard = true
9✔
174
  end
175

176
  # Uploads the readme the user selects
177
  # GET /works/1/readme_uploaded
178
  def readme_uploaded
1✔
179
    @wizard = true
9✔
180
    readme = Readme.new(@work, current_user)
9✔
181
    readme_error = readme.attach(readme_file_param)
9✔
182
    if readme_error.nil?
9✔
183
      if params[:save_only] == "true"
8✔
184
        readme = Readme.new(@work, current_user)
1✔
185
        @readme = readme.file_name
1✔
186
        render :readme_select
1✔
187
      else
188
        redirect_to work_attachment_select_url(@work)
7✔
189
      end
190
    else
191
      flash[:notice] = readme_error
1✔
192
      redirect_to work_readme_select_url(@work)
1✔
193
    end
194
  end
195

196
  private
1✔
197

198
    def load_work
1✔
199
      @work = Work.find(params[:id])
101✔
200
    end
201

202
    def patch_params
1✔
203
      return {} unless params.key?(:patch)
30✔
204

205
      params[:patch]
24✔
206
    end
207

208
    def pre_curation_uploads_param
1✔
209
      return if patch_params.nil?
6✔
210

211
      patch_params[:pre_curation_uploads]
6✔
212
    end
213

214
    def readme_file_param
1✔
215
      return if patch_params.nil?
9✔
216

217
      patch_params[:readme_file]
9✔
218
    end
219

220
    def rescue_aasm_error
1✔
221
      yield
41✔
222
    rescue AASM::InvalidTransition => error
223
      message = message_from_assm_error(aasm_error: error, work: @work)
2✔
224

225
      Honeybadger.notify("Invalid #{@work.current_transition}: #{error.message} errors: #{message}")
2✔
226
      transition_error_message = "We apologize, the following errors were encountered: #{message}. Please contact the PDC Describe administrators for any assistance."
2✔
227
      @errors = [transition_error_message]
2✔
228

229
      if @work.persisted?
2✔
230
        redirect_to edit_work_wizard_path(id: @work.id), notice: transition_error_message, params:
1✔
231
      else
232
        @form_resource_decorator = FormResourceDecorator.new(@work, current_user)
1✔
233
        redirect_to work_create_new_submission_path, notice: transition_error_message, params:
1✔
234
      end
235
    rescue StandardError => generic_error
UNCOV
236
      redirect_to root_url, notice: "We apologize, an error was encountered: #{generic_error.message}. Please contact the PDC Describe administrators."
×
237
    end
238
end
239
# 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