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

pulibrary / pdc_describe / c0e87805-f6a1-49a6-bff2-ecd1f2371edf

26 Mar 2024 05:45PM UTC coverage: 95.523% (-0.6%) from 96.167%
c0e87805-f6a1-49a6-bff2-ecd1f2371edf

Pull #1715

circleci

carolyncole
Split out the work wizard into its own controller
refs #1684
Pull Request #1715: Split out the work wizard into its own controller

133 of 143 new or added lines in 3 files covered. (93.01%)

16 existing lines in 1 file now uncovered.

3222 of 3373 relevant lines covered (95.52%)

203.36 hits per line

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

95.24
/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
# rubocop:disable Metrics/ClassLength
14
class WorksWizardController < ApplicationController
1✔
15
  include ERB::Util
1✔
16
  around_action :rescue_aasm_error, only: [:validate, :new_submission_save]
1✔
17

18
  # get Renders the "step 0" information page before creating a new dataset
19
  # only wizard mode
20
  def new_submission
1✔
21
    group = Group.find_by(code: params[:group_code]) || current_user.default_group
13✔
22
    group_id = group.id
13✔
23
    @work = Work.new(created_by_user_id: current_user.id, group_id:)
13✔
24
    @work_decorator = WorkDecorator.new(@work, current_user)
13✔
25
    @form_resource_decorator = FormResourceDecorator.new(@work, current_user)
13✔
26
  end
27

28
  # Creates the new dataset
29
  # only wizard mode
30
  def new_submission_save
1✔
31
    group = Group.find_by(code: params[:group_code]) || current_user.default_group
9✔
32
    group_id = group.id
9✔
33
    @work = Work.new(created_by_user_id: current_user.id, group_id:)
9✔
34
    @work.resource = FormToResourceService.convert(params, @work)
9✔
35
    @work.draft!(current_user)
9✔
36
    redirect_to edit_work_wizard_path(@work)
8✔
37
  end
38

39
  # GET /works/1/edit_wizard
40
  # only wizard
41
  def edit_wizard
1✔
42
    @work = Work.find(params[:id])
8✔
43
    @work_decorator = WorkDecorator.new(@work, current_user)
8✔
44
    @wizard_mode = true
8✔
45
    if validate_modification_permissions(work: @work,
8✔
46
                                         uneditable_message: "Can not edit work: #{@work.id} is not editable by #{current_user.uid}",
47
                                         current_state_message: "Can not edit work: #{@work.id} is not editable in current state by #{current_user.uid}")
48

49
      @form_resource_decorator = FormResourceDecorator.new(@work, current_user)
8✔
50
    end
51
  end
52

53
  # PATCH /works/1/update-wizard
54
  # only wizard  mode
55
  def update_wizard
1✔
56
    @work = Work.find(params[:id])
7✔
57
    if validate_modification_permissions(work: @work,
7✔
58
                                         uneditable_message: "Can not update work: #{@work.id} is not editable by #{current_user.uid}",
59
                                         current_state_message: "Can not update work: #{@work.id} is not editable in current state by #{current_user.uid}")
60
      work_before = @work.dup
7✔
61
      if @work.update(update_params)
7✔
62
        work_compare = WorkCompareService.new(work_before, @work)
6✔
63
        @work.log_changes(work_compare, current_user.id)
6✔
64

65
        redirect_to work_readme_select_url(@work)
6✔
66
      else
67
        # This is needed for rendering HTML views with validation errors
68
        @form_resource_decorator = FormResourceDecorator.new(@work, current_user)
1✔
69

70
        render :edit_wizard, status: :unprocessable_entity
1✔
71
      end
72
    end
73
  end
74

75
  # Prompt to select how to submit their files
76
  # only wizard mode
77
  def attachment_select
1✔
78
    @work = Work.find(params[:id])
7✔
79
    @wizard_mode = true
7✔
80
  end
81

82
  # User selected a specific way to submit their files
83
  # only wizard mode
84
  def attachment_selected
1✔
85
    @work = Work.find(params[:id])
6✔
86
    @wizard_mode = true
6✔
87
    @work.files_location = params["attachment_type"]
6✔
88
    @work.save!
6✔
89

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

93
    next_url = case @work.files_location
6✔
94
               when "file_upload"
95
                 work_file_upload_url(@work)
2✔
96
               else
97
                 work_file_other_url(@work)
4✔
98
               end
99
    redirect_to next_url
6✔
100
  end
101

102
  # Allow user to upload files directly
103
  def file_upload
1✔
104
    @work = Work.find(params[:id])
4✔
105
  end
106

107
  def file_uploaded
1✔
108
    @work = Work.find(params[:id])
5✔
109
    files = pre_curation_uploads_param || []
5✔
110
    if files.count > 0
5✔
111
      upload_service = WorkUploadsEditService.new(@work, current_user)
3✔
112
      @work = upload_service.update_precurated_file_list(files, [])
3✔
113
      @work.save!
2✔
114
      @work.reload_snapshots
2✔
115
    end
116
    redirect_to(work_review_path)
4✔
117
  rescue StandardError => active_storage_error
118
    Rails.logger.error("Failed to attach the file uploads for the work #{@work.doi}: #{active_storage_error}")
1✔
119
    flash[:notice] = "Failed to attach the file uploads for the work #{@work.doi}: #{active_storage_error}. Please contact rdss@princeton.edu for assistance."
1✔
120

121
    redirect_to work_file_upload_path(@work)
1✔
122
  end
123

124
  # Allow user to indicate where their files are located in the WWW
125
  # only wizard mode
126
  def file_other
1✔
127
    @work = Work.find(params[:id])
4✔
128
  end
129

130
  # only wizard mode
131
  def review
1✔
132
    @work = Work.find(params[:id])
6✔
133
    if request.method == "POST"
6✔
134
      @work.location_notes = params["location_notes"]
4✔
135
      @work.save!
4✔
136
    end
137
  end
138

139
  # only wizard mode
140
  def validate
1✔
141
    @work = Work.find(params[:id])
30✔
142
    @work.submission_notes = params["submission_notes"]
30✔
143
    @uploads = @work.uploads
30✔
144
    @wizard_mode = true
30✔
145
    @work.complete_submission!(current_user)
30✔
146
    redirect_to user_url(current_user)
29✔
147
  end
148

149
  # only wizard mode
150
  def readme_select
1✔
151
    @work = Work.find(params[:id])
9✔
152
    readme = Readme.new(@work, current_user)
9✔
153
    @readme = readme.file_name
9✔
154
    @wizard = true
9✔
155
  end
156

157
  # only wizard mode
158
  def readme_uploaded
1✔
159
    @work = Work.find(params[:id])
8✔
160
    @wizard = true
8✔
161
    readme = Readme.new(@work, current_user)
8✔
162
    readme_error = readme.attach(readme_file_param)
8✔
163
    if readme_error.nil?
8✔
164
      redirect_to work_attachment_select_url(@work)
7✔
165
    else
166
      flash[:notice] = readme_error
1✔
167
      redirect_to work_readme_select_url(@work)
1✔
168
    end
169
  end
170

171
  private
1✔
172

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

176
      params[:patch]
20✔
177
    end
178

179
    def pre_curation_uploads_param
1✔
180
      return if patch_params.nil?
5✔
181

182
      patch_params[:pre_curation_uploads]
5✔
183
    end
184

185
    def readme_file_param
1✔
186
      return if patch_params.nil?
8✔
187

188
      patch_params[:readme_file]
8✔
189
    end
190

191
    def rescue_aasm_error
1✔
192
      yield
39✔
193
    rescue AASM::InvalidTransition => error
194
      message = message_from_assm_error(aasm_error: error, work: @work)
2✔
195

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

200
      if @work.persisted?
2✔
201
        redirect_to edit_work_wizard_path(id: @work.id), notice: transition_error_message, params:
1✔
202
      else
203
        @form_resource_decorator = FormResourceDecorator.new(@work, current_user)
1✔
204
        redirect_to work_create_new_submission_path, notice: transition_error_message, params:
1✔
205
      end
206
    rescue StandardError => generic_error
NEW
207
      redirect_to root_url, notice: "We apologize, an error was encountered: #{generic_error.message}. Please contact the PDC Describe administrators."
×
208
    end
209

210
    def embargo_date_param
1✔
211
      params["embargo-date"]
7✔
212
    end
213

214
    def embargo_date
1✔
215
      return nil if embargo_date_param.blank?
7✔
216

NEW
217
      Date.parse(embargo_date_param)
×
218
    rescue Date::Error
NEW
219
      Rails.logger.error("Failed to parse the embargo date #{embargo_date_param} for Work #{@work.id}")
×
NEW
220
      nil
×
221
    end
222

223
    def update_params
1✔
224
      {
225
        group_id: params_group_id,
7✔
226
        embargo_date:,
227
        resource: FormToResourceService.convert(params, @work)
228
      }
229
    end
230

231
    def params_group_id
1✔
232
      # Do not allow a nil for the group id
233
      @params_group_id ||= begin
7✔
234
        group_id = params[:group_id]
7✔
235
        if group_id.blank?
7✔
NEW
236
          group_id = current_user.default_group.id
×
NEW
237
          Honeybadger.notify("We got a nil group as part of the parameters #{params} #{request}")
×
238
        end
239
        group_id
7✔
240
      end
241
    end
242
end
243
# 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