From c20ea80649bfa72d187fa573d6a6d4ce2f0e0598 Mon Sep 17 00:00:00 2001 From: Martin Fenner Date: Thu, 1 Aug 2019 10:47:30 +0200 Subject: [PATCH] added date validation, using edft gem. #326 --- app/models/doi.rb | 7 +++++++ spec/models/doi_spec.rb | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/app/models/doi.rb b/app/models/doi.rb index 3ca39db27..5dd0341f3 100644 --- a/app/models/doi.rb +++ b/app/models/doi.rb @@ -83,6 +83,7 @@ class Doi < ActiveRecord::Base validates_uniqueness_of :doi, message: "This DOI has already been taken", unless: :only_validate validates :last_landing_page_status, numericality: { only_integer: true }, if: :last_landing_page_status? validates :xml, presence: true, xml_schema: true, if: Proc.new { |doi| doi.validatable? } + validate :check_dates, if: :dates? after_commit :update_url, on: [:create, :update] after_commit :update_media, on: [:create, :update] @@ -743,6 +744,12 @@ def event=(value) self.send(value) if %w(register publish hide show).include?(value) end + def check_dates + Array.wrap(dates).each do |d| + errors.add(:dates, "Date #{d["date"]} is not in a supported format.") unless Date.edtf(d["date"]).present? + end + end + # to be used after DOIs were transferred to another DOI RA def self.delete_dois_by_prefix(prefix, options={}) logger = Logger.new(STDOUT) diff --git a/spec/models/doi_spec.rb b/spec/models/doi_spec.rb index 6b01ff43f..30330078e 100644 --- a/spec/models/doi_spec.rb +++ b/spec/models/doi_spec.rb @@ -216,6 +216,46 @@ # end end + describe "dates" do + let(:doi) { build(:doi) } + + it "full date" do + doi.dates = [{ "date" => "2019-08-01" }] + expect(doi.save).to be true + expect(doi.errors.details).to be_empty + end + + it "year-month" do + doi.dates = [{ "date" => "2019-08" }] + expect(doi.save).to be true + expect(doi.errors.details).to be_empty + end + + it "year" do + doi.dates = [{ "date" => "2019" }] + expect(doi.save).to be true + expect(doi.errors.details).to be_empty + end + + it "date range" do + doi.dates = [{ "date" => "2019-07-31/2019-08-01" }] + expect(doi.save).to be true + expect(doi.errors.details).to be_empty + end + + it "date range years" do + doi.dates = [{ "date" => "2018/2019" }] + expect(doi.save).to be true + expect(doi.errors.details).to be_empty + end + + it "invalid" do + doi.dates = [{ "date" => "08/01/2019" }] + expect(doi.save).to be false + expect(doi.errors.details).to eq(:dates=>[{:error=>"Date 08/01/2019 is not in a supported format."}]) + end + end + describe "metadata" do subject { create(:doi) }