Module: Shared::IsIndexedBiologicalAssociation

Extended by:
ActiveSupport::Concern
Included in:
BiologicalAssociation
Defined in:
app/models/concerns/shared/is_indexed_biological_association.rb

Overview

Shared code for BiologicalAssociation to maintain the biological_association_index table.

Constant Summary collapse

DWC_TYPES =

Types that have dwc_recorded_by method.

%w{CollectionObject FieldOccurrence}.freeze
SOURCE_TYPES =

Types that don’t have dwc_recorded_by - use citation sources instead.

%w{Otu AnatomicalPart}.freeze

Instance Method Summary collapse

Instance Method Details

#biological_association_citation_yearObject (private)



175
176
177
# File 'app/models/concerns/shared/is_indexed_biological_association.rb', line 175

def biological_association_citation_year
  ApplicationController.helpers.short_sources_year_tag(sources)
end

#biological_association_citationsObject (private)

TODO: Should reference DOIs, Identifiers, or identifiers in lieu of short citations. Could be collectors (ORCID or …) Could be citation providers



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'app/models/concerns/shared/is_indexed_biological_association.rb', line 194

def biological_association_citations
  subject_type = biological_association_subject_type
  object_type = biological_association_object_type

  subject_is_dwc = DWC_TYPES.include?(subject_type)
  object_is_dwc = DWC_TYPES.include?(object_type)

  if !subject_is_dwc && !object_is_dwc
    # Both are source-based types, like OTU.
    ApplicationController.helpers.short_sources_tag(sources)
  elsif subject_is_dwc && !object_is_dwc
    # Subject is specimen-like, object is OTU-like.
    biological_association_subject.dwc_recorded_by || ApplicationController.helpers.short_sources_tag(sources)
  elsif !subject_is_dwc && object_is_dwc
    # Subject is OTU-like, object is specimen-like.
    biological_association_object.dwc_recorded_by || ApplicationController.helpers.short_sources_tag(sources)
  else
    # Both are specimen-like.
    # Lots of assumptions behind this. What if specimens were marked as
    # collected in 2 different events, that would be odd but perhaps not
    # impossible.
    biological_association_subject.dwc_recorded_by || biological_association_object.dwc_recorded_by
  end
end

#biological_association_established_dateObject (private)



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'app/models/concerns/shared/is_indexed_biological_association.rb', line 219

def biological_association_established_date
  subject_type = biological_association_subject_type
  object_type = biological_association_object_type

  subject_is_dwc = DWC_TYPES.include?(subject_type)
  object_is_dwc = DWC_TYPES.include?(object_type)

  if !subject_is_dwc && !object_is_dwc
    # Both are source-based types, like OTU.
    ApplicationController.helpers.short_sources_year_tag(sources)
  elsif subject_is_dwc && !object_is_dwc
    # Subject is specimen-like, object is OTU-like.
    biological_association_subject.dwc_event_date || ApplicationController.helpers.short_sources_year_tag(sources)
  elsif !subject_is_dwc && object_is_dwc
    # Subject is OTU-like, object is specimen-like.
    biological_association_object.dwc_event_date || ApplicationController.helpers.short_sources_year_tag(sources)
  else
    # Both are specimen-like.
    # Lots of assumptions behind this. What if specimens were marked as
    # collected in 2 different events, that would be odd but perhaps not
    # impossible.
    biological_association_subject.dwc_event_date || biological_association_object.dwc_event_date
  end
end

#biological_association_index_attributesObject

!! This is expensive, it recomputes values for every field.

Returns:

  • Hash of field: value



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'app/models/concerns/shared/is_indexed_biological_association.rb', line 51

def biological_association_index_attributes
  {
    biological_association_id: id,
    biological_association_uuid: uuid,
    biological_relationship_id: biological_relationship_id,
    project_id: project_id,
    created_by_id: created_by_id,
    updated_by_id: updated_by_id,

    # Subject fields
    subject_id: biological_association_subject_id,
    subject_type: biological_association_subject_type,
    subject_otu_id: biological_association_subject_otu_id,
    subject_uuid: biological_association_subject_uuid,
    subject_label: biological_association_subject_label,
    subject_order: biological_association_subject_taxonomy_field('order'),
    subject_family: biological_association_subject_taxonomy_field('family'),
    subject_genus: biological_association_subject_taxonomy_field('genus'),
    subject_properties: biological_association_subject_properties,

    # Relationship fields
    biological_relationship_uri: biological_relationship_uri,
    relationship_name: biological_relationship.name,
    relationship_inverted_name: biological_relationship.inverted_name,

    # Object fields
    object_id: biological_association_object_id,
    object_type: biological_association_object_type,
    object_otu_id: biological_association_object_otu_id,
    object_uuid: biological_association_object_uuid,
    object_label: biological_association_object_label,
    object_order: biological_association_object_taxonomy_field('order'),
    object_family: biological_association_object_taxonomy_field('family'),
    object_genus: biological_association_object_taxonomy_field('genus'),
    object_properties: biological_association_object_properties,

    # Citations and metadata
    citations: biological_association_citations,
    citation_year: biological_association_citation_year,
    established_date: biological_association_established_date,
    remarks: biological_association_remarks
  }
end

#biological_association_object_labelObject (private)



151
152
153
# File 'app/models/concerns/shared/is_indexed_biological_association.rb', line 151

def biological_association_object_label
  ApplicationController.helpers.label_for(biological_association_object)
end

#biological_association_object_otu_idObject (private)



134
135
136
137
138
139
140
141
142
143
144
145
# File 'app/models/concerns/shared/is_indexed_biological_association.rb', line 134

def biological_association_object_otu_id
  case biological_association_object.class.base_class.name
  when 'Otu'
    biological_association_object.id
  when 'CollectionObject', 'FieldOccurrence'
    biological_association_object.current_otu&.id
  when 'AnatomicalPart'
    biological_association_object.cached_otu_id
  else
    raise TaxonWorks::Error, "Unhandled object type #{biological_association_object.class.base_class.name} in biological_association_object_otu_id"
  end
end

#biological_association_object_propertiesObject (private)



167
168
169
# File 'app/models/concerns/shared/is_indexed_biological_association.rb', line 167

def biological_association_object_properties
  object_biological_properties.pluck(:name).join(Shared::IsDwcOccurrence::DWC_DELIMITER).presence
end

#biological_association_object_taxonomy_field(rank) ⇒ Object (private)



159
160
161
# File 'app/models/concerns/shared/is_indexed_biological_association.rb', line 159

def biological_association_object_taxonomy_field(rank)
  [biological_association_object.taxonomy[rank]].flatten.compact.join(' ').presence
end

#biological_association_object_uuidObject (private)



114
115
116
117
118
119
# File 'app/models/concerns/shared/is_indexed_biological_association.rb', line 114

def biological_association_object_uuid
  case biological_association_object.class.base_class.name
  when 'Otu', 'CollectionObject', 'FieldOccurrence', 'AnatomicalPart'
    biological_association_object.uuid
  end
end

#biological_association_remarksObject (private)

TODO: Generic helper



180
181
182
# File 'app/models/concerns/shared/is_indexed_biological_association.rb', line 180

def biological_association_remarks
  Utilities::Strings.sanitize_for_csv(notes.collect { |n| n.text }.join(Shared::IsDwcOccurrence::DWC_DELIMITER)).presence
end

#biological_association_subject_labelObject (private)



147
148
149
# File 'app/models/concerns/shared/is_indexed_biological_association.rb', line 147

def biological_association_subject_label
  ApplicationController.helpers.label_for(biological_association_subject)
end

#biological_association_subject_otu_idObject (private)



121
122
123
124
125
126
127
128
129
130
131
132
# File 'app/models/concerns/shared/is_indexed_biological_association.rb', line 121

def biological_association_subject_otu_id
  case biological_association_subject.class.base_class.name
  when 'Otu'
    biological_association_subject.id
  when 'CollectionObject', 'FieldOccurrence'
    biological_association_subject.current_otu&.id
  when 'AnatomicalPart'
    biological_association_subject.cached_otu_id
  else
    raise TaxonWorks::Error, "Unhandled subject type #{biological_association_subject.class.base_class.name} in biological_association_subject_otu_id"
  end
end

#biological_association_subject_propertiesObject (private)



163
164
165
# File 'app/models/concerns/shared/is_indexed_biological_association.rb', line 163

def biological_association_subject_properties
  subject_biological_properties.pluck(:name).join(Shared::IsDwcOccurrence::DWC_DELIMITER).presence
end

#biological_association_subject_taxonomy_field(rank) ⇒ Object (private)



155
156
157
# File 'app/models/concerns/shared/is_indexed_biological_association.rb', line 155

def biological_association_subject_taxonomy_field(rank)
  [biological_association_subject.taxonomy[rank]].flatten.compact.join(' ').presence
end

#biological_association_subject_uuidObject (private)



107
108
109
110
111
112
# File 'app/models/concerns/shared/is_indexed_biological_association.rb', line 107

def biological_association_subject_uuid
  case biological_association_subject.class.base_class.name
  when 'Otu', 'CollectionObject', 'FieldOccurrence', 'AnatomicalPart'
    biological_association_subject.uuid
  end
end

#biological_relationship_uriObject (private)



171
172
173
# File 'app/models/concerns/shared/is_indexed_biological_association.rb', line 171

def biological_relationship_uri
  biological_relationship.uris.first&.cached
end

#get_biological_association_indexBiologicalAssociationIndex

Returns does not rebuild if already built.

Returns:



97
98
99
100
101
102
103
# File 'app/models/concerns/shared/is_indexed_biological_association.rb', line 97

def get_biological_association_index
  if biological_association_index_persisted?
    biological_association_index
  else
    set_biological_association_index
  end
end

#set_biological_association_indexBiologicalAssociationIndex

Returns !! always touches the database.

Returns:



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/models/concerns/shared/is_indexed_biological_association.rb', line 23

def set_biological_association_index
  retried = false
  begin
    if biological_association_index_persisted?
      biological_association_index.update_columns(
        biological_association_index_attributes.merge(
          rebuild_set: nil,
          updated_at: Time.zone.now)
      )
    else
      create_biological_association_index!(biological_association_index_attributes)
    end
  rescue ActiveRecord::ActiveRecordError
    unless retried
      retried = true
      biological_association_index&.reload
      retry
    else
      raise
    end
  end
  biological_association_index
end