diff --git a/app/models/doi.rb b/app/models/doi.rb index c6faab398..ac7915154 100644 --- a/app/models/doi.rb +++ b/app/models/doi.rb @@ -1940,8 +1940,7 @@ def check_container end def check_language - entry = ISO_639.find_by_code(language) || ISO_639.find_by_english_name(language.upcase_first) - errors.add(:language, "Language #{language} not found.") if entry.blank? + errors.add(:language, "Language #{language} is in an invalid format.") if !language.match?(/^[a-zA-Z]{1,8}(-[a-zA-Z0-9]{1,8})*$/) end # To be used for isolated clean up of errored individual DOIs @@ -2218,9 +2217,12 @@ def update_agency def update_language lang = language.to_s.split("-").first entry = ISO_639.find_by_code(lang) || ISO_639.find_by_english_name(lang.upcase_first) - self.language = if entry.present? && entry.alpha2.present? - entry.alpha2 - end + self.language = + if entry.present? && entry.alpha2.present? + entry.alpha2 + elsif language.match?(/^[a-zA-Z]{1,8}(-[a-zA-Z0-9]{1,8})*$/) + language + end end def update_field_of_science diff --git a/spec/models/doi_spec.rb b/spec/models/doi_spec.rb index a5ef79d27..6085ac247 100644 --- a/spec/models/doi_spec.rb +++ b/spec/models/doi_spec.rb @@ -298,10 +298,31 @@ expect(doi.language).to eq("fr") end - it "error" do + it "human longer than 8 characters" do + doi.language = "Indonesian" + expect(doi.save).to be true + expect(doi.errors.details).to be_empty + expect(doi.language).to eq("id") + end + + it "non-iso 639-1" do doi.language = "hhh" expect(doi.save).to be true expect(doi.errors.details).to be_empty + expect(doi.language).to eq("hhh") + end + + it "non-iso 639-1 with country code" do + doi.language = "prq-PE" + expect(doi.save).to be true + expect(doi.errors.details).to be_empty + expect(doi.language).to eq("prq-PE") + end + + it "fails xs:language regex" do + doi.language = "Borgesian" + expect(doi.save).to be true + expect(doi.errors.details).to be_empty expect(doi.language).to be_nil end