Skip to content

Commit 8125c8b

Browse files
committed
DRY up some ORCID handling code
1 parent 0892030 commit 8125c8b

File tree

5 files changed

+40
-39
lines changed

5 files changed

+40
-39
lines changed

app/models/concerns/has_orcid.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module HasOrcid
2+
extend ActiveSupport::Concern
3+
4+
included do
5+
auto_strip_attributes :orcid
6+
before_validation :normalize_orcid
7+
end
8+
9+
def orcid_url
10+
return nil if orcid.blank?
11+
"#{OrcidValidator::ORCID_PREFIX}#{orcid}"
12+
end
13+
14+
def normalize_orcid
15+
return if orcid.blank?
16+
self.orcid = orcid.strip.sub(OrcidValidator::ORCID_DOMAIN_REGEX, '')
17+
end
18+
end

app/models/person.rb

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
class Person < ApplicationRecord
2+
include HasOrcid
3+
24
belongs_to :profile, optional: true
35
has_many :person_links, dependent: :destroy
46

@@ -23,16 +25,11 @@ def name_presence
2325

2426
# Automatically link to a Profile if one exists with a matching ORCID
2527
def link_to_profile_by_orcid
26-
return if orcid.blank?
27-
return if profile_id.present? # Already linked
28-
29-
# Normalize the ORCID for matching - Profile stores it as a full URL
30-
normalized_orcid = orcid.strip
31-
if normalized_orcid =~ OrcidValidator::ORCID_ID_REGEX
32-
normalized_orcid = "#{OrcidValidator::ORCID_PREFIX}#{normalized_orcid}"
28+
if orcid.blank?
29+
self.profile = nil
30+
else
31+
matching_profile = Profile.find_by(orcid: orcid)
32+
self.profile = matching_profile if matching_profile.present?
3333
end
34-
35-
matching_profile = Profile.find_by(orcid: normalized_orcid)
36-
self.profile = matching_profile if matching_profile.present?
3734
end
3835
end

app/models/profile.rb

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
require 'uri'
22

33
class Profile < ApplicationRecord
4-
auto_strip_attributes :firstname, :surname, :website, :orcid, squish: false
4+
include HasOrcid
5+
6+
auto_strip_attributes :firstname, :surname, :website, squish: false
57
belongs_to :user, inverse_of: :profile
68

79
before_validation :normalize_orcid
@@ -25,11 +27,6 @@ def full_name
2527
"#{firstname} #{surname}".strip
2628
end
2729

28-
def orcid_url
29-
return nil if orcid.blank?
30-
"#{OrcidValidator::ORCID_PREFIX}#{orcid}"
31-
end
32-
3330
def merge(*others)
3431
Profile.transaction do
3532
attrs = attributes
@@ -66,11 +63,6 @@ def authenticate_orcid(orcid)
6663

6764
private
6865

69-
def normalize_orcid
70-
return if orcid.blank?
71-
self.orcid = orcid.strip.sub(OrcidValidator::ORCID_DOMAIN_REGEX, '')
72-
end
73-
7466
def check_public
7567
public ? self.type = 'Trainer' : self.type = 'Profile'
7668
end

lib/bioschemas/generator.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,5 +166,15 @@ def self.provider(resource)
166166

167167
p.any? ? p : nil
168168
end
169+
170+
def self.people(people)
171+
people.map do |person|
172+
person_hash = { '@type' => 'Person', 'name' => person.display_name }
173+
person_hash['givenName'] = person.given_name if person.given_name.present?
174+
person_hash['familyName'] = person.family_name if person.family_name.present?
175+
person_hash['@id'] = person.orcid_url if person.orcid.present?
176+
person_hash
177+
end
178+
end
169179
end
170180
end

lib/bioschemas/learning_resource_generator.rb

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,8 @@ def self.bioschemas_profile
1515
property :version, :version
1616
property :description, :description
1717
property :keywords, :keywords
18-
property :author, -> (material) {
19-
material.authors.map { |a|
20-
author_hash = { "@type" => "Person", "name" => a.display_name }
21-
author_hash["givenName"] = a.given_name if a.given_name.present?
22-
author_hash["familyName"] = a.family_name if a.family_name.present?
23-
author_hash["@id"] = "https://orcid.org/#{a.orcid}" if a.orcid.present?
24-
author_hash
25-
}
26-
}
27-
property :contributor, -> (material) {
28-
material.contributors.map { |c|
29-
contributor_hash = { "@type" => "Person", "name" => c.display_name }
30-
contributor_hash["givenName"] = c.given_name if c.given_name.present?
31-
contributor_hash["familyName"] = c.family_name if c.family_name.present?
32-
contributor_hash["@id"] = "https://orcid.org/#{c.orcid}" if c.orcid.present?
33-
contributor_hash
34-
}
35-
}
18+
property :author, -> (material) { people(material.authors) }
19+
property :contributor, -> (material) { people(material.contributors) }
3620
property :provider, -> (material) { provider(material) }
3721
property :audience, -> (material) {
3822
material.target_audience.map { |audience| { '@type' => 'Audience', 'audienceType' => audience } }

0 commit comments

Comments
 (0)