Skip to content

Commit

Permalink
Merge pull request #1937 from seek4science/link-creators-by-orcid
Browse files Browse the repository at this point in the history
Automatically link creators to SEEK People based on matching ORCID
  • Loading branch information
fbacall authored Jul 4, 2024
2 parents 6581e0d + ee17e7b commit df2ac8b
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 5 deletions.
8 changes: 8 additions & 0 deletions app/models/assets_creator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class AssetsCreator < ApplicationRecord
include Seek::OrcidSupport
include Seek::BioSchema::Support

before_validation :associate_creator_from_orcid

update_rdf_on_change :asset

default_scope { order(:pos) }
Expand Down Expand Up @@ -74,4 +76,10 @@ def rdf_resource
end
end

private

def associate_creator_from_orcid
return if orcid.blank? || creator.present?
self.creator = Person.find_by_orcid(orcid)
end
end
4 changes: 2 additions & 2 deletions test/functional/data_files_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4236,7 +4236,7 @@ def register_content_blob(skip_provide_metadata:false)
creators: [someone],
contributor: FactoryBot.create(:person, first_name: 'Joe', last_name: 'Bloggs', orcid: 'https://orcid.org/0000-0002-1694-233X')
).latest_version
thing.assets_creators.create!(given_name: 'Phil', family_name: 'Collins', orcid: 'https://orcid.org/0000-0002-1694-233X')
thing.assets_creators.create!(given_name: 'Phil', family_name: 'Collins', orcid: 'https://orcid.org/0000-0001-9842-9718')

get :show, params: { id: thing.parent.id, version: thing.version, format: :datacite_xml }

Expand All @@ -4252,7 +4252,7 @@ def register_content_blob(skip_provide_metadata:false)
phil = parsed.xpath("//xmlns:resource/xmlns:creators/xmlns:creator[xmlns:creatorName/text()='Collins, Phil']").first
jane = parsed.xpath("//xmlns:resource/xmlns:creators/xmlns:creator[xmlns:creatorName/text()='Bloggs, Jane']").first
assert_equal 'Collins, Phil', phil.xpath('./xmlns:creatorName').first.text
assert_equal 'https://orcid.org/0000-0002-1694-233X', phil.xpath('./xmlns:nameIdentifier').first.text
assert_equal 'https://orcid.org/0000-0001-9842-9718', phil.xpath('./xmlns:nameIdentifier').first.text
assert_equal 'Bloggs, Jane', jane.xpath('./xmlns:creatorName').first.text
assert_nil jane.xpath('./xmlns:nameIdentifier').first
assert_equal 'ORCID', resource.xpath('./xmlns:creators/xmlns:creator/xmlns:nameIdentifier/@nameIdentifierScheme').first.text
Expand Down
28 changes: 28 additions & 0 deletions test/unit/assets_creators_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -173,4 +173,32 @@ def teardown
end
end
end

test 'automatically links to creator based on orcid' do
explicitly_linked_person = FactoryBot.create(:person, first_name: 'Link', orcid: 'https://orcid.org/0000-0002-5111-7263')
orcid_linked_person = FactoryBot.create(:person_not_in_project, first_name: 'Orc', orcid: 'https://orcid.org/0000-0002-1825-0097')
should_not_be_linked = FactoryBot.create(:person_not_in_project, orcid: 'https://orcid.org/0000-0001-9842-9718')

sop = FactoryBot.create(:sop)

disable_authorization_checks do
assert_difference('AssetsCreator.count', 2) do
sop.update(assets_creators_attributes: {
'4634' => {
orcid: 'https://orcid.org/0000-0001-9842-9718',
creator_id: explicitly_linked_person.id
},
'123' => {
orcid: 'https://orcid.org/0000-0002-1825-0097'
}
})

ac = sop.reload.assets_creators.to_a
assert_equal 2, ac.length
assert_equal orcid_linked_person, ac.detect { |a| a.given_name == 'Orc' }.creator
assert_equal explicitly_linked_person, ac.detect { |a| a.given_name == 'Link' }.creator,
'If creator is explicitly set, it should not attempt to link a different creator via orcid'
end
end
end
end
10 changes: 7 additions & 3 deletions test/unit/datacite_metadata_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ class DataciteMetadataTest < ActiveSupport::TestCase
creators: [someone],
contributor: FactoryBot.create(:person, first_name: 'Joe', last_name: 'Bloggs', orcid: 'https://orcid.org/0000-0002-1694-233X')
).latest_version
thing.assets_creators.create!(given_name: 'Phil', family_name: 'Collins', orcid: 'https://orcid.org/0000-0002-1694-233X')
thing.assets_creators.create!(given_name: 'Phil', family_name: 'Collins', orcid: 'https://orcid.org/0000-0001-9842-9718')
thing.assets_creators.create!(orcid: 'https://orcid.org/0000-0002-1694-233X')

metadata = thing.datacite_metadata
xml = metadata.build
Expand All @@ -93,13 +94,16 @@ class DataciteMetadataTest < ActiveSupport::TestCase
resource = parsed.xpath('//xmlns:resource').first
assert_equal 'The title', resource.xpath('./xmlns:titles/xmlns:title').first.text
assert_equal 'The description', resource.xpath('./xmlns:descriptions/xmlns:description').first.text
assert_equal 2, resource.xpath('./xmlns:creators/xmlns:creator').length
assert_equal 3, resource.xpath('./xmlns:creators/xmlns:creator').length
phil = parsed.xpath("//xmlns:resource/xmlns:creators/xmlns:creator[xmlns:creatorName/text()='Collins, Phil']").first
jane = parsed.xpath("//xmlns:resource/xmlns:creators/xmlns:creator[xmlns:creatorName/text()='Bloggs, Jane']").first
joe = parsed.xpath("//xmlns:resource/xmlns:creators/xmlns:creator[xmlns:creatorName/text()='Bloggs, Joe']").first
assert_equal 'Collins, Phil', phil.xpath('./xmlns:creatorName').first.text
assert_equal 'https://orcid.org/0000-0002-1694-233X', phil.xpath('./xmlns:nameIdentifier').first.text
assert_equal 'https://orcid.org/0000-0001-9842-9718', phil.xpath('./xmlns:nameIdentifier').first.text
assert_equal 'Bloggs, Jane', jane.xpath('./xmlns:creatorName').first.text
assert_nil jane.xpath('./xmlns:nameIdentifier').first
assert_equal 'Bloggs, Joe', joe.xpath('./xmlns:creatorName').first.text
assert_equal 'https://orcid.org/0000-0002-1694-233X', joe.xpath('./xmlns:nameIdentifier').first.text
assert_equal 'ORCID', resource.xpath('./xmlns:creators/xmlns:creator/xmlns:nameIdentifier/@nameIdentifierScheme').first.text
assert_equal 'https://orcid.org', resource.xpath('./xmlns:creators/xmlns:creator/xmlns:nameIdentifier/@schemeURI').first.text
assert_equal thing.created_at.year.to_s, resource.xpath('./xmlns:publicationYear').first.text
Expand Down

0 comments on commit df2ac8b

Please sign in to comment.