From c80f779c46fb4dfc31aa3628d57c02942228d733 Mon Sep 17 00:00:00 2001 From: Martin Fenner Date: Wed, 22 Jul 2020 22:50:15 +0200 Subject: [PATCH] fix schema_org type --- app/models/doi.rb | 15 +++++++++++++++ lib/tasks/doi.rake | 10 ++++++++++ spec/models/doi_spec.rb | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+) diff --git a/app/models/doi.rb b/app/models/doi.rb index cb99d297b..d7595d227 100644 --- a/app/models/doi.rb +++ b/app/models/doi.rb @@ -128,6 +128,7 @@ class Doi < ActiveRecord::Base before_validation :update_language, if: :language? before_validation :update_rights_list, if: :rights_list? before_validation :update_identifiers + before_validation :update_types before_save :set_defaults, :save_metadata before_create { self.created = Time.zone.now.utc.iso8601 } @@ -1963,6 +1964,20 @@ def update_identifiers self.identifiers = Array.wrap(identifiers).select { |i| i["identifierType"] != "DOI" } end + def update_types + return nil unless types.is_a?(Hash) + + res = types["resourceType"].to_s.underscore.camelcase + resgen = types["resourceTypeGeneral"].to_s.dasherize + schema_org = Bolognese::Utils::CR_TO_SO_TRANSLATIONS[res] || Bolognese::Utils::DC_TO_SO_TRANSLATIONS[resgen] || "CreativeWork" + + self.types = types.reverse_merge( + "schemaOrg" => schema_org, + "citeproc" => Bolognese::Utils::CR_TO_CP_TRANSLATIONS[res] || Bolognese::Utils::SO_TO_CP_TRANSLATIONS[schema_org] || "article", + "bibtex" => Bolognese::Utils::CR_TO_BIB_TRANSLATIONS[res] || Bolognese::Utils::SO_TO_BIB_TRANSLATIONS[schema_org] || "misc", + "ris" => Bolognese::Utils::CR_TO_RIS_TRANSLATIONS[res] || Bolognese::Utils::DC_TO_RIS_TRANSLATIONS[resgen] || "GEN").compact + end + def self.repair_landing_page(id: nil) if id.blank? Rails.logger.error "[Error] No id provided." diff --git a/lib/tasks/doi.rake b/lib/tasks/doi.rake index bb863d7c8..377a896ed 100644 --- a/lib/tasks/doi.rake +++ b/lib/tasks/doi.rake @@ -149,6 +149,16 @@ namespace :doi do Doi.loop_through_dois(options) end + desc "Set types" + task set_types: :environment do + options = { + query: "types.resourceTypeGeneral:* AND -types.schemaOrg:*", + label: "[SetTypes]", + job_name: "UpdateDoiJob", + } + Doi.loop_through_dois(options) + end + desc 'Convert affiliations to new format' task :convert_affiliations => :environment do from_id = (ENV['FROM_ID'] || Doi.minimum(:id)).to_i diff --git a/spec/models/doi_spec.rb b/spec/models/doi_spec.rb index 26973c403..7a1775eb5 100644 --- a/spec/models/doi_spec.rb +++ b/spec/models/doi_spec.rb @@ -492,6 +492,44 @@ end end + describe "types" do + let(:doi) { build(:doi) } + + it "string" do + doi.types = "Dataset" + expect(doi.save).to be false + expect(doi.errors.details).to eq(:types=>[{:error=>"Types 'Dataset' should be an object instead of a string."}]) + end + + it "only resource_type_general" do + doi.types = { "resourceTypeGeneral" => "Dataset" } + expect(doi.save).to be true + expect(doi.errors.details).to be_empty + expect(doi.types).to eq("bibtex"=>"misc", "citeproc"=>"dataset", "resourceTypeGeneral"=>"Dataset", "ris"=>"DATA", "schemaOrg"=>"Dataset") + end + + it "resource_type and resource_type_general" do + doi.types = { "resourceTypeGeneral" => "Dataset", "resourceType" => "EEG data"} + expect(doi.save).to be true + expect(doi.errors.details).to be_empty + expect(doi.types).to eq("bibtex"=>"misc", "citeproc"=>"dataset", "resourceTypeGeneral"=>"Dataset", "resourceType" => "EEG data", "ris"=>"DATA", "schemaOrg"=>"Dataset") + end + + it "resource_type_general and different schema_org" do + doi.types = { "resourceTypeGeneral" => "Dataset", "schemaOrg" => "JournalArticle" } + expect(doi.save).to be true + expect(doi.errors.details).to be_empty + expect(doi.types).to eq("bibtex"=>"misc", "citeproc"=>"dataset", "resourceTypeGeneral"=>"Dataset", "ris"=>"DATA", "schemaOrg"=>"JournalArticle") + end + + it "resource_type_general and different ris" do + doi.types = { "resourceTypeGeneral" => "Dataset", "ris" => "GEN" } + expect(doi.save).to be true + expect(doi.errors.details).to be_empty + expect(doi.types).to eq("bibtex"=>"misc", "citeproc"=>"dataset", "resourceTypeGeneral"=>"Dataset", "ris"=>"GEN", "schemaOrg"=>"Dataset") + end + end + # describe "related_identifiers" do # it "has part" do # subject = build(:doi, related_identifiers: [ {