• 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

22.47
/app/services/form_to_resource_service.rb
1
# frozen_string_literal: true
2
class FormToResourceService
1✔
3
  class << self
1✔
4
    # Convert params into a resource
5
    #
6
    #  @param [Hash] params controller params to be converted
7
    #  @param [Work] work params will be applied to. Utilizes the work for old values if needed.
8
    #
9
    # @return [PDCMetadata::Resource] Fully formed resource containing updates from the user
10
    #
11
    def convert(params, work)
1✔
12
      resource = reset_resource_to_work(work)
×
13

14
      resource.description = params["description"]
×
15
      resource.publisher = params["publisher"] if params["publisher"].present?
×
16
      resource.publication_year = params["publication_year"] if params["publication_year"].present?
×
17
      resource.keywords = (params["keywords"] || "").split(",").map(&:strip)
×
18

19
      add_rights(params, resource)
×
20
      add_additional_metadata(params, resource)
×
21
      add_curator_controlled(params, resource)
×
22
      add_titles(params, resource)
×
23
      add_related_objects(params, resource)
×
24
      add_creators(params, resource)
×
25
      add_individual_contributors(params, resource)
×
26
      add_organizational_contributors(params, resource)
×
27
      add_funders(params, resource)
×
28

29
      resource
×
30
    end
31

32
    private
1✔
33

34
      def reset_resource_to_work(work)
1✔
35
        resource = PDCMetadata::Resource.new
×
36

37
        resource.doi = work.doi
×
38
        resource.ark = work.ark
×
39
        resource.migrated = work.resource.migrated
×
40
        resource.collection_tags = work.resource.collection_tags || []
×
41
        resource.publisher = work.group.publisher
×
42
        resource
×
43
      end
44

45
      def add_rights(params, resource)
1✔
46
        resource.rights_many = (params["rights_identifiers"] || []).map do |rights_id|
×
47
          PDCMetadata::Rights.find(rights_id)
×
48
        end.compact
49
      end
50

51
      def add_additional_metadata(params, resource)
1✔
52
        resource.domains = params["domains"] || []
×
53
        resource.communities = params["communities"] || []
×
54
        resource.subcommunities = params["subcommunities"] || []
×
55
      end
56

57
      def add_curator_controlled(params, resource)
1✔
58
        resource.doi = params["doi"] if params["doi"].present?
×
59
        resource.ark = params["ark"] if params["ark"].present?
×
60
        resource.version_number = params["version_number"] if params["version_number"].present?
×
61
        resource.collection_tags = params["collection_tags"].split(",").map(&:strip) if params["collection_tags"]
×
62
        resource.resource_type = params["resource_type"] if params["resource_type"]
×
63
        resource.resource_type_general = params["resource_type_general"] if params["resource_type_general"]
×
64
      end
65

66
      # Titles:
67

68
      def add_titles(params, resource)
1✔
69
        resource.titles << PDCMetadata::Title.new(title: params["title_main"])
×
70
        resource.titles.concat((1..params["existing_title_count"].to_i).filter_map do |i|
×
71
          title = params["title_#{i}"]
×
72
          title_type = params["title_type_#{i}"]
×
73
          new_title(title, title_type)
×
74
        end)
75
        resource.titles.concat((1..params["new_title_count"].to_i).filter_map do |i|
×
76
          title = params["new_title_#{i}"]
×
77
          title_type = params["new_title_type_#{i}"]
×
78
          new_title(title, title_type)
×
79
        end)
80
      end
81

82
      def new_title(title, title_type)
1✔
83
        return if title.blank?
×
84
        PDCMetadata::Title.new(title:, title_type:)
×
85
      end
86

87
      # Related Objects:
88

89
      def add_related_objects(params, resource)
1✔
90
        return if params[:related_objects].blank?
×
91
        resource.related_objects = params[:related_objects].each.filter_map do |related_object|
×
92
          new_related_object(related_object[:related_identifier], related_object[:related_identifier_type], related_object[:relation_type])
×
93
        end
94
      end
95

96
      def new_related_object(related_identifier, related_identifier_type, relation_type)
1✔
97
        return if related_identifier.blank? && related_identifier_type.blank? && relation_type.blank?
×
98
        PDCMetadata::RelatedObject.new(related_identifier:, related_identifier_type:, relation_type:)
×
99
      end
100

101
      # Creators:
102

103
      def add_creators(params, resource)
1✔
104
        resource.creators = params[:creators].each_with_index.filter_map do |creator, idx|
×
105
          new_creator(creator[:given_name], creator[:family_name], creator[:orcid], idx, creator[:affiliation], creator[:ror])
×
106
        end
107
      end
108

109
      def new_creator(given_name, family_name, orcid, sequence, affiliation, ror)
1✔
110
        return if family_name.blank? && given_name.blank? && orcid.blank?
×
111
        PDCMetadata::Creator.new_person(given_name, family_name, orcid, sequence, affiliation:, ror:)
×
112
      end
113

114
      # Individual Contributors:
115

116
      def add_individual_contributors(params, resource)
1✔
117
        return if params[:contributors].blank?
×
118
        resource.individual_contributors = params[:contributors].each_with_index.filter_map do |contributor, idx|
×
119
          new_individual_contributor(contributor[:given_name], contributor[:family_name], contributor[:orcid], contributor[:role], idx)
×
120
        end
121
      end
122

123
      def new_individual_contributor(given_name, family_name, orcid, type, sequence)
1✔
124
        return if family_name.blank? && given_name.blank? && orcid.blank?
×
125
        PDCMetadata::Creator.new_individual_contributor(given_name, family_name, orcid, type, sequence)
×
126
      end
127

128
      # Organizational Contributors:
129

130
      def add_organizational_contributors(params, resource)
1✔
131
        # (New pattern: Use rails param name conventions rather than numbering fields.)
132
        resource.organizational_contributors = (params[:organizational_contributors] || []).filter_map do |contributor|
×
133
          value = contributor["value"]
×
134
          ror = contributor["ror"]
×
135
          type = contributor["type"]
×
136
          new_organizational_contributor(value, ror, type)
×
137
        end
138
      end
139

140
      def new_organizational_contributor(value, ror, type)
1✔
141
        return if value.blank? && ror.blank?
×
142
        PDCMetadata::Creator.new_organizational_contributor(value, ror, type)
×
143
      end
144

145
      # Funders:
146

147
      def add_funders(params, resource)
1✔
148
        resource.funders = (params[:funders] || []).filter_map do |funder|
×
149
          new_funder(funder[:ror], funder[:funder_name], funder[:award_number], funder[:award_uri])
×
150
        end
151
      end
152

153
      def new_funder(ror, funder_name, award_number, award_uri)
1✔
154
        return if funder_name.blank? && award_number.blank? && award_uri.blank?
×
155
        PDCMetadata::Funder.new(ror, funder_name, award_number, award_uri)
×
156
      end
157
  end
158
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