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

pulibrary / pdc_describe / 9cd1579d-9684-4b66-ae83-9619a7d64644

pending completion
9cd1579d-9684-4b66-ae83-9619a7d64644

Pull #1094

circleci

GitHub
Merge branch 'main' into sidekiq-prod
Pull Request #1094: Adding redis and sidekiq

1628 of 2126 relevant lines covered (76.58%)

97.58 hits per line

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

98.34
/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
340✔
21
    return true if action_name == "show" && request.format.symbol == :json && Work.find(params[:id]).state == "approved"
339✔
22
    false
339✔
23
  end
24

25
  def index
1✔
26
    @works = Work.all
2✔
27
    respond_to do |format|
2✔
28
      format.html
2✔
29
      format.rss { render layout: false }
3✔
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?
19✔
36
      render "new_submission"
8✔
37
    else
38
      @work = Work.new(created_by_user_id: current_user.id, collection: current_user.default_collection)
11✔
39
    end
40
  end
41

42
  def create
1✔
43
    @work = Work.new(created_by_user_id: current_user.id, collection_id: params_collection_id, user_entered_doi: params["doi"].present?)
13✔
44
    @work.resource = FormToResourceService.convert(params, @work)
13✔
45
    if @work.valid?
13✔
46
      @work.draft!(current_user)
13✔
47
      upload_service = WorkUploadsEditService.new(@work, current_user)
13✔
48
      upload_service.update_precurated_file_list(added_files_param, deleted_files_param)
13✔
49
      redirect_to work_url(@work), notice: "Work was successfully created."
13✔
50
    else
51
      render :new, status: :unprocessable_entity
×
52
    end
53
  end
54

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

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

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

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

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

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

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

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

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

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

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

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

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

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

169
  def file_uploaded
1✔
170
    @work = Work.find(params[:id])
4✔
171
    files = work_params.dig("patch", "pre_curation_uploads") || []
4✔
172
    if files.count > 0
4✔
173
      upload_service = WorkUploadsEditService.new(@work, current_user)
2✔
174
      @work = upload_service.update_precurated_file_list(files, [])
2✔
175
      @work.save!
1✔
176
    end
177
    redirect_to(work_review_path)
3✔
178
  rescue StandardError => active_storage_error
179
    Rails.logger.error("Failed to attach the file uploads for the work #{@work.doi}: #{active_storage_error}")
1✔
180
    flash[:notice] = "Failed to attach the file uploads for the work #{@work.doi}: #{active_storage_error}. Please contact rdss@princeton.edu for assistance."
1✔
181

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

284
  def readme_select
1✔
285
    @work = Work.find(params[:id])
8✔
286
    readme = Readme.new(@work)
8✔
287
    @readme = readme.file_name
8✔
288
    @wizard = true
8✔
289
  end
290

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

304
  private
1✔
305

306
    def work_params
1✔
307
      params[:work] || params
116✔
308
    end
309

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

313
      params[:patch]
12✔
314
    end
315

316
    def pre_curation_uploads_param
1✔
317
      return if patch_params.nil?
×
318

319
      patch_params[:pre_curation_uploads_added]
×
320
    end
321

322
    def readme_file_param
1✔
323
      return if patch_params.nil?
8✔
324

325
      patch_params[:readme_file]
8✔
326
    end
327

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

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

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

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

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

366
      process_updates
42✔
367
    end
368

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

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

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

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

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

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

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