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

pulibrary / pdc_describe / 9091a1ae-29be-458c-984a-339d213919c4

12 Dec 2024 07:41PM UTC coverage: 26.434% (-69.7%) from 96.113%
9091a1ae-29be-458c-984a-339d213919c4

Pull #2000

circleci

jrgriffiniii
Removing integration with ActiveStorage
Pull Request #2000: Bump actionpack from 7.2.1.1 to 7.2.2.1

945 of 3575 relevant lines covered (26.43%)

0.35 hits per line

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

0.0
/app/services/work_validator.rb
1
# frozen_string_literal: true
2
class WorkValidator
×
3
  include Rails.application.routes.url_helpers
×
4
  attr_reader :work
×
5

6
  delegate :errors, :metadata, :resource, :ark, :doi, :user_entered_doi, :id, :group,
×
7
           :pre_curation_uploads, :post_curation_uploads, to: :work
×
8

9
  def initialize(work)
×
10
    @work = work
×
11
  end
×
12

13
  def valid?
×
14
    if work.none?
×
15
      validate_ids
×
16
    elsif work.draft?
×
17
      valid_to_draft
×
18
    else
×
19
      valid_to_submit
×
20
    end
×
21
  end
×
22

23
  def valid_to_draft(*)
×
24
    errors.add(:base, "Must provide a title") if resource.main_title.blank?
×
25
    validate_ark
×
26
    validate_creators
×
27
    validate_related_objects if resource.present? && !resource.related_objects.empty?
×
28
    errors.count == 0
×
29
  end
×
30

31
  def valid_to_complete(*args)
×
32
    validate_files
×
33
    valid_to_submit(args)
×
34
  end
×
35

36
  def valid_to_submit(*args)
×
37
    valid_to_draft(args)
×
38
    validate_metadata
×
39
    if errors.count == 0
×
40
      valid_datacite? # test if the datacite update will be valid
×
41
    end
×
42
    errors.count == 0
×
43
  end
×
44

45
  def valid_to_approve(user)
×
46
    if resource.doi.blank?
×
47
      errors.add :base, "DOI must be present for a work to be approved"
×
48
    end
×
49
    valid_to_submit(user)
×
50
    unless user.has_role? :group_admin, group
×
51
      errors.add :base, "Unauthorized to Approve"
×
52
    end
×
53
    validate_files
×
54
    if pre_curation_uploads.empty? && post_curation_uploads.empty?
×
55
      errors.add(:base, "You must include at least one file. <a href='#{work_file_upload_path(work)}'>Please upload one</a>")
×
56
    end
×
57
    errors.count == 0
×
58
  end
×
59

60
  def valid_datacite?
×
61
    datacite_serialization = resource.datacite_serialization
×
62
    datacite_serialization.valid?
×
63
    datacite_serialization.errors.each { |error| errors.add(:base, error) }
×
64
    errors.count == 0
×
65
  rescue ArgumentError => error
×
66
    argument_path = error.backtrace_locations.first.path
×
67
    argument_file = argument_path.split("/").last
×
68
    argument_name = argument_file.split(".").first
×
69
    errors.add(:base, "#{argument_name.titleize}: #{error.message}")
×
70
    false
×
71
  end
×
72

73
  private
×
74

75
    def validate_ark
×
76
      return if ark.blank?
×
77
      return false unless unique_ark
×
78
      first_save = id.blank?
×
79
      changed_value = metadata["ark"] != ark
×
80
      if first_save || changed_value
×
81
        errors.add(:base, "Invalid ARK provided for the Work: #{ark}") unless Ark.valid?(ark)
×
82
      end
×
83
    end
×
84

85
    def validate_related_objects
×
86
      return if resource.related_objects.empty?
×
87
      invalid = resource.related_objects.reject(&:valid?)
×
88
      if invalid.count.positive?
×
89
        related_object_errors = "Related Objects are invalid: "
×
90
        prev_errors = errors.to_a
×
91
        prev_related_object_errors = prev_errors.map { |e| e.include?(related_object_errors) }.reduce(:|)
×
92

93
        errors.add(:base, "#{related_object_errors}#{invalid.map(&:errors).join(', ')}") unless prev_related_object_errors
×
94
      end
×
95
    end
×
96

97
    def validate_creators
×
98
      if resource.creators.count == 0
×
99
        errors.add(:base, "Must provide at least one Creator")
×
100
      else
×
101
        resource.creators.each do |creator|
×
102
          if creator.orcid.present? && Orcid.invalid?(creator.orcid)
×
103
            errors.add(:base, "ORCID for creator #{creator.value} is not in format 0000-0000-0000-0000")
×
104
          end
×
105
        end
×
106
      end
×
107
    end
×
108

109
    def validate_required_metadata
×
110
      return if metadata.blank?
×
111
      errors.add(:base, "Must provide a title") if resource.main_title.blank?
×
112
      validate_creators
×
113
    end
×
114

115
    def validate_doi
×
116
      return true unless user_entered_doi
×
117
      return false unless unique_doi
×
118
      if /^10.\d{4,9}\/[-._;()\/:a-z0-9\-]+$/.match?(doi.downcase)
×
119
        response = Faraday.get("#{Rails.configuration.datacite.doi_url}#{doi}")
×
120
        errors.add(:base, "Invalid DOI: can not verify it's authenticity") unless response.success? || response.status == 302
×
121
      else
×
122
        errors.add(:base, "Invalid DOI: does not match format")
×
123
      end
×
124
      errors.count == 0
×
125
    end
×
126

127
    def unique_ark
×
128
      return true if ark.blank?
×
129
      other_record = Work.find_by_ark(ark)
×
130
      return true if other_record == work
×
131
      errors.add(:base, "Invalid ARK: It has already been applied to another work #{other_record.id}")
×
132
      false
×
133
    rescue ActiveRecord::RecordNotFound
×
134
      true
×
135
    end
×
136

137
    def validate_ids
×
138
      validate_doi
×
139
      unique_ark
×
140
    end
×
141

142
    def unique_doi
×
143
      other_record = Work.find_by_doi(doi)
×
144
      return true if other_record == work
×
145
      errors.add(:base, "Invalid DOI: It has already been applied to another work #{other_record.id}")
×
146
      false
×
147
    rescue ActiveRecord::RecordNotFound
×
148
      true
×
149
    end
×
150

151
    def validate_metadata
×
152
      return if metadata.blank?
×
153
      validate_required_metadata
×
154
      errors.add(:base, "Must provide a description") if resource.description.blank?
×
155
      errors.add(:base, "Must indicate the Publisher") if resource.publisher.blank?
×
156
      errors.add(:base, "Must indicate the Publication Year") if resource.publication_year.blank?
×
157
      errors.add(:base, "Must indicate at least one Rights statement") if resource.rights_many.count == 0
×
158
      errors.add(:base, "Must provide a Version number") if resource.version_number.blank?
×
159
      validate_related_objects
×
160
    end
×
161

162
    def validate_files
×
163
      return if @work.resource.migrated
×
164
      readme = Readme.new(work, nil)
×
165

166
      # when files are not uploaded
167
      errors.add(:base, "You must include a README. <a href='#{work_readme_select_path(work)}'>Please upload one</a>") if readme.blank?
×
168
      if !work.files_location_upload?
×
169
      elsif work.uploads.length < 2
×
170

171
        # files_location_upload?
172
        # 1 readme and 1 file
173
        # 2 readme files and 0 files
174
        errors.add(:base,
×
175
        "You must include one or more files if you are uploading files from your local environment. <a href='#{work_file_upload_path(work)}'>Please resubmit after uploading the file(s)</a>")
×
176
      end
×
177
    end
×
178
end
×
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