Skip to content

Commit

Permalink
support issn types. datacite/datacite#797
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed Aug 3, 2019
1 parent 89f4286 commit 49d9200
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 12 deletions.
2 changes: 1 addition & 1 deletion app/controllers/providers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ def safe_params
ActiveModelSerializers::Deserialization.jsonapi_parse!(
params,
only: [
:name, "displayName", :symbol, :description, :website, :joined, "organizationType", "focusArea", "consortiumLead", "systemEmail", "groupEmail", "isActive", "passwordInput", :country, "billingInformation",{ "billingInformation": ["postCode", :state, :city, :address, :department, :organization, :country]}, "rorId", "twitterHandle","memberType",
:name, "displayName", :symbol, :description, :website, :joined, "organizationType", "focusArea", "consortiumLead", "systemEmail", "groupEmail", "isActive", "passwordInput", :country, "billingInformation", { "billingInformation": ["postCode", :state, :city, :address, :department, :organization, :country]}, "rorId", "twitterHandle","memberType",
"technicalContact",{ "technicalContact": [:email, "givenName", "familyName"]},
"secondaryTechnicalContact",{ "secondaryTechnicalContact": [:email, "givenName", "familyName"]},
"secondaryBillingContact",{ "secondaryBillingContact": [:email, "givenName", "familyName"]},
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/repositories_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ def set_repository
def safe_params
fail JSON::ParserError, "You need to provide a payload following the JSONAPI spec" unless params[:data].present?
ActiveModelSerializers::Deserialization.jsonapi_parse!(
params, only: [:symbol, :name, "contactName", "contactEmail", :domains, :provider, :url, "repositoryType", { "repositoryType" => [] }, :description, :language, { language: [] }, "alternateName", :software, "targetId", "isActive", "passwordInput", "clientType", :re3data, :opendoar, :issn, { issn: [] }, :certificate, { certificate: [] }],
params, only: [:symbol, :name, "contactName", "contactEmail", :domains, :provider, :url, "repositoryType", { "repositoryType" => [] }, :description, :language, { language: [] }, "alternateName", :software, "targetId", "isActive", "passwordInput", "clientType", :re3data, :opendoar, :issn, { issn: [:issnl, :print, :electronic] }, :certificate, { certificate: [] }],
keys: { "contactName" => :contact_name, "contactEmail" => :contact_email, "targetId" => :target_id, "isActive" => :is_active, "passwordInput" => :password_input, "clientType" => :client_type, "alternateName" => :alternate_name, "repositoryType" => :repository_type }
)
end
Expand Down
19 changes: 15 additions & 4 deletions app/models/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@ class Client < ActiveRecord::Base
indexes :provider_id, type: :keyword
indexes :re3data_id, type: :keyword
indexes :opendoar_id, type: :integer
indexes :issn, type: :keyword
indexes :issn, type: :object, properties: {
issnl: { type: :keyword },
electronic: { type: :keyword },
print: { type: :keyword }}
indexes :prefix_ids, type: :keyword
indexes :name, type: :text, fields: { keyword: { type: "keyword" }, raw: { type: "text", analyzer: "string_lowercase", "fielddata": true }}
indexes :alternate_name, type: :text, fields: { keyword: { type: "keyword" }, raw: { type: "text", analyzer: "string_lowercase", "fielddata": true }}
Expand Down Expand Up @@ -115,7 +118,7 @@ def as_indexed_json(options={})
"provider_id" => provider_id,
"re3data_id" => re3data_id,
"opendoar_id" => opendoar_id,
"issn" => Array.wrap(issn),
"issn" => issn,
"prefix_ids" => prefix_ids,
"name" => name,
"alternate_name" => alternate_name,
Expand Down Expand Up @@ -261,7 +264,15 @@ def to_jsonapi

def check_issn
Array.wrap(issn).each do |i|
errors.add(:issn, "ISSN #{i} is in the wrong format.") unless /\A\d{4}(-)?\d{3}[0-9X]+\z/.match(i)
if i["issnl"].present?
errors.add(:issn, "ISSN-L #{i["issnl"]} is in the wrong format.") unless /\A\d{4}(-)?\d{3}[0-9X]+\z/.match(i["issnl"])
end
if i["electronic"].present?
errors.add(:issn, "ISSN (electronic) #{i["electronic"]} is in the wrong format.") unless /\A\d{4}(-)?\d{3}[0-9X]+\z/.match(i["electronic"])
end
if i["print"].present?
errors.add(:issn, "ISSN (print) #{i["print"]} is in the wrong format.") unless /\A\d{4}(-)?\d{3}[0-9X]+\z/.match(i["print"])
end
end
end

Expand Down Expand Up @@ -303,7 +314,7 @@ def set_defaults
self.contact_name = "" unless contact_name.present?
self.domains = "*" unless domains.present?
self.client_type = "repository" unless client_type.present?
self.issn = [] if issn.blank? || client_type == "repository"
self.issn = {} if issn.blank? || client_type == "repository"
self.certificate = [] if certificate.blank? || client_type == "periodical"
self.repository_type = [] if repository_type.blank? || client_type == "periodical"
self.is_active = is_active ? "\x01" : "\x00"
Expand Down
32 changes: 30 additions & 2 deletions app/models/doi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -588,9 +588,23 @@ def self.convert_affiliation_by_id(options={})
elsif c["affiliation"].is_a?(String)
c["affiliation"] = [{ "name" => c["affiliation"] }]
should_update = true
else c["affiliation"].is_a?(Hash)
elsif c["affiliation"].is_a?(Hash)
c["affiliation"] = Array.wrap(c["affiliation"])
should_update = true
elsif c["affiliation"].is_a?(Array)
c["affiliation"].map do |a|
if a.nil?
should_update = true

a
elsif a.is_a?(String)
should_update = true

{ "name" => a }
else
a
end
end.compact
end

c
Expand All @@ -602,9 +616,23 @@ def self.convert_affiliation_by_id(options={})
elsif c["affiliation"].is_a?(String)
c["affiliation"] = [{ "name" => c["affiliation"] }]
should_update = true
else c["affiliation"].is_a?(Hash)
elsif c["affiliation"].is_a?(Hash)
c["affiliation"] = Array.wrap(c["affiliation"])
should_update = true
elsif c["affiliation"].is_a?(Array)
c["affiliation"].map do |a|
if a.nil?
should_update = true

a
elsif a.is_a?(String)
should_update = true

{ "name" => a }
else
a
end
end.compact
end

c
Expand Down
8 changes: 4 additions & 4 deletions spec/models/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,21 @@
let(:client) { build(:client, provider: provider, client_type: "periodical") }

it "should support issn" do
client.issn = ["1544-9173"]
client.issn = { "issnl" => "1544-9173" }
expect(client.save).to be true
expect(client.errors.details).to be_empty
end

it "should support multiple issn" do
client.issn = ["1544-9173", "1545-7885"]
client.issn = { "electronic" => "1544-9173", "print" => "1545-7885" }
expect(client.save).to be true
expect(client.errors.details).to be_empty
end

it "should reject invalid issn" do
client.issn = ["1544-91XX"]
client.issn = { "issnl" => "1544-91XX" }
expect(client.save).to be false
expect(client.errors.details).to eq(:issn=>[{:error=>"ISSN 1544-91XX is in the wrong format."}])
expect(client.errors.details).to eq(:issn=>[{:error=>"ISSN-L 1544-91XX is in the wrong format."}])
end
end

Expand Down

0 comments on commit 49d9200

Please sign in to comment.