Class: Catalog::Otu::InventoryEntry

Inherits:
Entry
  • Object
show all
Defined in:
lib/catalog/otu/inventory_entry.rb

Overview

A Catalog::Entry that contains the biological history of an otu via associated data of that otu (images, asserted distributions, observations, etc.).

Instance Attribute Summary collapse

Attributes inherited from Entry

#dates, #item_index, #items, #object, #sort_order, #sources, #topics

Instance Method Summary collapse

Methods inherited from Entry

#all_citations, #all_dates, #all_sources, #all_topics, #citations, #coordinate_entry_items, #date_range, #first_item?, #index_items, #is_subsequent_entry_item?, #items_by_object, #last_item?, #ordered_by_nomenclature_date, #original_citation_present?, #to_json, #topics_for_source, #year_hash

Constructor Details

#initialize(otu, include_belongs_to: false) ⇒ InventoryEntry

nomenclature catalog typically handles citations on taxon_name, so by default we don’t process belongs_to relations on otu.



9
10
11
12
13
# File 'lib/catalog/otu/inventory_entry.rb', line 9

def initialize(otu, include_belongs_to: false)
  @include_belongs_to = include_belongs_to
  super(otu)
  true
end

Instance Attribute Details

#include_belongs_toObject

Returns the value of attribute include_belongs_to.



5
6
7
# File 'lib/catalog/otu/inventory_entry.rb', line 5

def include_belongs_to
  @include_belongs_to
end

Instance Method Details

#add_rendering_includes(citation_query, relation_klass) ⇒ Object (private)

Eager load citation associations and the cited object for views. (We’re unfortunately tied to views since catalog is essentially a rendering tool.)



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/catalog/otu/inventory_entry.rb', line 177

def add_rendering_includes(citation_query, relation_klass)
  if relation_klass.name == 'BiologicalAssociation'
    citation_query = citation_query
      .includes(
        :source,
        :notes,
        :topics,
        {
          citation_object: [
            { biological_association_subject: { taxon_name: [:taxon_name_classifications, :taxon_name_relationships] } },
            { biological_association_object: { taxon_name: [:taxon_name_classifications, :taxon_name_relationships] } }
          ]
        }
      )
  else
    citation_query = citation_query
      .includes(:source, :notes, :topics, :citation_object)
  end

  citation_query
end

#buildObject



15
16
17
18
# File 'lib/catalog/otu/inventory_entry.rb', line 15

def build
  from_self
  true
end

#entry_item_matches_target?(item_object, reference_object) ⇒ Boolean (private)

Returns:

  • (Boolean)


200
201
202
# File 'lib/catalog/otu/inventory_entry.rb', line 200

def entry_item_matches_target?(item_object, reference_object)
  item_object.taxon_name_id == reference_object.taxon_name_id
end

#from_selfObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/catalog/otu/inventory_entry.rb', line 24

def from_self
  coordinate_otu_ids = ::Otu.coordinate_otus(object.id).pluck(:id)
  relation_names = ApplicationEnumeration.citable_relations(Otu).values.flatten(1)
  reflections = ApplicationEnumeration.filter_sti_relations(Otu, relation_names)

  # Include taxon_name with nested associations to avoid N+1 queries when
  # rendering.
  coordinate_otus = ::Otu.where(id: coordinate_otu_ids)
    .includes(taxon_name: [:taxon_name_classifications, :taxon_name_relationships])
    .index_by(&:id)

  belongs_to_relations = []
  has_one_relations = []
  has_many_relations = []
  through_relations = []

  reflections.each do |relation_name, reflection|
    if reflection.through_reflection
      through_relations << relation_name
    elsif reflection.macro == :belongs_to
      belongs_to_relations << relation_name
    elsif reflection.macro == :has_one
      has_one_relations << relation_name
    elsif reflection.macro == :has_many
      has_many_relations << relation_name
    end
  end

  if include_belongs_to
    process_belongs_to_relations(belongs_to_relations, coordinate_otus)
  end

  # has_one and has_many use the same foreign key logic.
  process_has_relations(has_one_relations + has_many_relations, coordinate_otus)

  process_through_relations(through_relations, coordinate_otu_ids)

  @items.uniq! { |item| [item.citation.id, item.object.class.name, item.object.id] }
end

#process_belongs_to_relations(belongs_to_relations, coordinate_otus) ⇒ Object (private)



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
94
95
96
97
98
99
100
101
# File 'lib/catalog/otu/inventory_entry.rb', line 66

def process_belongs_to_relations(belongs_to_relations, coordinate_otus)
  belongs_to_relations.each do |relation_name|
    reflection = Otu.reflect_on_association(relation_name)
    relation_klass = reflection.klass
    foreign_key = reflection.foreign_key

    target_ids = coordinate_otus.values.map { |otu| otu.send(foreign_key) }.compact.uniq
    next if target_ids.empty?

    otu_to_target = coordinate_otus.transform_values { |otu| otu.send(foreign_key) }

    # Query citations directly on the target objects
    citations = Citation
      .where(citation_object_type: relation_klass.name, citation_object_id: target_ids)
      .includes(:source, :notes, :citation_object)
      .to_a

    citations.each do |citation|
      associated_object = citation.citation_object

      # Find which coordinate OTUs reference this object
      referencing_otu_ids = otu_to_target.select { |otu_id, target_id| target_id == associated_object.id }.keys

      referencing_otu_ids.each do |otu_id|
        base_otu = coordinate_otus[otu_id]
        @items << Catalog::Otu::InventoryEntryItem.new(
          object: associated_object,
          base_object: base_otu,
          citation: citation,
          nomenclature_date: citation.source&.cached_nomenclature_date,
          current_target: entry_item_matches_target?(base_otu, object)
        )
      end
    end
  end
end

#process_has_relations(has_relations, coordinate_otus) ⇒ Object (private)

Processes has_one and has_many relations (both use foreign key logic). !! Does not process :through relations.



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
# File 'lib/catalog/otu/inventory_entry.rb', line 105

def process_has_relations(has_relations, coordinate_otus)
  coordinate_otu_ids = coordinate_otus.keys

  has_relations.each do |relation_name|
    reflection = Otu.reflect_on_association(relation_name)
    relation_klass = reflection.klass
    foreign_key = reflection.foreign_key

    citation_query = Citation
      .joins("INNER JOIN #{relation_klass.table_name} ON citations.citation_object_type = '#{relation_klass.name}' AND citations.citation_object_id = #{relation_klass.table_name}.id")
      .where("#{relation_klass.table_name}.#{foreign_key}" => coordinate_otu_ids)

    # Handle polymorphic associations (e.g., otu_id with otu_type = 'Otu')
    if reflection.type
      citation_query = citation_query.where("#{relation_klass.table_name}.#{reflection.type}" => 'Otu')
    end

    citations = add_rendering_includes(citation_query, relation_klass).to_a

    citations.each do |citation|
      associated_object = citation.citation_object
      base_otu_id = associated_object.send(foreign_key)
      base_otu = coordinate_otus[base_otu_id]

      @items << Catalog::Otu::InventoryEntryItem.new(
        object: associated_object,
        base_object: base_otu,
        citation: citation,
        nomenclature_date: citation.source&.cached_nomenclature_date,
        current_target: entry_item_matches_target?(base_otu, object)
      )
    end
  end
end

#process_through_relations(through_relations, coordinate_otu_ids) ⇒ Object (private)



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/catalog/otu/inventory_entry.rb', line 140

def process_through_relations(through_relations, coordinate_otu_ids)
  return if through_relations.empty?

  # Attempting to build custom joins for through relations a la
  # #process_has_relations is complex due to:
  # - STI tables (e.g., protonym → taxon_names table)
  # - Polymorphic sources
  # - Different join patterns for each through type
  # So here we just let rails handle everything.
  includes_hash = through_relations.each_with_object({}) do |rel, hash|
    hash[rel] = { citations: [:source, :notes, :topics] } # for views
  end

  coordinate_otus = ::Otu.where(id: coordinate_otu_ids).includes(includes_hash).to_a

  coordinate_otus.each do |otu|
    through_relations.each do |relation_name|
      association = otu.send(relation_name)
      association = [association] if !association.is_a?(Enumerable)

      association.compact.each do |associated_object|
        associated_object.citations.each do |citation|
          @items << Catalog::Otu::InventoryEntryItem.new(
            object: associated_object,
            base_object: otu,
            citation: citation,
            nomenclature_date: citation.source&.cached_nomenclature_date,
            current_target: entry_item_matches_target?(otu, object)
          )
        end
      end
    end
  end
end

#to_html_methodObject



20
21
22
# File 'lib/catalog/otu/inventory_entry.rb', line 20

def to_html_method
  :otu_catalog_entry_item_to_html
end