diff --git a/app/models/taxon.rb b/app/models/taxon.rb index cc2757dd053..4aa673ba383 100644 --- a/app/models/taxon.rb +++ b/app/models/taxon.rb @@ -67,6 +67,9 @@ class Taxon < ActiveRecord::Base :unless => Proc.new { |taxon| (taxon.parent_id.nil? || !taxon.is_active)}, :message => "already used as a child of this " + "taxon's parent" + validates_uniqueness_of :source_identifier, + :scope => [:source_id], + :message => "already exists" NAME_PROVIDER_TITLES = { 'ColNameProvider' => 'Catalogue of Life', @@ -523,6 +526,7 @@ def set_scientific_taxon_name :source => source, :source_identifier => source_identifier, :source_url => source_url, + :name_provider => name_provider, :lexicon => TaxonName::LEXICONS[:SCIENTIFIC_NAMES], :is_valid => true ) diff --git a/app/models/taxon_name.rb b/app/models/taxon_name.rb index 4dae91977e7..4f941e8c3f3 100644 --- a/app/models/taxon_name.rb +++ b/app/models/taxon_name.rb @@ -10,6 +10,10 @@ class TaxonName < ActiveRecord::Base :scope => [:lexicon, :taxon_id], :message => "already exists for this taxon in this lexicon", :case_sensitive => false + validates_uniqueness_of :source_identifier, + :scope => [:source_id], + :message => "already exists" + #TODO is the validates uniqueness correct? Allows duplicate TaxonNames to be created with same #source_url but different taxon_ids before_validation :strip_tags, :strip_name, :remove_rank_from_name, :normalize_lexicon diff --git a/vendor/plugins/ratatosk/lib/ratatosk/name_providers/nzor.rb b/vendor/plugins/ratatosk/lib/ratatosk/name_providers/nzor.rb index f2d2a097fda..6035dbaf7ff 100644 --- a/vendor/plugins/ratatosk/lib/ratatosk/name_providers/nzor.rb +++ b/vendor/plugins/ratatosk/lib/ratatosk/name_providers/nzor.rb @@ -163,8 +163,14 @@ class NZORTaxonAdapter # Initialize with a Nokogiri object of a single CoL XML response # def initialize(hxml, params = {}) - @adaptee = Taxon.new(params) @hxml = hxml + existing = Taxon.find(:first, :conditions => {:source_id => Source.find_by_title('New Zealand Organisms Register').id, :source_identifier => @hxml.at('NameId').inner_text}) + @adaptee = if existing.present? + existing + else + Taxon.new(params) + end + @adaptee.name = @hxml.at('PartialName').inner_text @adaptee.rank = @hxml.at('Rank').inner_text.downcase @adaptee.source = Source.find_by_title('New Zealand Organisms Register')