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

pulibrary / pdc_describe / cace366a-ffad-45f1-9b60-678e607fa527

14 May 2024 02:21PM UTC coverage: 60.862% (-35.0%) from 95.908%
cace366a-ffad-45f1-9b60-678e607fa527

push

circleci

jrgriffiniii
wip

1 of 3 new or added lines in 2 files covered. (33.33%)

1194 existing lines in 57 files now uncovered.

2076 of 3411 relevant lines covered (60.86%)

22.71 hits per line

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

94.38
/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✔
13

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

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

29
      resource
13✔
30
    end
31

32
    private
1✔
33

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

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

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

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

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

66
      # Titles:
67

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

82
      def new_title(title, title_type)
1✔
83
        return if title.blank?
10✔
UNCOV
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?
13✔
91
        resource.related_objects = params[:related_objects].each.filter_map do |related_object|
11✔
92
          new_related_object(related_object[:related_identifier], related_object[:related_identifier_type], related_object[:relation_type])
18✔
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?
18✔
98
        PDCMetadata::RelatedObject.new(related_identifier:, related_identifier_type:, relation_type:)
14✔
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|
13✔
105
          new_creator(creator[:given_name], creator[:family_name], creator[:orcid], idx, creator[:affiliation], creator[:ror])
104✔
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?
104✔
111
        PDCMetadata::Creator.new_person(given_name, family_name, orcid, sequence, affiliation:, ror:)
104✔
112
      end
113

114
      # Individual Contributors:
115

116
      def add_individual_contributors(params, resource)
1✔
117
        return if params[:contributors].blank?
13✔
118
        resource.individual_contributors = params[:contributors].each_with_index.filter_map do |contributor, idx|
11✔
119
          new_individual_contributor(contributor[:given_name], contributor[:family_name], contributor[:orcid], contributor[:role], idx)
15✔
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?
15✔
125
        PDCMetadata::Creator.new_individual_contributor(given_name, family_name, orcid, type, sequence)
5✔
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|
13✔
133
          value = contributor["value"]
11✔
134
          ror = contributor["ror"]
11✔
135
          type = contributor["type"]
11✔
136
          new_organizational_contributor(value, ror, type)
11✔
137
        end
138
      end
139

140
      def new_organizational_contributor(value, ror, type)
1✔
141
        return if value.blank? && ror.blank?
11✔
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|
13✔
149
          new_funder(funder[:ror], funder[:funder_name], funder[:award_number], funder[:award_uri])
15✔
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?
15✔
155
        PDCMetadata::Funder.new(ror, funder_name, award_number, award_uri)
8✔
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