diff --git a/app/models/doi.rb b/app/models/doi.rb index 96ae126f9..71cbb8368 100644 --- a/app/models/doi.rb +++ b/app/models/doi.rb @@ -1273,11 +1273,11 @@ def downloads_over_time end def reference_ids - reference_events.pluck(:target_doi).compact.uniq.map(&:downcase) + reference_events.pluck(:target_doi).compact.map(&:downcase).uniq end def reference_count - reference_events.pluck(:target_doi).uniq.length + reference_events.pluck(:target_doi).compact.map(&:downcase).uniq.length end def indexed_references @@ -1285,18 +1285,18 @@ def indexed_references end def citation_ids - citation_events.pluck(:source_doi).compact.uniq.map(&:downcase) + citation_events.pluck(:source_doi).compact.map(&:downcase).uniq end # remove duplicate citing source dois def citation_count - citation_events.pluck(:source_doi).uniq.length + citation_events.pluck(:source_doi).compact.map(&:downcase).uniq.length end # remove duplicate citing source dois, # then show distribution by year def citations_over_time - citation_events.pluck(:occurred_at, :source_doi).uniq { |v| v[1] }. + citation_events.pluck(:occurred_at, :source_doi).map { |v| [v[0], v[1].downcase] }.sort_by { |v| v[0] }.uniq { |v| v[1] }. group_by { |v| v[0].utc.iso8601[0..3] }. map { |k, v| { "year" => k, "total" => v.length } }. sort_by { |h| h["year"] } @@ -1307,11 +1307,11 @@ def indexed_citations end def part_ids - part_events.pluck(:target_doi).compact.uniq.map(&:downcase) + part_events.pluck(:target_doi).compact.map(&:downcase).uniq end def part_count - part_events.pluck(:target_doi).uniq.length + part_events.pluck(:target_doi).compact.map(&:downcase).uniq.length end def indexed_parts @@ -1319,11 +1319,11 @@ def indexed_parts end def part_of_ids - part_of_events.pluck(:source_doi).compact.uniq.map(&:downcase) + part_of_events.pluck(:source_doi).compact.map(&:downcase).uniq end def part_of_count - part_of_events.pluck(:source_doi).uniq.length + part_of_events.pluck(:source_doi).compact.map(&:downcase).uniq.length end def indexed_part_of @@ -1331,11 +1331,11 @@ def indexed_part_of end def version_ids - version_events.pluck(:target_doi).compact.uniq.map(&:downcase) + version_events.pluck(:target_doi).compact.map(&:downcase).uniq end def version_count - version_events.pluck(:target_doi).uniq.length + version_events.pluck(:target_doi).compact.map(&:downcase).uniq.length end def indexed_versions @@ -1343,11 +1343,11 @@ def indexed_versions end def version_of_ids - version_of_events.pluck(:source_doi).compact.uniq.map(&:downcase) + version_of_events.pluck(:source_doi).compact.map(&:downcase).uniq end def version_of_count - version_of_events.pluck(:source_doi).uniq.length + version_of_events.pluck(:source_doi).compact.map(&:downcase).uniq.length end def indexed_version_of @@ -1361,11 +1361,11 @@ def other_relation_events def other_relation_ids other_relation_events.map do |e| e.doi - end.flatten.uniq - [doi.downcase] + end.flatten.compact.map(&:downcase).uniq - [doi.downcase] end def other_relation_count - other_relation_ids.length + other_relation_ids.compact.map(&:downcase).length end def xml_encoded diff --git a/spec/models/doi_related_spec.rb b/spec/models/doi_related_spec.rb index 402e9204d..0e59658c4 100644 --- a/spec/models/doi_related_spec.rb +++ b/spec/models/doi_related_spec.rb @@ -83,10 +83,30 @@ let(:client) { create(:client) } let(:doi) { create(:doi, client: client, aasm_state: "findable") } let(:target_doi) { create(:doi, client: client, aasm_state: "findable") } - let!(:reference_events) { create(:event_for_crossref, subj_id: "https://doi.org/#{doi.doi}", obj_id: "https://doi.org/#{target_doi.doi}", relation_type_id: "references") } + let!(:reference_event) do + create(:event_for_crossref, { + subj_id: "https://doi.org/#{doi.doi}", + obj_id: "https://doi.org/#{target_doi.doi}", + relation_type_id: "references", + occurred_at: "2015-06-13T16:14:19Z", + }) + end + let!(:reference_event2) do + create(:event_for_crossref, { + subj_id: "https://doi.org/#{target_doi.doi}", + obj_id: "https://doi.org/#{doi.doi}", + occurred_at: "2016-06-13T16:14:19Z", + relation_type_id: "is-referenced-by", + }) + end it "has references" do - expect(doi.references.count).to eq(1) + # Some older events have downcased source_doi and target_doi + reference_event2.target_doi = target_doi.doi.downcase + reference_event2.source_doi = doi.doi.downcase + reference_event2.save + + expect(doi.references.count).to eq(2) expect(doi.reference_ids.count).to eq(1) expect(doi.reference_count).to eq(1) @@ -124,10 +144,24 @@ occurred_at: "2016-06-13T16:14:19Z" }) end + let!(:citation_event4) do + create(:event_for_datacite_crossref, { + subj_id: "https://doi.org/#{source_doi2.doi}", + obj_id: "https://doi.org/#{doi.doi}", + relation_type_id: "cites", + source_id: "crossref", + occurred_at: "2017-06-13T16:14:19Z" + }) + end # removing duplicate dois in citation_ids, citation_count and citations_over_time (different relation_type_id) it "has citations" do - expect(doi.citations.count).to eq(3) + # Some older events have downcased source_doi and target_doi + citation_event4.source_doi = source_doi2.doi.downcase + citation_event4.target_doi = doi.doi.downcase + citation_event4.save + + expect(doi.citations.count).to eq(4) expect(doi.citation_ids.count).to eq(2) expect(doi.citation_count).to eq(2) expect(doi.citations_over_time).to eq([{ "total" => 1, "year" => "2015" }, { "total" => 1, "year" => "2016" }])