-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1049 from datacite/11-30-Add_person_id_to_doi_model
Add person_id to doi model
- Loading branch information
Showing
5 changed files
with
235 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# frozen_string_literal: true | ||
|
||
module Doi::Indexer | ||
class RelatedDoiIndexer | ||
def initialize(related_identifiers) | ||
@related_identifiers = related_identifiers | ||
@related_dois = nil | ||
end | ||
|
||
def related_dois | ||
@related_dois ||= @related_identifiers.select { |r| r["relatedIdentifierType"] == "DOI" } | ||
end | ||
|
||
def related_grouped_by_id | ||
related_dois.group_by { |r| r["relatedIdentifier"].downcase } | ||
end | ||
|
||
def relation_types_gouped_by_id | ||
related_grouped_by_id.transform_values do |values| | ||
values.map { |val| val["relationType"].underscore }.uniq | ||
end | ||
end | ||
|
||
def related_doi_ids | ||
related_grouped_by_id.keys | ||
end | ||
|
||
def dois | ||
Doi.where(doi: related_doi_ids) | ||
end | ||
|
||
def indexed_dois | ||
dois.map { |d| RelatedIdentifierDenormalizer.new(d).to_hash } | ||
end | ||
|
||
def as_indexed_json | ||
rtypes = relation_types_gouped_by_id | ||
indexed_dois.map do |indexed_doi| | ||
doi = indexed_doi["doi"] | ||
indexed_doi["relation_type"] = rtypes[doi] | ||
indexed_doi | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# frozen_string_literal: true | ||
|
||
module Doi::Indexer | ||
class RelatedIdentifierDenormalizer | ||
attr_reader :related_doi | ||
|
||
def initialize(doi) | ||
@related_doi = doi | ||
end | ||
|
||
def to_hash | ||
%w[ | ||
client_id | ||
doi | ||
organization_id | ||
person_id | ||
resource_type_id | ||
resource_type_id_and_name | ||
].index_with { |method_name| send(method_name) } | ||
end | ||
|
||
delegate :resource_type_id, to: :related_doi | ||
delegate :resource_type_id_and_name, to: :related_doi | ||
delegate :organization_id, to: :related_doi | ||
delegate :person_id, to: :related_doi | ||
delegate :client_id, to: :related_doi | ||
|
||
def doi | ||
@related_doi.doi.downcase | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
|
||
# frozen_string_literal: true | ||
|
||
require "rails_helper" | ||
|
||
describe Doi, type: :model, vcr: true, elasticsearch: true do | ||
describe "related_doi" do | ||
let(:client) { create(:client) } | ||
let(:target_doi) do | ||
create(:doi, | ||
client: client, | ||
aasm_state: "findable", | ||
types: { "resourceTypeGeneral" => "Dataset" } | ||
) | ||
end | ||
let(:doi) do | ||
create(:doi, | ||
client: client, | ||
aasm_state: "findable", | ||
related_identifiers: [ | ||
{ | ||
"relatedIdentifier": target_doi.doi, | ||
"relatedIdentifierType": "DOI", | ||
"relationType": "HasPart", | ||
"resourceTypeGeneral": "OutputManagementPlan", | ||
}, | ||
{ | ||
"relatedIdentifier": target_doi.doi, | ||
"relatedIdentifierType": "DOI", | ||
"relationType": "Cites", | ||
"resourceTypeGeneral": "Text", | ||
} | ||
]) | ||
end | ||
|
||
it "indexes related_dois" do | ||
expect(doi.related_dois.first["doi"]).to eq(target_doi.doi.downcase) | ||
end | ||
|
||
it "indexes related doi's client_id" do | ||
expect(doi.related_dois.first["client_id"]).to eq(target_doi.client_id) | ||
end | ||
|
||
it "indexes related doi's person_id" do | ||
expect(doi.related_dois.first["person_id"]).to eq(target_doi.person_id) | ||
end | ||
|
||
it "does not index related doi's claimed resource_type_id" do | ||
expect(doi.related_dois.first["resource_type_id"]).not_to eq("output_management_plan") | ||
end | ||
|
||
it "indexes related doi's true resource_type_id" do | ||
expect(doi.related_dois.first["resource_type_id"]).to eq("dataset") | ||
end | ||
|
||
it "indexes all relations to the related doi" do | ||
expect(doi.related_dois.first["relation_type"]).to eq(["has_part", "cites"]) | ||
end | ||
end | ||
end |