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

pulibrary / pdc_describe / c4feae6c-aab8-4c77-8138-2800cf8b747e

30 May 2024 07:41PM UTC coverage: 95.644% (-0.3%) from 95.909%
c4feae6c-aab8-4c77-8138-2800cf8b747e

Pull #1829

circleci

bess
Remove outdated config
Pull Request #1829: Upgrade ruby to 3.3.2

3250 of 3398 relevant lines covered (95.64%)

239.03 hits per line

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

95.91
/app/models/work.rb
1
# frozen_string_literal: true
2

3
# rubocop:disable Metrics/ClassLength
4
class Work < ApplicationRecord
1✔
5
  # Errors for cases where there is no valid Group
6
  class InvalidGroupError < ::ArgumentError; end
1✔
7

8
  has_many :work_activity, -> { order(updated_at: :desc) }, dependent: :destroy
235✔
9
  has_many :user_work, -> { order(updated_at: :desc) }, dependent: :destroy
8✔
10
  has_many :upload_snapshots, -> { order(updated_at: :desc) }, dependent: :destroy
401✔
11

12
  belongs_to :group, class_name: "Group"
1✔
13
  belongs_to :curator, class_name: "User", foreign_key: "curator_user_id", optional: true
1✔
14

15
  attribute :work_type, :string, default: "DATASET"
1✔
16
  attribute :profile, :string, default: "DATACITE"
1✔
17

18
  attr_accessor :user_entered_doi
1✔
19

20
  alias state_history user_work
1✔
21

22
  delegate :valid_to_submit, :valid_to_draft, :valid_to_approve, :valid_to_complete, to: :work_validator
1✔
23

24
  include AASM
1✔
25

26
  aasm column: :state do
1✔
27
    state :none, initial: true
1✔
28
    state :draft, :awaiting_approval, :approved, :withdrawn, :deletion_marker
1✔
29

30
    event :draft, after: :draft_doi do
1✔
31
      transitions from: :none, to: :draft, guard: :valid_to_draft
1✔
32
    end
33

34
    event :complete_submission do
1✔
35
      transitions from: :draft, to: :awaiting_approval, guard: :valid_to_complete
1✔
36
    end
37

38
    event :request_changes do
1✔
39
      transitions from: :awaiting_approval, to: :awaiting_approval, guard: :valid_to_submit
1✔
40
    end
41

42
    event :approve do
1✔
43
      transitions from: :awaiting_approval, to: :approved, guard: :valid_to_approve, after: :publish
1✔
44
    end
45

46
    event :withdraw do
1✔
47
      transitions from: [:draft, :awaiting_approval, :approved], to: :withdrawn
1✔
48
    end
49

50
    event :resubmit do
1✔
51
      transitions from: :withdrawn, to: :draft
1✔
52
    end
53

54
    event :remove do
1✔
55
      transitions from: :withdrawn, to: :deletion_marker
1✔
56
    end
57

58
    after_all_events :track_state_change
1✔
59
  end
60

61
  def state=(new_state)
1✔
62
    new_state_sym = new_state.to_sym
765✔
63
    valid_states = self.class.aasm.states.map(&:name)
765✔
64
    raise(StandardError, "Invalid state '#{new_state}'") unless valid_states.include?(new_state_sym)
765✔
65
    aasm_write_state_without_persistence(new_state_sym)
764✔
66
  end
67

68
  ##
69
  # Is this work editable by a given user?
70
  # A work is editable when:
71
  # * it is being edited by the person who made it
72
  # * it is being edited by a group admin of the group where is resides
73
  # * it is being edited by a super admin
74
  # @param [User]
75
  # @return [Boolean]
76
  def editable_by?(user)
1✔
77
    submitted_by?(user) || administered_by?(user)
542✔
78
  end
79

80
  def editable_in_current_state?(user)
1✔
81
    # anyone with edit privleges can edit a work while it is in draft
82
    return editable_by?(user) if draft?
340✔
83

84
    # Only admisitrators can edit a work in other states
85
    administered_by?(user)
50✔
86
  end
87

88
  def submitted_by?(user)
1✔
89
    created_by_user_id == user.id
542✔
90
  end
91

92
  def administered_by?(user)
1✔
93
    user.has_role?(:group_admin, group)
101✔
94
  end
95

96
  class << self
1✔
97
    def find_by_doi(doi)
1✔
98
      prefix = "10.34770/"
31✔
99
      doi = "#{prefix}#{doi}" unless doi.blank? || doi.start_with?(prefix)
31✔
100
      Work.find_by!("metadata @> ?", JSON.dump(doi:))
31✔
101
    end
102

103
    def find_by_ark(ark)
1✔
104
      prefix = "ark:/"
230✔
105
      ark = "#{prefix}#{ark}" unless ark.blank? || ark.start_with?(prefix)
230✔
106
      Work.find_by!("metadata @> ?", JSON.dump(ark:))
230✔
107
    end
108

109
    delegate :resource_type_general_values, to: PDCMetadata::Resource
1✔
110
  end
111

112
  include Rails.application.routes.url_helpers
1✔
113

114
  before_save do |work|
1✔
115
    # Ensure that the metadata JSONB postgres field is persisted properly
116
    work.metadata = JSON.parse(work.resource.to_json)
1,267✔
117
  end
118

119
  after_save do |work|
1✔
120
    if work.approved?
1,266✔
121
      work.reload
109✔
122
    end
123
  end
124

125
  validate do |_work|
1✔
126
    work_validator.valid?
1,369✔
127
  end
128

129
  # Overload ActiveRecord.reload method
130
  # https://apidock.com/rails/ActiveRecord/Base/reload
131
  #
132
  # NOTE: Usually `after_save` is a better place to put this kind of code:
133
  #
134
  #   after_save do |work|
135
  #     work.resource = nil
136
  #   end
137
  #
138
  # but that does not work in this case because the block points to a different
139
  # memory object for `work` than the we want we want to reload.
140
  def reload(options = nil)
1✔
141
    super
206✔
142
    # Force `resource` to be reloaded
143
    @resource = nil
206✔
144
    self
206✔
145
  end
146

147
  def title
1✔
148
    resource.main_title
499✔
149
  end
150

151
  def uploads_attributes
1✔
152
    return [] if approved? # once approved we no longer allow the updating of uploads via the application
192✔
153
    uploads.map do |upload|
186✔
154
      {
155
        id: upload.id,
50✔
156
        key: upload.key,
157
        filename: upload.filename.to_s,
158
        created_at: upload.created_at,
159
        url: upload.url
160
      }
161
    end
162
  end
163

164
  def form_attributes
1✔
165
    {
166
      uploads: uploads_attributes
192✔
167
    }
168
  end
169

170
  def draft_doi
1✔
171
    return if resource.doi.present?
95✔
172
    resource.doi = datacite_service.draft_doi
89✔
173
    save!
85✔
174
  end
175

176
  # Return the DOI formatted as a URL, so it can be used as a link on display pages
177
  # @return [String] A url formatted version of the DOI
178
  def doi_url
1✔
179
    return "https://doi.org/#{doi}" unless doi.starts_with?("https://doi.org")
1✔
180
    doi
×
181
  end
182

183
  def created_by_user
1✔
184
    User.find(created_by_user_id)
531✔
185
  rescue ActiveRecord::RecordNotFound
186
    nil
1✔
187
  end
188

189
  def resource=(resource)
1✔
190
    @resource = resource
1,047✔
191
    # Ensure that the metadata JSONB postgres field is persisted properly
192
    self.metadata = JSON.parse(resource.to_json)
1,047✔
193
  end
194

195
  def resource
1✔
196
    @resource ||= PDCMetadata::Resource.new_from_jsonb(metadata)
37,654✔
197
  end
198

199
  def url
1✔
200
    return unless persisted?
×
201

202
    @url ||= url_for(self)
×
203
  end
204

205
  def files_location_upload?
1✔
206
    files_location.blank? || files_location == "file_upload"
11✔
207
  end
208

209
  def files_location_cluster?
1✔
210
    files_location == "file_cluster"
130✔
211
  end
212

213
  def files_location_other?
1✔
214
    files_location == "file_other"
141✔
215
  end
216

217
  def change_curator(curator_user_id, current_user)
1✔
218
    if curator_user_id == "no-one"
6✔
219
      clear_curator(current_user)
1✔
220
    else
221
      update_curator(curator_user_id, current_user)
5✔
222
    end
223
  end
224

225
  def clear_curator(current_user)
1✔
226
    # Update the curator on the Work
227
    self.curator_user_id = nil
2✔
228
    save!
2✔
229

230
    # ...and log the activity
231
    WorkActivity.add_work_activity(id, "Unassigned existing curator", current_user.id, activity_type: WorkActivity::SYSTEM)
2✔
232
  end
233

234
  def update_curator(curator_user_id, current_user)
1✔
235
    # Update the curator on the Work
236
    self.curator_user_id = curator_user_id
6✔
237
    save!
6✔
238

239
    # ...and log the activity
240
    new_curator = User.find(curator_user_id)
5✔
241

242
    work_url = "[#{title}](#{Rails.application.routes.url_helpers.work_url(self)})"
5✔
243

244
    # Troubleshooting https://github.com/pulibrary/pdc_describe/issues/1783
245
    if work_url.include?("/describe/describe/")
5✔
246
      Rails.logger.error("URL #{work_url} included /describe/describe/ and was fixed. See https://github.com/pulibrary/pdc_describe/issues/1783")
×
247
      work_url = work_url.gsub("/describe/describe/", "/describe/")
×
248
    end
249

250
    message = if curator_user_id.to_i == current_user.id
5✔
251
                "Self-assigned @#{current_user.uid} as curator for work #{work_url}"
2✔
252
              else
253
                "Set curator to @#{new_curator.uid} for work #{work_url}"
3✔
254
              end
255
    WorkActivity.add_work_activity(id, message, current_user.id, activity_type: WorkActivity::SYSTEM)
5✔
256
  end
257

258
  def add_message(message, current_user_id)
1✔
259
    WorkActivity.add_work_activity(id, message, current_user_id, activity_type: WorkActivity::MESSAGE)
11✔
260
  end
261

262
  def add_provenance_note(date, note, current_user_id, change_label = "")
1✔
263
    WorkActivity.add_work_activity(id, { note:, change_label: }.to_json, current_user_id, activity_type: WorkActivity::PROVENANCE_NOTES, created_at: date)
43✔
264
    # WorkActivity.add_work_activity(id, note, current_user_id, activity_type: WorkActivity::PROVENANCE_NOTES, created_at: date)
265
  end
266

267
  def log_changes(resource_compare, current_user_id)
1✔
268
    return if resource_compare.identical?
86✔
269
    WorkActivity.add_work_activity(id, resource_compare.differences.to_json, current_user_id, activity_type: WorkActivity::CHANGES)
71✔
270
  end
271

272
  def log_file_changes(current_user_id)
1✔
273
    return if changes.count == 0
19✔
274
    WorkActivity.add_work_activity(id, changes.to_json, current_user_id, activity_type: WorkActivity::FILE_CHANGES)
19✔
275
  end
276

277
  def activities
1✔
278
    WorkActivity.activities_for_work(id, WorkActivity::MESSAGE_ACTIVITY_TYPES + WorkActivity::CHANGE_LOG_ACTIVITY_TYPES)
162✔
279
  end
280

281
  def new_notification_count_for_user(user_id)
1✔
282
    WorkActivityNotification.joins(:work_activity)
63✔
283
                            .where(user_id:, read_at: nil)
284
                            .where(work_activity: { work_id: id })
285
                            .count
286
  end
287

288
  # Marks as read the notifications for the given user_id in this work.
289
  # In practice, the user_id is the id of the current user and therefore this method marks the current's user
290
  # notifications as read.
291
  def mark_new_notifications_as_read(user_id)
1✔
292
    activities.each do |activity|
160✔
293
      unread_notifications = WorkActivityNotification.where(user_id:, work_activity_id: activity.id, read_at: nil)
196✔
294
      unread_notifications.each do |notification|
196✔
295
        notification.read_at = Time.now.utc
66✔
296
        notification.save
66✔
297
      end
298
    end
299
  end
300

301
  def current_transition
1✔
302
    aasm.current_event.to_s.humanize.delete("!")
25✔
303
  end
304

305
  def uploads
1✔
306
    return post_curation_uploads if approved?
259✔
307

308
    pre_curation_uploads
251✔
309
  end
310

311
  # Returns the list of files for the work with some basic information about each of them.
312
  # This method is much faster than `uploads` because it does not return the actual S3File
313
  # objects to the client, instead it returns just a few selected data elements.
314
  def file_list
1✔
315
    s3_files = approved? ? post_curation_uploads : pre_curation_uploads
373✔
316
    files_info = s3_files.map do |s3_file|
373✔
317
      {
318
        "safe_id": s3_file.safe_id,
175✔
319
        "filename": s3_file.filename,
320
        "filename_display": s3_file.filename_display,
321
        "last_modified": s3_file.last_modified,
322
        "last_modified_display": s3_file.last_modified_display,
323
        "size": s3_file.size,
324
        "display_size": s3_file.display_size,
325
        "url": s3_file.url
326
      }
327
    end
328
    files_info
373✔
329
  end
330

331
  def total_file_size
1✔
332
    @total_file_size ||= begin
173✔
333
      total_size = 0
161✔
334
      file_list.each do |file|
161✔
335
        total_size += file[:size]
66✔
336
      end
337
      total_size
161✔
338
    end
339
  end
340

341
  # Fetches the data from S3 directly bypassing ActiveStorage
342
  def pre_curation_uploads
1✔
343
    s3_query_service.client_s3_files.sort_by(&:filename)
765✔
344
  end
345

346
  # Accesses post-curation S3 Bucket Objects
347
  def post_curation_s3_resources
1✔
348
    if approved?
51✔
349
      s3_resources
42✔
350
    else
351
      []
9✔
352
    end
353
  end
354

355
  # Returns the files in post-curation for the work
356
  def post_curation_uploads(force_post_curation: false)
1✔
357
    if force_post_curation
56✔
358
      # Always use the post-curation data regardless of the work's status
359
      post_curation_s3_query_service = S3QueryService.new(self, "postcuration")
5✔
360
      post_curation_s3_query_service.data_profile.fetch(:objects, [])
5✔
361
    else
362
      # Return the list based of files honoring the work status
363
      post_curation_s3_resources
51✔
364
    end
365
  end
366

367
  def s3_files
1✔
368
    pre_curation_uploads
×
369
  end
370

371
  def s3_client
1✔
372
    s3_query_service.client
22✔
373
  end
374

375
  delegate :bucket_name, :prefix, to: :s3_query_service
1✔
376
  delegate :doi_attribute_url, :curator_or_current_uid, to: :datacite_service
1✔
377

378
  # Generates the S3 Object key
379
  # @return [String]
380
  def s3_object_key
1✔
381
    "#{doi}/#{id}"
81✔
382
  end
383

384
  # Transmit a HEAD request for the S3 Bucket directory for this Work
385
  # @param bucket_name location to be checked to be found
386
  # @return [Aws::S3::Types::HeadObjectOutput]
387
  def find_post_curation_s3_dir(bucket_name:)
1✔
388
    # TODO: Directories really do not exists in S3
389
    #      if we really need this check then we need to do something else to check the bucket
390
    s3_client.head_object({
22✔
391
                            bucket: bucket_name,
392
                            key: s3_object_key
393
                          })
394
    true
22✔
395
  rescue Aws::S3::Errors::NotFound
396
    nil
×
397
  end
398

399
  # Generates the JSON serialized expression of the Work
400
  # @param args [Array<Hash>]
401
  # @option args [Boolean] :force_post_curation Force the request of AWS S3
402
  #   Resources, clearing the in-memory cache
403
  # @return [String]
404
  def as_json(*args)
1✔
405
    files = files_as_json(*args)
14✔
406

407
    # to_json returns a string of serialized JSON.
408
    # as_json returns the corresponding hash.
409
    {
410
      "resource" => resource.as_json,
14✔
411
      "files" => files,
412
      "group" => group.as_json.except("id"),
413
      "embargo_date" => embargo_date_as_json,
414
      "created_at" => format_date_for_solr(created_at),
415
      "updated_at" => format_date_for_solr(updated_at)
416
    }
417
  end
418

419
  # Format the date for Apache Solr
420
  # @param date [ActiveSupport::TimeWithZone]
421
  # @return [String]
422
  def format_date_for_solr(date)
1✔
423
    date.strftime("%Y-%m-%dT%H:%M:%SZ")
28✔
424
  end
425

426
  def pre_curation_uploads_count
1✔
427
    s3_query_service.file_count
2✔
428
  end
429

430
  delegate :ark, :doi, :resource_type, :resource_type=, :resource_type_general, :resource_type_general=,
1✔
431
           :to_xml, to: :resource
432

433
  # S3QueryService object associated with this Work
434
  # @return [S3QueryService]
435
  def s3_query_service
1✔
436
    mode = approved? ? "postcuration" : "precuration"
1,581✔
437
    @s3_query_service ||= S3QueryService.new(self, mode)
1,581✔
438
  end
439

440
  def past_snapshots
1✔
441
    UploadSnapshot.where(work: self)
×
442
  end
443

444
  # Build or find persisted UploadSnapshot models for this Work
445
  # @param [integer] user_id optional user to assign the snapshot to
446
  # @return [UploadSnapshot]
447
  def reload_snapshots(user_id: nil)
1✔
448
    work_changes = []
17✔
449
    s3_files = pre_curation_uploads
17✔
450
    s3_filenames = s3_files.map(&:filename)
17✔
451

452
    upload_snapshot = latest_snapshot
17✔
453

454
    upload_snapshot.snapshot_deletions(work_changes, s3_filenames)
17✔
455

456
    upload_snapshot.snapshot_modifications(work_changes, s3_files)
17✔
457

458
    # Create WorkActivity models with the set of changes
459
    unless work_changes.empty?
17✔
460
      new_snapshot = UploadSnapshot.new(work: self, url: s3_query_service.prefix)
13✔
461
      new_snapshot.store_files(s3_files)
13✔
462
      new_snapshot.save!
13✔
463
      WorkActivity.add_work_activity(id, work_changes.to_json, user_id, activity_type: WorkActivity::FILE_CHANGES)
13✔
464
    end
465
  end
466

467
  def self.presenter_class
1✔
468
    WorkPresenter
178✔
469
  end
470

471
  def presenter
1✔
472
    self.class.presenter_class.new(work: self)
178✔
473
  end
474

475
  def changes
1✔
476
    @changes ||= []
197✔
477
  end
478

479
  def track_change(action, filename)
1✔
480
    changes << { action:, filename: }
33✔
481
  end
482

483
  # rubocop:disable Naming/PredicateName
484
  def has_rights?(rights_id)
1✔
485
    resource.rights_many.index { |rights| rights.identifier == rights_id } != nil
2,695✔
486
  end
487
  # rubocop:enable Naming/PredicateName
488

489
  # This is the solr id / work show page in PDC Discovery
490
  def pdc_discovery_url
1✔
491
    "https://datacommons.princeton.edu/discovery/catalog/doi-#{doi.tr('/', '-').tr('.', '-')}"
289✔
492
  end
493

494
  # Determine whether or not the Work is under active embargo
495
  # @return [Boolean]
496
  def embargoed?
1✔
497
    return false if embargo_date.blank?
328✔
498

499
    current_date = Time.zone.now
4✔
500
    embargo_date >= current_date
4✔
501
  end
502

503
  def upload_count
1✔
504
    @upload_count ||= s3_query_service.count_objects
1,433✔
505
  end
506

507
  protected
1✔
508

509
    def work_validator
1✔
510
      @work_validator ||= WorkValidator.new(self)
1,577✔
511
    end
512

513
    # This must be protected, NOT private for ActiveRecord to work properly with this attribute.
514
    #   Protected will still keep others from setting the metatdata, but allows ActiveRecord the access it needs
515
    def metadata=(metadata)
1✔
516
      super
2,314✔
517
      @resource = PDCMetadata::Resource.new_from_jsonb(metadata)
2,314✔
518
    end
519

520
  private
1✔
521

522
    def publish(user)
1✔
523
      datacite_service.publish_doi(user)
28✔
524
      update_ark_information
28✔
525
      publish_precurated_files(user)
28✔
526
      save!
6✔
527
    end
528

529
    # Update EZID (our provider of ARKs) with the new information for this work.
530
    def update_ark_information
1✔
531
      # We only want to update the ark url under certain conditions.
532
      # Set this value in config/update_ark_url.yml
533
      if Rails.configuration.update_ark_url
28✔
534
        if ark.present?
19✔
535
          Ark.update(ark, datacite_service.doi_attribute_url)
3✔
536
        end
537
      end
538
    end
539

540
    def track_state_change(user, state = aasm.to_state)
1✔
541
      uw = UserWork.new(user_id: user.id, work_id: id, state:)
162✔
542
      uw.save!
162✔
543
      WorkActivity.add_work_activity(id, "marked as #{state.to_s.titleize}", user.id, activity_type: WorkActivity::SYSTEM)
162✔
544
      WorkStateTransitionNotification.new(self, user.id).send
162✔
545
    end
546

547
    # Request S3 Bucket Objects associated with this Work
548
    # @return [Array<S3File>]
549
    def s3_resources
1✔
550
      data_profile = s3_query_service.data_profile
42✔
551
      data_profile.fetch(:objects, [])
42✔
552
    end
553
    alias pre_curation_s3_resources s3_resources
1✔
554

555
    def s3_object_persisted?(s3_file)
1✔
556
      uploads_keys = uploads.map(&:key)
×
557
      uploads_keys.include?(s3_file.key)
×
558
    end
559

560
    def publish_precurated_files(user)
1✔
561
      # We need to explicitly check the to post-curation bucket here.
562
      s3_post_curation_query_service = S3QueryService.new(self, "postcuration")
22✔
563

564
      s3_dir = find_post_curation_s3_dir(bucket_name: s3_post_curation_query_service.bucket_name)
22✔
565
      raise(StandardError, "Attempting to publish a Work with an existing S3 Bucket directory for: #{s3_object_key}") unless s3_dir.nil?
22✔
566

567
      # Copy the pre-curation S3 Objects to the post-curation S3 Bucket...
568
      s3_query_service.publish_files(user)
×
569
    end
570

571
    def latest_snapshot
1✔
572
      return upload_snapshots.first unless upload_snapshots.empty?
17✔
573

574
      UploadSnapshot.new(work: self, files: [])
12✔
575
    end
576

577
    def datacite_service
1✔
578
      @datacite_service ||= PULDatacite.new(self)
122✔
579
    end
580

581
    def files_as_json(*args)
1✔
582
      return [] if embargoed?
14✔
583

584
      force_post_curation = args.any? { |arg| arg[:force_post_curation] == true }
25✔
585

586
      # Pre-curation files are not accessible externally,
587
      # so we are not interested in listing them in JSON.
588
      post_curation_uploads(force_post_curation:).map do |upload|
13✔
589
        {
590
          "filename": upload.filename,
8✔
591
          "size": upload.size,
592
          "display_size": upload.display_size,
593
          "url": upload.globus_url
594
        }
595
      end
596
    end
597

598
    def embargo_date_as_json
1✔
599
      if embargo_date.present?
14✔
600
        embargo_datetime = embargo_date.to_datetime
2✔
601
        embargo_date_iso8601 = embargo_datetime.iso8601
2✔
602
        # Apache Solr timestamps require the following format:
603
        # 1972-05-20T17:33:18Z
604
        # https://solr.apache.org/guide/solr/latest/indexing-guide/date-formatting-math.html
605
        embargo_date_iso8601.gsub(/\+.+$/, "Z")
2✔
606
      end
607
    end
608
end
609
# 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