Skip to content

Commit e12439f

Browse files
Add doi specs
1 parent 0bd6e88 commit e12439f

File tree

3 files changed

+39
-9
lines changed

3 files changed

+39
-9
lines changed

app/models/concerns/event_index_handler.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,6 @@ def cache_key
146146
end
147147

148148
def date_published(doi)
149-
item = Doi.find_by(doi: DoiUtilities.uppercase_doi_from_url(doi))
150-
151-
item[:publication_date] if item.present?
149+
Doi.publication_date(doi)
152150
end
153151
end

app/models/doi.rb

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
# frozen_string_literal: true
22

3-
class Doi < ApplicationRecord
4-
# Map the doi model to the dataset table in the datacite mysql database
5-
self.table_name = "dataset"
3+
class Doi
4+
class << self
5+
def publication_date(doi)
6+
search_doi = DoiUtilities.uppercase_doi_from_url(doi)
67

7-
# Attributes
8-
attribute :doi, :string
9-
attribute :publication_year, :integer
8+
return nil if search_doi.blank?
9+
10+
sql = "SELECT publication_year FROM dataset WHERE doi = :doi LIMIT 1"
11+
sanitized_sql = ActiveRecord::Base.send(:sanitize_sql_array, [sql, { doi: search_doi }])
12+
13+
ActiveRecord::Base.connection.select_one(sanitized_sql)
14+
end
15+
end
1016
end

spec/models/doi_spec.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# frozen_string_literal: true
2+
3+
require "rails_helper"
4+
5+
RSpec.describe(Doi, type: :model) do
6+
let(:valid_doi) { "10.0000/ZENODO.00000000" }
7+
let(:invalid_doi) { "invalid-doi" }
8+
9+
describe "#publication_date" do
10+
it "returns nil when the doi is invalid" do
11+
expect(described_class.publication_date(invalid_doi)).to(be_nil)
12+
end
13+
14+
it "returns nil when no doi is found" do
15+
allow(ActiveRecord::Base.connection).to(receive(:select_one).and_return(nil))
16+
17+
expect(described_class.publication_date(valid_doi)).to(be_nil)
18+
end
19+
20+
it "returns the publication date(just the year) when the doi is found" do
21+
allow(ActiveRecord::Base.connection).to(receive(:select_one).and_return(2025))
22+
23+
expect(described_class.publication_date(valid_doi)).to(eq(2025))
24+
end
25+
end
26+
end

0 commit comments

Comments
 (0)