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)



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

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



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'app/models/concerns/shared/is_indexed_biological_association.rb', line 166

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)



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

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
# 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_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_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)



123
124
125
# File 'app/models/concerns/shared/is_indexed_biological_association.rb', line 123

def biological_association_object_label
  ApplicationController.helpers.label_for(biological_association_object)
end

#biological_association_object_propertiesObject (private)



139
140
141
# File 'app/models/concerns/shared/is_indexed_biological_association.rb', line 139

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)



131
132
133
# File 'app/models/concerns/shared/is_indexed_biological_association.rb', line 131

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

#biological_association_object_uuidObject (private)



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

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



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

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)



119
120
121
# File 'app/models/concerns/shared/is_indexed_biological_association.rb', line 119

def biological_association_subject_label
  ApplicationController.helpers.label_for(biological_association_subject)
end

#biological_association_subject_propertiesObject (private)



135
136
137
# File 'app/models/concerns/shared/is_indexed_biological_association.rb', line 135

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)



127
128
129
# File 'app/models/concerns/shared/is_indexed_biological_association.rb', line 127

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

#biological_association_subject_uuidObject (private)



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

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)



143
144
145
# File 'app/models/concerns/shared/is_indexed_biological_association.rb', line 143

def biological_relationship_uri
  biological_relationship.uris.first&.cached
end

#get_biological_association_indexBiologicalAssociationIndex

Returns does not rebuild if already built.

Returns:



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

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