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

pulibrary / pdc_describe / 64e42f72-7d76-486a-b44a-a33c65efaa6e

pending completion
64e42f72-7d76-486a-b44a-a33c65efaa6e

Pull #1098

circleci

jrgriffiniii
Renaming the Collection class to the Group class; Ensuring that legacy table names are preserved
Pull Request #1098: Renaming the Collection class to the Group class and ensures that legacy table names are preserved

114 of 114 new or added lines in 12 files covered. (100.0%)

1899 of 2157 relevant lines covered (88.04%)

161.82 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
483✔
21
    return true if action_name == "show" && request.format.symbol == :json && Work.find(params[:id]).state == "approved"
481✔
22
    false
480✔
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?
30✔
36
      render "new_submission"
8✔
37
    else
38
      @work = Work.new(created_by_user_id: current_user.id, group: current_user.default_group)
22✔
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?)
26✔
44
    @work.resource = FormToResourceService.convert(params, @work)
26✔
45
    if @work.valid?
26✔
46
      @work.draft!(current_user)
24✔
47
      upload_service = WorkUploadsEditService.new(@work, current_user)
24✔
48
      upload_service.update_precurated_file_list(added_files_param, deleted_files_param)
24✔
49
      redirect_to work_url(@work), notice: "Work was successfully created."
24✔
50
    else
51
      render :new, status: :unprocessable_entity
2✔
52
    end
53
  end
54

55
  # Creates the new dataset
56
  def new_submission
1✔
57
    default_collection_id = current_user.default_group.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])
86✔
69
    @changes =  WorkActivity.changes_for_work(@work.id)
86✔
70
    @messages = WorkActivity.messages_for_work(@work.id)
86✔
71

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

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

85
  def file_list
1✔
86
    if params[:id] == "NONE"
127✔
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: []
23✔
90
    else
91
      @work = Work.find(params[:id])
104✔
92
      render json: @work.uploads
104✔
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])
43✔
109
    if current_user && @work.editable_by?(current_user)
43✔
110
      if @work.approved? && !@work.administered_by?(current_user)
40✔
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
39✔
115
        @wizard_mode = wizard_mode?
39✔
116
        render "edit"
39✔
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
    if pre_curation_uploads_param
4✔
172
      @work.pre_curation_uploads.attach(pre_curation_uploads_param)
2✔
173
      @work.save!
1✔
174
    end
175

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

303
  private
1✔
304

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

309
    def patch_params
1✔
310
      return {} unless params.key?(:patch)
26✔
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?
8✔
323

324
      patch_params[:readme_file]
8✔
325
    end
326

327
    def rescue_aasm_error
1✔
328
      yield
68✔
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"
112✔
351
    end
352

353
    def update_work
1✔
354
      @wizard_mode = wizard_mode?
43✔
355
      upload_service = WorkUploadsEditService.new(@work, current_user)
43✔
356
      if @work.approved?
43✔
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)
38✔
363
      end
364

365
      process_updates
42✔
366
    end
367

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

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

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

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

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

391
        if @wizard_mode
40✔
392
          redirect_to work_readme_select_url(@work)
7✔
393
        else
394
          redirect_to work_url(@work), notice: "Work was successfully updated."
33✔
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
108✔
405
        collection_id = params[:collection_id]
66✔
406
        if collection_id.blank?
66✔
407
          collection_id = current_user.default_group.id
2✔
408
          Honeybadger.notify("We got a nil collection as part of the parameters #{params} #{request}")
2✔
409
        end
410
        collection_id
66✔
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