File tree Expand file tree Collapse file tree 3 files changed +39
-9
lines changed
Expand file tree Collapse file tree 3 files changed +39
-9
lines changed Original file line number Diff line number Diff 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
153151end
Original file line number Diff line number Diff line change 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
1016end
Original file line number Diff line number Diff line change 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
You can’t perform that action at this time.
0 commit comments