Module: OtusHelper

Includes:
RecordNavigationHelper
Defined in:
app/helpers/otus_helper.rb

Instance Method Summary collapse

Methods included from RecordNavigationHelper

for

Instance Method Details

Stub a smart link to browse OTUs

Parameters:

  • object (an instance of TaxonName or Otu) —

    if TaxonName is provided JS UI will disambiguate if more options are possible



235
236
237
238
239
# File 'app/helpers/otus_helper.rb', line 235

def browse_otu_link(object)
  return nil if object.nil?
  otu = object.metamorphosize
  (:div, '', 'data-taxon-name' => object_tag(otu), 'data-redirect' => 'true', 'data-id' => otu.id, 'data-klass' => object.class.base_class.name.to_s, 'data-otu-button' => 'true')
end

Returns { dwc_occurrence_id: [ image1, image2 ... ], ... }.

Returns:

  • (Hash) —

    { dwc_occurrence_id: [ image1, image2 ... ], ... }



336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
# File 'app/helpers/otus_helper.rb', line 336

def dwc_gallery_data(otu, dwc_occurrence_id: [], pagination_headers: true)
  a = DwcOccurrence.scoped_by_otu(otu)
    .select(:id, :dwc_occurrence_object_id, :dwc_occurrence_object_type)

  dwc_ids = [dwc_occurrence_id].flatten.compact.uniq

  if dwc_ids.any?
    a = a.where(id: dwc_ids)
  end

  a = a.page(params[:page]).per(params[:per])

  # Somehwhat of a janky pattern, probably needs to be
  # moved into Controller.
  assign_pagination(a) if pagination_headers

  b = Image.with(dwc_scope: a)
    .joins('JOIN depictions d on d.image_id = images.id' )
    .joins("JOIN dwc_scope on d.depiction_object_id = dwc_scope.dwc_occurrence_object_id AND d.depiction_object_type = 'CollectionObject' AND dwc_scope.dwc_occurrence_object_type = 'CollectionObject'")
    .select('images.*, dwc_scope.id dwc_id')
    .distinct

  r = {}
  b.find_each do |o|
    r[o.dwc_id] ||= []
    r[o.dwc_id].push o
  end
  r
end

#label_for_otu(otu) ⇒ Object



78
79
80
81
82
83
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
# File 'app/helpers/otus_helper.rb', line 78

def label_for_otu(otu)
  return nil if otu.nil?
  if otu.taxon_name
    if otu.taxon_name.is_combination?

      if otu.name?
        [
          otu.name,
          label_for_taxon_name(otu.taxon_name)
        ].compact.join(' == ').html_safe
      else
        label_for_taxon_name(otu.taxon_name) + ' [c]'
      end

    else # protonym
      if otu.taxon_name.is_valid?

        [ label_for_taxon_name(otu.taxon_name),
          otu.name
        ].compact.join(' ')

      else # invalid taxon name
        [
          [ label_for_taxon_name(otu.taxon_name),
            otu.name
          ].compact.join(' '),

          [ label_for_taxon_name(otu.taxon_name.valid_taxon_name),
            otu.name
          ].compact.join(' ')
        ].join(' now ')

      end
    end

  else
    otu.name
  end
end

#next_records(otu) ⇒ Array

Returns of OTUs.

Returns:

  • (Array) —

    of OTUs



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'app/helpers/otus_helper.rb', line 252

def next_records(otu)
  if otu.taxon_name_id
    o = []
    t = otu.taxon_name.next_sibling
    unless t.nil?
      while o.empty?
        break if t.nil?
        o = t.otus.to_a
        t = t.next_sibling
      end
    end
    o
  else
    super
  end
end

#otu_autocomplete_selected_tag(otu) ⇒ String

DEPRECATE

Returns:

  • (String) —

    no HTML inside



211
212
213
214
215
216
217
# File 'app/helpers/otus_helper.rb', line 211

def otu_autocomplete_selected_tag(otu)
  return nil if otu.nil? || (otu.new_record? && !otu.changed?) # probably get rid of this
  label_for_otu(otu)
#   [otu.name,
#    Utilities::Strings.nil_wrap('[',taxon_name_autocomplete_selected_tag(otu.taxon_name), ']')&.html_safe
#   ].compact.join(' ')
end

#otu_autocomplete_tag(otu, term = nil) ⇒ Object Also known as: otu_autoselect_tag



118
119
120
121
# File 'app/helpers/otus_helper.rb', line 118

def otu_autocomplete_tag(otu, term = nil)
  return nil if otu.nil?
  mark_tag(otu_tag(otu), term)
end

#otu_autoselect_info(otu) ⇒ Object

TODO: alias proper

HTML label for the autoselect dropdown (left-justified).

def otu_autoselect_tag(otu, term = nil) return nil if otu.nil? mark_tag(otu_tag(otu), term) end



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'app/helpers/otus_helper.rb', line 132

def otu_autoselect_info(otu)
  return [] if otu.taxon_name_id.blank?
  r = [ ]

  t = otu.taxon_name

  if t && t.is_protonym?
    if t.is_genus_or_species_rank?
      r.push taxon_name_original_combination_tag(t)
    end

  # if !t.is_valid?
  #   r.push taxon_name_now_tag(t.valid_taxon_name)
  # end
  else
    r.push t.type
  end
  r
end

#otu_common_names_label(common_names, term = nil) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'app/helpers/otus_helper.rb', line 179

def otu_common_names_label(common_names, term = nil)
  return nil if common_names.nil? || common_names.empty?

  if term.empty?
    return common_names.map(&:name).sort.join(', ')
  end

  prefix_matches = []
  wildcard_matches = []
  non_matches = []

  term = term.downcase
  # Regexp.escape turns each space into '\\ '.
  wildcard_term = Regexp.escape(term).gsub(/(\\ )+/, '.*')

  common_names.each do |o|
    name = o.name.downcase
    if name.start_with?(term)
      prefix_matches << name
    elsif name =~ /#{wildcard_term}/
      wildcard_matches << name
    else
      non_matches << name
    end
  end

  (prefix_matches.sort + wildcard_matches.sort + non_matches.sort).join(', ')
end

#otu_extended_autocomplete_tag(target, otu, common_names, term) ⇒ Object

Used exclusively in /api/v1/otus/autocomplete



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'app/helpers/otus_helper.rb', line 162

def otu_extended_autocomplete_tag(target, otu, common_names, term)
  if target.kind_of?(Otu)
    t = otu_tag(target)
  else # TaxonName
    a = [ tag.span( full_taxon_name_tag(target).html_safe, class: :otu_tag_taxon_name, title: target.id) ]
    a.push taxon_name_type_short_tag(target)
    t = tag.span( a.compact.join(' ').html_safe, class: :otu_tag )
  end

  if common_names.present? && otu.present? &&
     (common_names_label = otu_common_names_label(common_names, term)).present?
    tag.span( "#{common_names_label} (#{t})".html_safe )
  else
    t
  end
end

#otu_key_inventory(otu) ⇒ Object



378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
# File 'app/helpers/otus_helper.rb', line 378

def otu_key_inventory(otu)
  return {
    observation_matrices: {
      scoped: serialize_matrices(otu.in_scope_observation_matrices),
      in: serialize_matrices(otu.observation_matrices)
    },
    leads: {
      scoped: otu.leads
        .where(parent_id: nil, is_public: true)
        .where('leads.is_virtual IS NOT TRUE')
        .select(:id, :text)
        .map { |l| { id: l.id, text: l.text } },

      in: Lead.public_root_leads_for_leaf_otus(otu)
        .select(:id, :text)
        .map { |l| { id: l.id, text: l.text } }
    }
  }
end


219
220
221
222
# File 'app/helpers/otus_helper.rb', line 219

def otu_link(otu)
  return nil if otu.nil?
  link_to(otu_tag_elements(otu).join(' ').html_safe, otu)
end

#otu_tag(otu) ⇒ Object

a.push content_tag(:span, b.join(' ').html_safe, class: :otu_taxon_name) a.push taxon_name_type_short_tag(otu.taxon_name).html_safe end

tag.span( a.flatten.compact.join(' ').html_safe, class: :otu_tag )

end



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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'app/helpers/otus_helper.rb', line 27

def otu_tag(otu)
  return nil if otu.nil?
  return  tag.span(otu.name, class: :otu_tag) unless otu.taxon_name.present?

  if otu.taxon_name.is_combination?
    if otu.name?
      tag.span(
        [ h(otu.name),
          tag.span(otu.taxon_name.cached_html&.html_safe, class: :otu_tag_taxon_name)
        ].join(' ' + tag.em('==') + ' ').html_safe,
      class: :otu_tag
      )
    else
      tag.span(
        (tag.span(otu.taxon_name.cached_html&.html_safe, class: :otu_tag_taxon_name) + ' ' + TaxonNamesHelper::COMBINATION_MARK).html_safe,
        class: :otu_tag
      )
    end

  else # protonym
    if otu.taxon_name.is_valid?
      if otu.name
        tag.span(
          [ tag.span(otu.taxon_name.cached_html&.html_safe, class: :otu_tag_taxon_name),
            h(otu.name)
          ].compact.join('&nbsp;').html_safe,
        class: :otu_tag
        )
      else
        tag.span(
          tag.span(full_taxon_name_tag(otu.taxon_name), class: :otu_tag_taxon_name),
          class: :otu_tag
        )
      end

    else # invalid taxon name
      tag.span(
        [
          [ tag.span(full_taxon_name_tag(otu.taxon_name), class: [:otu_tag_taxon_name, :otu_tag_taxon_name_invalid]),
            ( otu.name ? h(otu.name) : nil )
          ].compact.join('&nbsp;'),

        tag.span(full_taxon_name_tag(otu.taxon_name.valid_taxon_name), class: :otu_tag_taxon_name)
        ].join(' ' + tag.em('now') + ' ').html_safe,
        class: :otu_tag
      )

    end
  end
end

#otu_tag_elements(otu) ⇒ Object



153
154
155
156
157
158
159
# File 'app/helpers/otus_helper.rb', line 153

def otu_tag_elements(otu)
  return nil if otu.nil?
  [
    ( otu.taxon_name ? tag.span(full_taxon_name_tag(otu.taxon_name).html_safe, class: :otu_tag_taxon_name, title: otu.taxon_name.id) : nil),
    ( otu.name ? (:span, otu.name, class: :otu_tag_otu_name, title: otu.id) : nil )
  ].compact
end


228
229
230
# File 'app/helpers/otus_helper.rb', line 228

def otus_link_list_tag(otus)
  otus.collect { |o| link_to(o.name, o) }.join(',')
end

#otus_radial(object) ⇒ Object



246
247
248
# File 'app/helpers/otus_helper.rb', line 246

def otus_radial(object)
  (:div, '', 'data-global-id' => object.to_global_id.to_s, 'data-otu-radial' => 'true')
end

#otus_radial_disambiguate(object) ⇒ Object



241
242
243
244
# File 'app/helpers/otus_helper.rb', line 241

def otus_radial_disambiguate(object)
  otu = object.metamorphosize
  (:div, '', 'data-taxon-name' => object_tag(otu), 'data-redirect' => 'false', 'data-id' => otu.id, 'data-klass' => object.class.base_class.name.to_s, 'data-otu-button' => 'true')
end

#otus_search_form ⇒ Object



224
225
226
# File 'app/helpers/otus_helper.rb', line 224

def otus_search_form
  render('/otus/quick_search_form')
end

#parent_records(otu) ⇒ Object



290
291
292
# File 'app/helpers/otus_helper.rb', line 290

def parent_records(otu)
  otu.taxon_name&.parent&.otus&.all || []
end

#parents_by_nomenclature(otu) ⇒ Object

See also otus#ancestor_otu_ids ?



295
296
297
298
299
300
301
302
303
304
305
306
307
# File 'app/helpers/otus_helper.rb', line 295

def parents_by_nomenclature(otu)
  above = [ ]
  if otu.taxon_name_id
    TaxonName.ancestors_of(otu.taxon_name)
      .select('taxon_names.*, taxon_name_hierarchies.generations')
      .that_is_valid.joins(:otus)
      .distinct
      .reorder('taxon_name_hierarchies.generations DESC, taxon_names.cached_valid_taxon_name_id').each do |t|
        above.push [t.cached, t.otus.to_a] # TODO: to_a vs. pluck
      end
  end
  above
end

#previous_records(otu) ⇒ Array

Some OTUs don't have TaxonName, skip along until we hit one.

Returns:

  • (Array) —

    of OTUs



273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'app/helpers/otus_helper.rb', line 273

def previous_records(otu)
  if otu.taxon_name_id
    o = []
    t = otu.taxon_name.previous_sibling
    unless t.nil?
      while o.empty?
        break if t.nil?
        o = t.otus.to_a
        t = t.previous_sibling
      end
    end
    o
  else
    super
  end
end

#ranked_otu_table(otus) ⇒ Object



309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'app/helpers/otus_helper.rb', line 309

def ranked_otu_table(otus)
  d = TaxonName.ranked_otus(otu_scope: otus, project_id: sessions_current_project_id)

  tbl = %w{otu_id order family genus species otu_name taxon_name taxon_name_author_year}
  output = StringIO.new
  output.puts ::CSV.generate_line(tbl, col_sep: "\t", encoding: Encoding::UTF_8)

  d.each do |o|
    output.puts ::CSV.generate_line(
      [
        o.id,
        o['order'],
        o['family'],
        o['genus'],
        o['species'],
        o.name,
        o.cached,
        o.cached_author_year
      ],
      col_sep: "\t", encoding: Encoding::UTF_8)
  end

  output.string
end

#serialize_matrices(scope) ⇒ Object



366
367
368
369
370
371
372
373
374
375
376
# File 'app/helpers/otus_helper.rb', line 366

def serialize_matrices(scope)
  scope
    .where(is_public: true)
    .map do |m|
      {
        id: m.id,
        name: m.name,
        is_media: m.is_media_matrix?
      }
    end
end