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

pulibrary / pdc_describe / 704e072a-60a4-43a6-a47a-6fa4f15ea520

03 Apr 2024 03:22PM UTC coverage: 95.821% (-0.05%) from 95.875%
704e072a-60a4-43a6-a47a-6fa4f15ea520

Pull #1729

circleci

carolyncole
Updating tests to click on the new button text
Also completed the other file path test
Pull Request #1729: Adding Save, Next and Previous buttons to the wizard form

8 of 9 new or added lines in 2 files covered. (88.89%)

1 existing line in 1 file now uncovered.

3233 of 3374 relevant lines covered (95.82%)

206.0 hits per line

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

97.06
/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
    @work = WorkMetadataService.new(params:, current_user:).work_for_new_submission
20✔
25
    prepare_decorators_for_work_form(@work)
20✔
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
18✔
32
    @errors = @work.errors.to_a
15✔
33
    if params[:save_only] == "true" || @errors.count.positive?
15✔
34
      prepare_decorators_for_work_form(@work)
3✔
35
      render :new_submission
3✔
36
    else
37
      redirect_to edit_work_wizard_path(@work)
12✔
38
    end
39
  end
40

41
  # GET /works/1/edit-wizard
42
  def edit_wizard
1✔
43
    @wizard_mode = true
15✔
44
    if validate_modification_permissions(work: @work,
15✔
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)
15✔
49
    end
50
  end
51

52
  # PATCH /works/1/update-wizard
53
  def update_wizard
1✔
54
    if validate_modification_permissions(work: @work,
6✔
55
                                         uneditable_message: "Can not update work: #{@work.id} is not editable by #{current_user.uid}",
56
                                         current_state_message: "Can not update work: #{@work.id} is not editable in current state by #{current_user.uid}")
57
      prepare_decorators_for_work_form(@work)
6✔
58
      if WorkCompareService.update_work(work: @work, update_params:, current_user:)
6✔
59
        if params[:save_only] == "true"
6✔
60
          render :edit_wizard
1✔
61
        else
62
          redirect_to work_readme_select_url(@work)
5✔
63
        end
64
      else
UNCOV
65
        render :edit_wizard, status: :unprocessable_entity
×
66
      end
67
    end
68
  end
69

70
  # Prompt to select how to submit their files
71
  # GET /works/1/attachment_select
72
  def attachment_select; end
1✔
73

74
  # User selected a specific way to submit their files
75
  # POST /works/1/attachment_selected
76
  def attachment_selected
1✔
77
    @work.files_location = params["attachment_type"]
11✔
78
    @work.save!
11✔
79

80
    # create a directory for the work if the curator will need to move files by hand
81
    @work.s3_query_service.create_directory if @work.files_location != "file_upload"
11✔
82

83
    if params[:save_only] == "true"
11✔
84
      render :attachment_select
3✔
85
    else
86
      redirect_to file_location_url
8✔
87
    end
88
  end
89

90
  # Allow user to upload files directly
91
  # GET /works/1/file_upload
92
  def file_upload; end
1✔
93

94
  # POST /works/1/file_upload
95
  def file_uploaded
1✔
96
    files = pre_curation_uploads_param || []
7✔
97
    if files.count > 0
7✔
98
      upload_service = WorkUploadsEditService.new(@work, current_user)
4✔
99
      @work = upload_service.update_precurated_file_list(files, [])
4✔
100
      @work.reload_snapshots
3✔
101
    end
102
    if params[:save_only] == "true"
6✔
103
      render :file_upload
1✔
104
    else
105
      redirect_to(work_review_path)
5✔
106
    end
107
  rescue StandardError => active_storage_error
108
    Rails.logger.error("Failed to attach the file uploads for the work #{@work.doi}: #{active_storage_error}")
1✔
109
    flash[:notice] = "Failed to attach the file uploads for the work #{@work.doi}: #{active_storage_error}. Please contact rdss@princeton.edu for assistance."
1✔
110

111
    redirect_to work_file_upload_path(@work)
1✔
112
  end
113

114
  # Allow user to indicate where their files are located in the WWW
115
  # GET /works/1/file_other
116
  def file_other; end
1✔
117

118
  # GET /works/1/review
119
  # POST /works/1/review
120
  def review
1✔
121
    if request.method == "POST" || request.method == "PATCH"
10✔
122
      @work.location_notes = params["location_notes"]
6✔
123
      @work.save!
6✔
124
      if params[:save_only] == "true"
6✔
125
        render :file_other
2✔
126
      end
127
    end
128
  end
129

130
  # Validates that the work is ready to be approved
131
  # GET /works/1/validate
132
  def validate
1✔
133
    @work.submission_notes = params["submission_notes"]
30✔
134
    if params[:save_only] == "true"
30✔
135
      @work.save
1✔
136
      render :review
1✔
137
    else
138
      @work.complete_submission!(current_user)
29✔
139
      redirect_to user_url(current_user)
28✔
140
    end
141
  end
142

143
  # Show the user the form to select a readme
144
  # GET /works/1/readme_select
145
  def readme_select
1✔
146
    readme = Readme.new(@work, current_user)
8✔
147
    @readme = readme.file_name
8✔
148
  end
149

150
  # Uploads the readme the user selects
151
  # GET /works/1/readme_uploaded
152
  def readme_uploaded
1✔
153
    readme = Readme.new(@work, current_user)
8✔
154
    readme_error = readme.attach(readme_file_param)
8✔
155
    if readme_error.nil?
8✔
156
      if params[:save_only] == "true"
7✔
157
        @readme = readme.file_name
1✔
158
        render :readme_select
1✔
159
      else
160
        redirect_to work_attachment_select_url(@work)
6✔
161
      end
162
    else
163
      flash[:notice] = readme_error
1✔
164
      redirect_to work_readme_select_url(@work)
1✔
165
    end
166
  end
167

168
  def file_location_url
1✔
169
    WorkMetadataService.file_location_url(@work)
15✔
170
  end
171
  helper_method :file_location_url
1✔
172

173
  private
1✔
174

175
    def load_work
1✔
176
      @work = Work.find(params[:id])
115✔
177
    end
178

179
    def patch_params
1✔
180
      return {} unless params.key?(:patch)
30✔
181

182
      params[:patch]
22✔
183
    end
184

185
    def pre_curation_uploads_param
1✔
186
      return if patch_params.nil?
7✔
187

188
      patch_params[:pre_curation_uploads]
7✔
189
    end
190

191
    def readme_file_param
1✔
192
      return if patch_params.nil?
8✔
193

194
      patch_params[:readme_file]
8✔
195
    end
196

197
    def rescue_aasm_error
1✔
198
      yield
48✔
199
    rescue AASM::InvalidTransition => error
200
      message = message_from_assm_error(aasm_error: error, work: @work)
1✔
201

202
      Honeybadger.notify("Invalid #{@work.current_transition}: #{error.message} errors: #{message}")
1✔
203
      transition_error_message = "We apologize, the following errors were encountered: #{message}. Please contact the PDC Describe administrators for any assistance."
1✔
204
      @errors = [transition_error_message]
1✔
205
      prepare_decorators_for_work_form(@work)
1✔
206

207
      if @work.persisted?
1✔
208
        redirect_to edit_work_wizard_path(id: @work.id), notice: transition_error_message, params:
1✔
209
      else
210
        redirect_to work_create_new_submission_path, notice: transition_error_message, params:
×
211
      end
212
    rescue StandardError => generic_error
213
      redirect_to root_url, notice: "We apologize, an error was encountered: #{generic_error.message}. Please contact the PDC Describe administrators."
×
214
    end
215
end
216
# 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