From 29324a1c18e6c0aad544d72a8e2e10d349e9fc75 Mon Sep 17 00:00:00 2001 From: Martin Fenner Date: Sun, 4 Aug 2019 14:53:17 +0200 Subject: [PATCH] enforce doi types. #331 --- app/models/doi.rb | 12 ++++++++++++ app/serializers/doi_serializer.rb | 16 ++++++++++++---- spec/models/doi_spec.rb | 2 +- 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/app/models/doi.rb b/app/models/doi.rb index 8c64cb405..e1a52f097 100644 --- a/app/models/doi.rb +++ b/app/models/doi.rb @@ -749,6 +749,18 @@ def content_url=(value) write_attribute(:content_url, Array.wrap(value)) end + def container=(value) + write_attribute(:container, value || {}) + end + + def types=(value) + write_attribute(:types, value || {}) + end + + def landing_page=(value) + write_attribute(:landing_page, value || {}) + end + def identifier normalize_doi(doi, sandbox: !Rails.env.production?) end diff --git a/app/serializers/doi_serializer.rb b/app/serializers/doi_serializer.rb index 19ea700c9..908494d42 100644 --- a/app/serializers/doi_serializer.rb +++ b/app/serializers/doi_serializer.rb @@ -29,9 +29,9 @@ class DoiSerializer Array.wrap(object.creators).map do |c| c["affiliation"] = Array.wrap(c["affiliation"]).map do |a| if params[:affiliation] - a.is_a?(Hash) ? a : { "name" => a }.compact + a else - a.is_a?(Hash) ? a["name"] : a + a["name"] end end.compact c @@ -44,9 +44,9 @@ class DoiSerializer Array.wrap(object.contributors).map do |c| c["affiliation"] = Array.wrap(c["affiliation"]).map do |a| if params[:affiliation] - a.is_a?(Hash) ? a : { "name" => a }.compact + a else - a.is_a?(Hash) ? a["name"] : a + a["name"] end end.compact c @@ -57,6 +57,14 @@ class DoiSerializer Array.wrap(object.rights_list) end + attribute :container do |object| + object.container || {} + end + + attribute :types do |object| + object.types || {} + end + attribute :state do |object| object.aasm_state end diff --git a/spec/models/doi_spec.rb b/spec/models/doi_spec.rb index 498425c0c..f5e39d499 100644 --- a/spec/models/doi_spec.rb +++ b/spec/models/doi_spec.rb @@ -642,7 +642,7 @@ let(:doi) { create(:doi, container: container)} it "convert" do - expect(Doi.convert_container_by_id(id: doi.id)).to eq(1) + expect(Doi.convert_container_by_id(id: doi.id)).to eq(0) end end