Module: Protonym::Becomes

Extended by:
ActiveSupport::Concern
Defined in:
app/models/protonym/becomes.rb

Overview

This module contains code that lets us convert Protonyms with very specific attributes into a Combination. It is used almost exclusively during an import.

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#becomes_combinationself, or self as Combination

Convert a Protonym into a Combination.

Returns:



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'app/models/protonym/becomes.rb', line 84

def becomes_combination
  a, original_relationships, c = nil, nil, nil

  if b = convertable_to_combination?
    a, original_relationships = b 
  else
    return self
  end

  begin
    Protonym.transaction do
      c = becomes!(Combination)

      c.assign_attributes(
        rank_class: nil,
        name: nil,
        verbatim_name: cached_original_combination,
        disable_combination_relationship_check: true
      )
      c.clear_cached

      # Destroy invalidating relationship, but note
      # what it references. 
      o = a.object_taxon_name
      a_id = a.id
      a.destroy!

      taxon_name_relationships.find_each do |r|
        next if r.object_taxon_name_id == r.subject_taxon_name_id
        next if r.id == a_id
        r.update_column(:subject_taxon_name_id, o.id)
      end

      original_relationships.each do |i|
        atr = { type: i.type.gsub(/TaxonNameRelationship::OriginalCombination::Original/, 'TaxonNameRelationship::Combination::' ) }
        if i.object_taxon_name_id == i.subject_taxon_name_id 
          atr[:subject_taxon_name_id] = o.id
        end

        i.update_columns(atr)
      end

      c.save!
      c.disable_combination_relationship_check = false
      c
    end

  # Note: technically a.destroy could hit this, but that should never happen.
  rescue ActiveRecord::RecordInvalid => e
    errors.add(:base, 'Combination failed to save: ' + c.errors.full_messages.join('; '))
    z = becomes!(Protonym)
  rescue
    raise
  end

  c 
end

#becomes_test_classificationsObject



41
42
43
44
45
46
47
48
# File 'app/models/protonym/becomes.rb', line 41

def becomes_test_classifications
  if taxon_name_classifications.any? 
    errors.add(:base, 'Protonym has taxon name classifications, it can not be converted to combination.')
    false
  else
    true
  end
end

#becomes_test_for_original_genusObject



23
24
25
26
27
28
29
30
# File 'app/models/protonym/becomes.rb', line 23

def becomes_test_for_original_genus
  if original_genus
    true
  else
    errors.add(:base, 'Protonym does not have original genus assigned.')
    false
  end
end

#becomes_test_for_original_relationshipsObject



59
60
61
62
63
64
65
66
67
# File 'app/models/protonym/becomes.rb', line 59

def becomes_test_for_original_relationships
  r = original_combination_relationships.load
  if r.select{|r| r.subject_taxon_name_id == id}.empty?
    errors.add(:base, 'Protonym is missing original combination relationship to self.')
    false
  else
    r 
  end
end

#becomes_test_for_other_relationshipsObject



50
51
52
53
54
55
56
57
# File 'app/models/protonym/becomes.rb', line 50

def becomes_test_for_other_relationships
  if related_taxon_name_relationships.with_type_base('TaxonNameRelationship::Iczn').any? || related_taxon_name_relationships.with_type_base('TaxonNameRelationship::Typification').any?
    errors.add(:base, 'Protonym has additional taxon name relationships, it can not be converted to combination.')
    false
  else
    true
  end
end

#becomes_test_for_relationshipObject



12
13
14
15
16
17
18
19
20
21
# File 'app/models/protonym/becomes.rb', line 12

def becomes_test_for_relationship
  # a = TaxonNameRelationship::Iczn::Invalidating.where(subject_taxon_name: self).first ### This one returns all subclasses
  a = TaxonNameRelationship.where(subject_taxon_name: self, type: 'TaxonNameRelationship::Iczn::Invalidating').first
  if a.nil? || a.subject_taxon_name_id == a.object_taxon_name_id
    errors.add(:base, 'Required TaxonNameRelationship::Iczn::Invalidating relationship not found on this name.')
    false
  else
    a
  end
end

#becomes_test_for_similarity(invalidating_relationship) ⇒ Object



32
33
34
35
36
37
38
39
# File 'app/models/protonym/becomes.rb', line 32

def becomes_test_for_similarity(invalidating_relationship)
  if invalidating_relationship.similar_homonym_string
    true
  else
    errors.add(:base, 'Related invalid name is not similar enough to protonym to be treated as combination.')
    false
  end
end

#convertable_to_combination?[taxon_name_relationship, original_relationships]

Returns:

  • ([taxon_name_relationship, original_relationships])


70
71
72
73
74
75
76
77
78
79
80
# File 'app/models/protonym/becomes.rb', line 70

def convertable_to_combination?
  a = nil

  return false unless a = becomes_test_for_relationship
  return false unless becomes_test_for_similarity(a)
  return false unless becomes_test_for_original_genus 
  return false unless becomes_test_classifications
  return false unless becomes_test_for_other_relationships
  return false unless original_relationships = becomes_test_for_original_relationships
  return [a, original_relationships]
end