Module: Protonym::Becomes

Extended by:
ActiveSupport::Concern
Included in:
Protonym
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:



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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'app/models/protonym/becomes.rb', line 97

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.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

      # Why?  Do something more specific, like
      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('; '))
    c = becomes!(Protonym)
  rescue
    raise
  end

  c
  # TODO: This fixes ./spec/models/combination/combination_spec.rb:62. But is returning `self` (or `z`) when fails trustworthy?
  # self
end

#becomes_test_classificationsObject



55
56
57
58
59
60
61
62
# File 'app/models/protonym/becomes.rb', line 55

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



37
38
39
40
41
42
43
44
# File 'app/models/protonym/becomes.rb', line 37

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



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

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



64
65
66
67
68
69
70
71
# File 'app/models/protonym/becomes.rb', line 64

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
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# 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
    if a.similar_homonym_string
      a
    else
      s = a.subject_taxon_name
      if !s.cached_secondary_homonym_alternative_spelling.blank?
        ids = TaxonName.where(cached_valid_taxon_name_id: s.cached_valid_taxon_name_id).pluck(:id)
        TaxonNameRelationship.where(subject_taxon_name: ids).where("type LIKE 'TaxonNameRelationship::Iczn::Invalidating::Synonym%'").each do |tr|
          o = tr.subject_taxon_name
          if !o.cached_secondary_homonym_alternative_spelling.blank? && s.cached_secondary_homonym_alternative_spelling == o.cached_secondary_homonym_alternative_spelling && s.cached_valid_taxon_name_id == o.cached_valid_taxon_name_id
            a.object_taxon_name_id = o.id
          end
        end
      end
      a
    end
  end
end

#becomes_test_for_similarity(invalidating_relationship) ⇒ Object



46
47
48
49
50
51
52
53
# File 'app/models/protonym/becomes.rb', line 46

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])


84
85
86
87
88
89
90
91
92
93
# File 'app/models/protonym/becomes.rb', line 84

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