Skip to content

Commit

Permalink
Find existing taxa in NZORTaxonAdapter, validate unique source identi…
Browse files Browse the repository at this point in the history
…fiers.
  • Loading branch information
Kale Worsley authored and kueda committed Sep 10, 2012
1 parent a3449de commit 4f60606
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
4 changes: 4 additions & 0 deletions app/models/taxon.rb
Expand Up @@ -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',
Expand Down Expand Up @@ -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
)
Expand Down
4 changes: 4 additions & 0 deletions app/models/taxon_name.rb
Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion vendor/plugins/ratatosk/lib/ratatosk/name_providers/nzor.rb
Expand Up @@ -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')
Expand Down

0 comments on commit 4f60606

Please sign in to comment.