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

pulibrary / pdc_describe / 48f85909-ee15-4aa8-b584-b9fb90047076

28 Mar 2024 12:39PM UTC coverage: 95.907% (+0.02%) from 95.886%
48f85909-ee15-4aa8-b584-b9fb90047076

Pull #1721

circleci

carolyncole
Moving decorator creation to application controller so wizard and work form can share
Pull Request #1721: Added save_only parameter processing to update_wizard

41 of 41 new or added lines in 4 files covered. (100.0%)

1 existing line in 1 file now uncovered.

3210 of 3347 relevant lines covered (95.91%)

204.81 hits per line

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

99.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 -> 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
    prepare_decorators_for_work_form(@work)
13✔
28
  end
29

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

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

53
      prepare_decorators_for_work_form(@work)
8✔
54
    end
55
  end
56

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

68
        if params[:save_only] == "true"
7✔
69
          render :edit_wizard
1✔
70
        else
71
          redirect_to work_readme_select_url(@work)
6✔
72
        end
73
      else
74
        render :edit_wizard, status: :unprocessable_entity
1✔
75
      end
76
    end
77
  end
78

79
  # Prompt to select how to submit their files
80
  # GET /works/1/attachment_select
81
  def attachment_select
1✔
82
    @wizard_mode = true
7✔
83
  end
84

85
  # User selected a specific way to submit their files
86
  # POST /works/1/attachment_selected
87
  def attachment_selected
1✔
88
    @wizard_mode = true
8✔
89
    @work.files_location = params["attachment_type"]
8✔
90
    @work.save!
8✔
91

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

95
    if params[:save_only] == "true"
8✔
96
      render :attachment_select
2✔
97
    else
98

99
      next_url = case @work.files_location
6✔
100
                 when "file_upload"
101
                   work_file_upload_url(@work)
2✔
102
                 else
103
                   work_file_other_url(@work)
4✔
104
                 end
105
      redirect_to next_url
6✔
106
    end
107
  end
108

109
  # Allow user to upload files directly
110
  # GET /works/1/file_upload
111
  def file_upload; end
1✔
112

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

131
    redirect_to work_file_upload_path(@work)
1✔
132
  end
133

134
  # Allow user to indicate where their files are located in the WWW
135
  # GET /works/1/file_other
136
  def file_other; end
1✔
137

138
  # GET /works/1/review
139
  # POST /works/1/review
140
  def review
1✔
141
    if request.method == "POST"
7✔
142
      @work.location_notes = params["location_notes"]
5✔
143
      @work.save!
5✔
144
      if params[:save_only] == "true"
5✔
145
        render :file_other
1✔
146
      end
147
    end
148
  end
149

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

162
  # Show the user the form to select a readme
163
  # GET /works/1/readme_select
164
  def readme_select
1✔
165
    readme = Readme.new(@work, current_user)
9✔
166
    @readme = readme.file_name
9✔
167
    @wizard = true
9✔
168
  end
169

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

189
  private
1✔
190

191
    def load_work
1✔
192
      @work = Work.find(params[:id])
101✔
193
    end
194

195
    def patch_params
1✔
196
      return {} unless params.key?(:patch)
30✔
197

198
      params[:patch]
24✔
199
    end
200

201
    def pre_curation_uploads_param
1✔
202
      return if patch_params.nil?
6✔
203

204
      patch_params[:pre_curation_uploads]
6✔
205
    end
206

207
    def readme_file_param
1✔
208
      return if patch_params.nil?
9✔
209

210
      patch_params[:readme_file]
9✔
211
    end
212

213
    def rescue_aasm_error
1✔
214
      yield
41✔
215
    rescue AASM::InvalidTransition => error
216
      message = message_from_assm_error(aasm_error: error, work: @work)
2✔
217

218
      Honeybadger.notify("Invalid #{@work.current_transition}: #{error.message} errors: #{message}")
2✔
219
      transition_error_message = "We apologize, the following errors were encountered: #{message}. Please contact the PDC Describe administrators for any assistance."
2✔
220
      @errors = [transition_error_message]
2✔
221
      prepare_decorators_for_work_form(@work)
2✔
222

223
      if @work.persisted?
2✔
224
        redirect_to edit_work_wizard_path(id: @work.id), notice: transition_error_message, params:
1✔
225
      else
226
        redirect_to work_create_new_submission_path, notice: transition_error_message, params:
1✔
227
      end
228
    rescue StandardError => generic_error
UNCOV
229
      redirect_to root_url, notice: "We apologize, an error was encountered: #{generic_error.message}. Please contact the PDC Describe administrators."
×
230
    end
231
end
232
# 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