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

pulibrary / pdc_describe / aa4d17ae-5b88-4b80-9b41-f98c7bc03ef4

pending completion
aa4d17ae-5b88-4b80-9b41-f98c7bc03ef4

Pull #1060

circleci

jrgriffiniii
wip
Pull Request #1060: [wip] Integrating Support for Upload Snapshots

152 of 152 new or added lines in 7 files covered. (100.0%)

2016 of 2058 relevant lines covered (97.96%)

171.45 hits per line

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

99.58
/app/controllers/works_controller.rb
1
# frozen_string_literal: true
2

3
require "nokogiri"
1✔
4
require "open-uri"
1✔
5

6
# rubocop:disable Metrics/ClassLength
7
class WorksController < ApplicationController
1✔
8
  include ERB::Util
1✔
9
  around_action :rescue_aasm_error, only: [:approve, :withdraw, :resubmit, :validate, :create]
1✔
10

11
  skip_before_action :authenticate_user!
1✔
12
  before_action :authenticate_user!, unless: :public_request?
1✔
13

14
  ##
15
  # Public requests are requests that do not require authentication.
16
  # This is to enable PDC Discovery to index approved content via the RSS feed and
17
  # .json calls to individual works without needing to log in as a user.
18
  # Note that only approved works can be fetched for indexing.
19
  def public_request?
1✔
20
    return true if action_name == "index" && request.format.symbol == :rss
460✔
21
    return true if action_name == "show" && request.format.symbol == :json && Work.find(params[:id]).state == "approved"
458✔
22
    false
457✔
23
  end
24

25
  def index
1✔
26
    @works = Work.all
4✔
27
    respond_to do |format|
4✔
28
      format.html
4✔
29
      format.rss { render layout: false }
6✔
30
    end
31
  end
32

33
  # Renders the "step 0" information page before creating a new dataset
34
  def new
1✔
35
    if wizard_mode?
25✔
36
      render "new_submission"
8✔
37
    else
38
      @work = Work.new(created_by_user_id: current_user.id, collection: current_user.default_collection)
17✔
39
    end
40
  end
41

42
  def create
1✔
43
    # TODO: We need to process files submitted by the user in this method.
44
    # We currently do not and therefore there are not saved to the work.
45
    # See https://github.com/pulibrary/pdc_describe/issues/1041
46
    @work = Work.new(created_by_user_id: current_user.id, collection_id: params_collection_id, user_entered_doi: params["doi"].present?)
20✔
47
    @work.resource = FormToResourceService.convert(params, @work)
20✔
48
    if @work.valid?
20✔
49
      @work.draft!(current_user)
18✔
50
      redirect_to work_url(@work), notice: "Work was successfully created."
18✔
51
    else
52
      render :new, status: :unprocessable_entity
2✔
53
    end
54
  end
55

56
  # Creates the new dataset
57
  def new_submission
1✔
58
    default_collection_id = current_user.default_collection.id
6✔
59
    work = Work.new(created_by_user_id: current_user.id, collection_id: default_collection_id)
6✔
60
    work.resource = FormToResourceService.convert(params, work)
6✔
61
    work.draft!(current_user)
6✔
62
    redirect_to edit_work_path(work, wizard: true)
6✔
63
  end
64

65
  ##
66
  # Show the information for the dataset with the given id
67
  # When requested as .json, return the internal json resource
68
  def show
1✔
69
    @work = Work.find(params[:id])
83✔
70
    @work.reload_snapshots
83✔
71
    @changes = WorkActivity.changes_for_work(@work.id)
82✔
72
    @messages = WorkActivity.messages_for_work(@work.id)
82✔
73

74
    respond_to do |format|
82✔
75
      format.html do
82✔
76
        # Ensure that the Work belongs to a Collection
77
        @collection = @work.collection
80✔
78
        raise(Work::InvalidCollectionError, "The Work #{@work.id} does not belong to any Collection") unless @collection
80✔
79

80
        @can_curate = current_user.can_admin?(@collection)
80✔
81
        @work.mark_new_notifications_as_read(current_user.id)
80✔
82
      end
83
      format.json { render json: @work.to_json }
84✔
84
    end
85
  end
86

87
  def file_list
1✔
88
    if params[:id] == "NONE"
117✔
89
      # This is a special case when we render the file list for a work being created
90
      # (i.e. it does not have an id just yet)
91
      render json: []
18✔
92
    else
93
      @work = Work.find(params[:id])
99✔
94
      render json: @work.uploads
99✔
95
    end
96
  end
97

98
  def resolve_doi
1✔
99
    @work = Work.find_by_doi(params[:doi])
3✔
100
    redirect_to @work
2✔
101
  end
102

103
  def resolve_ark
1✔
104
    @work = Work.find_by_ark(params[:ark])
3✔
105
    redirect_to @work
2✔
106
  end
107

108
  # GET /works/1/edit
109
  def edit
1✔
110
    @work = Work.find(params[:id])
43✔
111
    if current_user && @work.editable_by?(current_user)
43✔
112
      if @work.approved? && !@work.administered_by?(current_user)
40✔
113
        Honeybadger.notify("Can not edit work: #{@work.id} is approved but #{current_user} is not admin")
1✔
114
        redirect_to root_path, notice: I18n.t("works.uneditable.approved")
1✔
115
      else
116
        @uploads = @work.uploads
39✔
117
        @wizard_mode = wizard_mode?
39✔
118
        render "edit"
39✔
119
      end
120
    else
121
      Honeybadger.notify("Can not edit work: #{@work.id} is not editable by #{current_user}")
3✔
122
      redirect_to root_path, notice: I18n.t("works.uneditable.privs")
3✔
123
    end
124
  end
125

126
  def update
1✔
127
    @work = Work.find(params[:id])
47✔
128
    if current_user.blank? || !@work.editable_by?(current_user)
47✔
129
      Honeybadger.notify("Can not update work: #{@work.id} is not editable by #{current_user}")
1✔
130
      redirect_to root_path, notice: I18n.t("works.uneditable.privs")
1✔
131
    elsif !@work.editable_in_current_state?(current_user)
46✔
132
      Honeybadger.notify("Can not update work: #{@work.id} is not editable in current state by #{current_user}")
1✔
133
      redirect_to root_path, notice: I18n.t("works.uneditable.approved")
1✔
134
    else
135
      update_work
45✔
136
    end
137
  end
138

139
  # Prompt to select how to submit their files
140
  def attachment_select
1✔
141
    @work = Work.find(params[:id])
7✔
142
    @wizard_mode = true
7✔
143
  end
144

145
  # User selected a specific way to submit their files
146
  def attachment_selected
1✔
147
    @work = Work.find(params[:id])
8✔
148
    @wizard_mode = true
8✔
149
    @work.files_location = params["attachment_type"]
8✔
150
    @work.save!
8✔
151

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

155
    next_url = case @work.files_location
8✔
156
               when "file_upload"
157
                 work_file_upload_url(@work)
2✔
158
               when "file_cluster"
159
                 work_file_cluster_url(@work)
1✔
160
               else
161
                 work_file_other_url(@work)
5✔
162
               end
163
    redirect_to next_url
8✔
164
  end
165

166
  # Allow user to upload files directly
167
  def file_upload
1✔
168
    @work = Work.find(params[:id])
3✔
169
  end
170

171
  def file_uploaded
1✔
172
    @work = Work.find(params[:id])
4✔
173
    if pre_curation_uploads_param
4✔
174
      @work.pre_curation_uploads.attach(pre_curation_uploads_param)
2✔
175
      @work.save!
1✔
176
    end
177

178
    redirect_to(work_review_path)
3✔
179
  rescue StandardError => active_storage_error
180
    Rails.logger.error("Failed to attach the file uploads for the work #{@work.doi}: #{active_storage_error}")
1✔
181
    flash[:notice] = "Failed to attach the file uploads for the work #{@work.doi}: #{active_storage_error}. Please contact rdss@princeton.edu for assistance."
1✔
182

183
    redirect_to work_file_upload_path(@work)
1✔
184
  end
185

186
  # Allow user to indicate where their files are located in the PUL Research Cluster
187
  def file_cluster
1✔
188
    @work = Work.find(params[:id])
1✔
189
  end
190

191
  # Allow user to indicate where their files are located in the WWW
192
  def file_other
1✔
193
    @work = Work.find(params[:id])
5✔
194
  end
195

196
  def review
1✔
197
    @work = Work.find(params[:id])
6✔
198
    if request.method == "POST"
6✔
199
      @work.location_notes = params["location_notes"]
5✔
200
      @work.save!
5✔
201
    end
202
  end
203

204
  def validate
1✔
205
    @work = Work.find(params[:id])
23✔
206
    @work.submission_notes = params["submission_notes"]
23✔
207
    @uploads = @work.uploads
23✔
208
    @wizard_mode = true
23✔
209
    @work.complete_submission!(current_user)
23✔
210
    redirect_to user_url(current_user)
21✔
211
  end
212

213
  def approve
1✔
214
    @work = Work.find(params[:id])
10✔
215
    @work.approve!(current_user)
10✔
216
    redirect_to work_path(@work)
6✔
217
  end
218

219
  def withdraw
1✔
220
    @work = Work.find(params[:id])
2✔
221
    @work.withdraw!(current_user)
2✔
222
    redirect_to work_path(@work)
1✔
223
  end
224

225
  def resubmit
1✔
226
    @work = Work.find(params[:id])
2✔
227
    @work.resubmit!(current_user)
2✔
228
    redirect_to work_path(@work)
1✔
229
  end
230

231
  def assign_curator
1✔
232
    work = Work.find(params[:id])
4✔
233
    work.change_curator(params[:uid], current_user)
4✔
234
    if work.errors.count > 0
3✔
235
      render json: { errors: work.errors.map(&:type) }, status: :bad_request
1✔
236
    else
237
      render json: {}
2✔
238
    end
239
  rescue => ex
240
    Rails.logger.error("Error changing curator for work: #{work.id}. Exception: #{ex.message}")
1✔
241
    render json: { errors: ["Cannot save dataset"] }, status: :bad_request
1✔
242
  end
243

244
  def add_message
1✔
245
    work = Work.find(params[:id])
6✔
246
    if params["new-message"].present?
6✔
247
      new_message_param = params["new-message"]
6✔
248
      sanitized_new_message = html_escape(new_message_param)
6✔
249

250
      work.add_message(sanitized_new_message, current_user.id)
6✔
251
    end
252
    redirect_to work_path(id: params[:id])
6✔
253
  end
254

255
  def add_provenance_note
1✔
256
    work = Work.find(params[:id])
1✔
257
    if params["new-provenance-note"].present?
1✔
258
      new_date = params["new-provenance-date"]
1✔
259
      new_note = html_escape(params["new-provenance-note"])
1✔
260

261
      work.add_provenance_note(new_date, new_note, current_user.id)
1✔
262
    end
263
    redirect_to work_path(id: params[:id])
1✔
264
  end
265

266
  # Outputs the Datacite XML representation of the work
267
  def datacite
1✔
268
    work = Work.find(params[:id])
2✔
269
    render xml: work.to_xml
2✔
270
  end
271

272
  def datacite_validate
1✔
273
    @errors = []
1✔
274
    @work = Work.find(params[:id])
1✔
275
    datacite_xml = Nokogiri::XML(@work.to_xml)
1✔
276
    schema_location = Rails.root.join("config", "schema")
1✔
277
    Dir.chdir(schema_location) do
1✔
278
      xsd = Nokogiri::XML::Schema(File.read("datacite_4_4.xsd"))
1✔
279
      xsd.validate(datacite_xml).each do |error|
1✔
280
        @errors << error
1✔
281
      end
282
    end
283
  end
284

285
  def readme_select
1✔
286
    @work = Work.find(params[:id])
10✔
287
    @wizard = true
10✔
288
  end
289

290
  def readme_uploaded
1✔
291
    @work = Work.find(params[:id])
10✔
292
    @wizard = true
10✔
293
    readme = Readme.new(@work)
10✔
294
    readme_error = readme.attach(readme_file_param)
10✔
295
    if readme_error.nil?
10✔
296
      redirect_to work_attachment_select_url(@work)
7✔
297
    else
298
      flash[:notice] = readme_error
3✔
299
      redirect_to work_readme_select_url(@work)
3✔
300
    end
301
  end
302

303
  private
1✔
304

305
    def work_params
1✔
306
      params[:work] || params
90✔
307
    end
308

309
    def patch_params
1✔
310
      return {} unless params.key?(:patch)
30✔
311

312
      params[:patch]
18✔
313
    end
314

315
    def pre_curation_uploads_param
1✔
316
      return if patch_params.nil?
5✔
317

318
      patch_params[:pre_curation_uploads_added]
5✔
319
    end
320

321
    def readme_file_param
1✔
322
      return if patch_params.nil?
10✔
323

324
      patch_params[:readme_file]
10✔
325
    end
326

327
    def rescue_aasm_error
1✔
328
      yield
57✔
329
    rescue AASM::InvalidTransition => error
330
      message = error.message
8✔
331
      if @work.errors.count > 0
8✔
332
        message = @work.errors.to_a.join(", ")
5✔
333
      end
334
      Honeybadger.notify("Invalid #{@work.current_transition}: #{error.message} errors: #{message}")
8✔
335
      @errors = ["Cannot #{@work.current_transition}: #{message}"]
8✔
336
      render error_action, status: :unprocessable_entity
8✔
337
    end
338

339
    def error_action
1✔
340
      if action_name == "create"
8✔
341
        :new
×
342
      elsif action_name == "validate"
8✔
343
        :edit
2✔
344
      else
345
        :show
6✔
346
      end
347
    end
348

349
    def wizard_mode?
1✔
350
      params[:wizard] == "true"
109✔
351
    end
352

353
    def update_work
1✔
354
      @wizard_mode = wizard_mode?
45✔
355
      upload_service = WorkUploadsEditService.new(@work, current_user)
45✔
356
      if @work.approved?
45✔
357
        upload_keys = work_params[:deleted_uploads] || []
5✔
358
        deleted_uploads = upload_service.find_post_curation_uploads(upload_keys: upload_keys)
5✔
359

360
        return head(:forbidden) unless deleted_uploads.empty?
5✔
361
      else
362
        @work = upload_service.update_precurated_file_list(added_files_param, deleted_files_param)
40✔
363
      end
364

365
      process_updates
44✔
366
    end
367

368
    def update_params
1✔
369
      {
370
        collection_id: params_collection_id,
86✔
371
        resource: FormToResourceService.convert(params, @work)
372
      }
373
    end
374

375
    def added_files_param
1✔
376
      Array(work_params[:pre_curation_uploads_added])
40✔
377
    end
378

379
    def deleted_files_param
1✔
380
      deleted_count = (work_params["deleted_files_count"] || "0").to_i
40✔
381
      (1..deleted_count).map { |i| work_params["deleted_file_#{i}"] }.select(&:present?)
45✔
382
    end
383

384
    def process_updates
1✔
385
      resource_before = @work.resource
44✔
386
      if @work.update(update_params)
44✔
387

388
        resource_compare = ResourceCompareService.new(resource_before, update_params[:resource])
42✔
389
        @work.log_changes(resource_compare, current_user.id)
42✔
390

391
        if @wizard_mode
42✔
392
          redirect_to work_readme_select_url(@work)
7✔
393
        else
394
          redirect_to work_url(@work), notice: "Work was successfully updated."
35✔
395
        end
396
      else
397
        @uploads = @work.uploads
2✔
398
        render :edit, status: :unprocessable_entity
2✔
399
      end
400
    end
401

402
    def params_collection_id
1✔
403
      # Do not allow a nil for the collection id
404
      @params_collection_id ||= begin
106✔
405
        collection_id = params[:collection_id]
62✔
406
        if collection_id.blank?
62✔
407
          collection_id = current_user.default_collection.id
4✔
408
          Honeybadger.notify("We got a nil collection as part of the parameters #{params} #{request}")
4✔
409
        end
410
        collection_id
62✔
411
      end
412
    end
413
end
414
# 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