app/models/taxon.rb
def merge(reject)
    raise "Can't merge a taxon with itself" if reject.id == self.id
    reject_taxon_names = reject.taxon_names.all
    reject_taxon_scheme_taxa = reject.taxon_scheme_taxa.all
    # otherwise it will screw up merge_has_many_associations
    TaxonAncestor.where(:taxon_id => reject.id).delete_all
    TaxonAncestor.where(:ancestor_taxon_id => reject.id).delete_all
    merge_has_many_associations(reject)
    
    # Merge ListRules and other polymorphic assocs
    ListRule.update_all(["operand_id = ?", id], ["operand_id = ? AND operand_type = ?", reject.id, Taxon.to_s])
    
    # Keep reject colors if keeper has none
    self.colors << reject.colors if colors.blank?
    
    # Move reject child taxa to the keeper
    reject.children.each {|child| child.move_to_child_of(self)}
    
    # Update or destroy merged taxon scheme taxa
    reject_taxon_scheme_taxa.each do |reject_tst|
      reject_tst.reload
      reject_tst_name = reject_tst.taxon_name
      if taxon_name = self.taxon_names.where(:lexicon => TaxonName::SCIENTIFIC_NAMES, :name => reject_tst_name.name).first
        reject_tst.update_attributes(:taxon_name_id => taxon_name.id)
      end
      unless reject_tst.valid?
        Rails.logger.info "[INFO] Destroying #{reject_tst} while merging taxon " + 
          "#{reject.id} into taxon #{id}: #{reject_tst.errors.full_messages.to_sentence}"
        reject_tst.destroy 
        next
      end
    end
    
    # Update or destroy merged taxon_names
    reject_taxon_names.each do |taxon_name|
      taxon_name.reload
      unless taxon_name.valid?
        Rails.logger.info "[INFO] Destroying #{taxon_name} while merging taxon " + 
          "#{reject.id} into taxon #{id}: #{taxon_name.errors.full_messages.to_sentence}"
        taxon_name.destroy 
        next
      end
      if taxon_name.is_scientific_names? && taxon_name.is_valid?
        taxon_name.update_attributes(:is_valid => false)
      end
    end
    
    LifeList.delay(:priority => INTEGRITY_PRIORITY).update_life_lists_for_taxon(self)
    Taxon.delay(:priority => INTEGRITY_PRIORITY, :queue => "slow").update_listed_taxa_for(self, reject.ancestry)
    
    %w(flags).each do |association|
      send(association, :reload => true).each do |associate|
        associate.destroy unless associate.valid?
      end
    end

    Taxon.delay(:priority => INTEGRITY_PRIORITY).set_iconic_taxon_for_observations_of(id)
    
    reject.reload
    Rails.logger.info "[INFO] Merged #{reject} into #{self}"
    reject.destroy
  end
# Sometimes you don't want to make a new taxon name with a taxon, like when
# you're saving a new taxon name with a new associated taxon. Hence, this.
Sometimes you don't want to make a new taxon name with a taxon, like when
you're saving a new taxon name with a new associated taxon. Hence, this.
Sometimes you don ' t want to make a new taxon name with a taxon like when you ' re saving a new taxon name with a new associated taxon . Hence this .
def merge(reject)
    raise "Can't merge a taxon with itself" if reject.id == self.id
    reject_taxon_names = reject.taxon_names.all
    reject_taxon_scheme_taxa = reject.taxon_scheme_taxa.all
    TaxonAncestor.where(:taxon_id => reject.id).delete_all
    TaxonAncestor.where(:ancestor_taxon_id => reject.id).delete_all
    merge_has_many_associations(reject)
    ListRule.update_all(["operand_id = ?", id], ["operand_id = ? AND operand_type = ?", reject.id, Taxon.to_s])
    self.colors << reject.colors if colors.blank?
    reject.children.each {|child| child.move_to_child_of(self)}
    reject_taxon_scheme_taxa.each do |reject_tst|
      reject_tst.reload
      reject_tst_name = reject_tst.taxon_name
      if taxon_name = self.taxon_names.where(:lexicon => TaxonName::SCIENTIFIC_NAMES, :name => reject_tst_name.name).first
        reject_tst.update_attributes(:taxon_name_id => taxon_name.id)
      end
      unless reject_tst.valid?
        Rails.logger.info "[INFO] Destroying #{reject_tst} while merging taxon " + 
          "#{reject.id} into taxon #{id}: #{reject_tst.errors.full_messages.to_sentence}"
        reject_tst.destroy 
        next
      end
    end
    reject_taxon_names.each do |taxon_name|
      taxon_name.reload
      unless taxon_name.valid?
        Rails.logger.info "[INFO] Destroying #{taxon_name} while merging taxon " + 
          "#{reject.id} into taxon #{id}: #{taxon_name.errors.full_messages.to_sentence}"
        taxon_name.destroy 
        next
      end
      if taxon_name.is_scientific_names? && taxon_name.is_valid?
        taxon_name.update_attributes(:is_valid => false)
      end
    end
    LifeList.delay(:priority => INTEGRITY_PRIORITY).update_life_lists_for_taxon(self)
    Taxon.delay(:priority => INTEGRITY_PRIORITY, :queue => "slow").update_listed_taxa_for(self, reject.ancestry)
    %w(flags).each do |association|
      send(association, :reload => true).each do |associate|
        associate.destroy unless associate.valid?
      end
    end
    Taxon.delay(:priority => INTEGRITY_PRIORITY).set_iconic_taxon_for_observations_of(id)
    reject.reload
    Rails.logger.info "[INFO] Merged #{reject} into #{self}"
    reject.destroy
  end
def merge ( reject ) raise "Can't merge a taxon with itself" if reject . id == self . id reject_taxon_names = reject . taxon_names . all reject_taxon_scheme_taxa = reject . taxon_scheme_taxa . all TaxonAncestor . where ( :taxon_id => reject . id ) . delete_all TaxonAncestor . where ( :ancestor_taxon_id => reject . id ) . delete_all merge_has_many_associations ( reject ) ListRule . update_all ( [ "operand_id = ?" , id ] , [ "operand_id = ? AND operand_type = ?" , reject . id , Taxon . to_s ] ) self . colors << reject . colors if colors . blank? reject . children . each { | child | child . move_to_child_of ( self ) } reject_taxon_scheme_taxa . each do | reject_tst | reject_tst . reload reject_tst_name = reject_tst . taxon_name if taxon_name = self . taxon_names . where ( :lexicon => TaxonName :: SCIENTIFIC_NAMES , :name => reject_tst_name . name ) . first reject_tst . update_attributes ( :taxon_name_id => taxon_name . id ) end unless reject_tst . valid? Rails . logger . info "[INFO] Destroying #{reject_tst} while merging taxon " + "#{reject.id} into taxon #{id}: #{reject_tst.errors.full_messages.to_sentence}" reject_tst . destroy next end end reject_taxon_names . each do | taxon_name | taxon_name . reload unless taxon_name . valid? Rails . logger . info "[INFO] Destroying #{taxon_name} while merging taxon " + "#{reject.id} into taxon #{id}: #{taxon_name.errors.full_messages.to_sentence}" taxon_name . destroy next end if taxon_name . is_scientific_names? && taxon_name . is_valid? taxon_name . update_attributes ( :is_valid => false ) end end LifeList . delay ( :priority => INTEGRITY_PRIORITY ) . update_life_lists_for_taxon ( self ) Taxon . delay ( :priority => INTEGRITY_PRIORITY , :queue => "slow" ) . update_listed_taxa_for ( self , reject . ancestry ) %w( flags ) . each do | association | send ( association , :reload => true ) . each do | associate | associate . destroy unless associate . valid? end end Taxon . delay ( :priority => INTEGRITY_PRIORITY ) . set_iconic_taxon_for_observations_of ( id ) reject . reload Rails . logger . info "[INFO] Merged #{reject} into #{self}" reject . destroy end
Sometimes you don't want to make a new taxon name with a taxon, like when
you're saving a new taxon name with a new associated taxon.
Sometimes you don ' t want to make a new taxon name with a taxon like when you ' re saving a new taxon name with a new associated taxon .
# otherwise it will screw up merge_has_many_associations # Merge ListRules and other polymorphic assocs # Keep reject colors if keeper has none # Move reject child taxa to the keeper # Update or destroy merged taxon scheme taxa # Update or destroy merged taxon_names
reject