Module: CollectionObjectsHelper

Defined in:
app/helpers/collection_objects_helper.rb

Instance Method Summary collapse

Instance Method Details

#collection_object_autocomplete_tag(collection_object) ⇒ Object



69
70
71
72
73
74
75
76
77
# File 'app/helpers/collection_objects_helper.rb', line 69

def collection_object_autocomplete_tag(collection_object)
  return nil if collection_object.nil?
  [
    collection_object_loan_tag(collection_object),
    collection_object_deaccession_tag(collection_object),
    collection_object_identifier_tag(collection_object),
    collection_object_taxon_determination_tag(collection_object)
  ].join(' ').html_safe
end

#collection_object_browse_next_by_identifier(collection_object) ⇒ link_to

Returns this may not work for all identifier types, i.e. those with identifiers like ‘123.34` or `3434.33X` may not increment correctly.

Returns:

  • (link_to)

    this may not work for all identifier types, i.e. those with identifiers like ‘123.34` or `3434.33X` may not increment correctly



215
216
217
218
219
220
221
222
223
224
# File 'app/helpers/collection_objects_helper.rb', line 215

def collection_object_browse_next_by_identifier(collection_object)
  return nil if collection_object.nil?
  o = collection_object.next_by_identifier
  return (:div, 'None', 'class' => 'navigation-item disable') if o.nil?
  link_text = (:span, 'Next by id', 'class' => 'small-icon icon-right', 'data-icon' => 'arrow-right')
  link_to(link_text, browse_collection_objects_task_path(collection_object_id: o.id),
          data: {arrow: :next,
                 'no-turbolinks' => 'false',
                 help: 'Sorts by identifier type, namespace, then an conversion of identifier into integer.  Will not work for all identifier types.'}, class:'navigation-item')
end

#collection_object_browse_previous_by_identifier(collection_object) ⇒ link_to

Returns this may not work for all identifier types, i.e. those with identifiers like ‘123.34` or `3434.33X` may not increment correctly.

Returns:

  • (link_to)

    this may not work for all identifier types, i.e. those with identifiers like ‘123.34` or `3434.33X` may not increment correctly



201
202
203
204
205
206
207
208
209
210
211
# File 'app/helpers/collection_objects_helper.rb', line 201

def collection_object_browse_previous_by_identifier(collection_object)
  return nil if collection_object.nil?
  o = collection_object.previous_by_identifier
  return (:div, 'None', 'class' => 'navigation-item disable') if o.nil?
  link_text = (:span, 'Previous by id', 'class' => 'small-icon icon-left', 'data-icon' => 'arrow-left')
  link_to(link_text, browse_collection_objects_task_path(collection_object_id: o.id), data: {
    arrow: :previous,
    'no-turbolinks' => 'true',
    help: 'Sorts by identifier type, namespace, then an conversion of identifier into integer.  Will not work for all identifier types.'},
    class: 'navigation-item')
end

#collection_object_count_by_classification(scope = nil) ⇒ Object

Perhaps a /lib/catalog method

Returns:

  • Hash



282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# File 'app/helpers/collection_objects_helper.rb', line 282

def collection_object_count_by_classification(scope = nil)

  return [] if scope.nil?
  specimen_data = {}
  lot_data = {}

  total_index = {}

  scope.each do |n|
    a = ::Queries::CollectionObject::Filter.new(project_id: sessions_current_project_id, taxon_name_id: n.id, descendants: true,  collection_object_type: 'Specimen').all
    b = ::Queries::CollectionObject::Filter.new(project_id: sessions_current_project_id, taxon_name_id: n.id, descendants: true, collection_object_type: 'Lot').all

    ts = CollectionObject.where(id: a).calculate(:sum, :total)
    tl = CollectionObject.where(id: b).calculate(:sum, :total)

    if (ts > 0) || (tl > 0)
      lot_data[n.cached] = tl
      specimen_data[n.cached] = ts
      total_index[n.cached] = ts + tl
    end

  end

  # We only need to sort 1 pile!
  specimen_data = specimen_data.sort{|a,b| total_index[b[0]] <=> total_index[a[0]] }.to_h

  return {
    total_index:,
    data: [
      { name: 'Specimen', data: specimen_data},
      { name: 'Lot', data: lot_data}
    ]
  }
end

#collection_object_deaccession_tag(collection_object) ⇒ Object



115
116
117
118
119
120
121
122
123
# File 'app/helpers/collection_objects_helper.rb', line 115

def collection_object_deaccession_tag(collection_object)
  return nil if collection_object.nil? || (collection_object.deaccession_reason.blank? && collection_object.deaccessioned_at.nil?)
  msg = ['DEACCESSIONED"', collection_object.deaccession_reason, collection_object.deaccessioned_at&.year].compact.join(' - ')
  (:span, msg, class: [
    :feedback,
    'feedback-thin',
    'feedback-danger'
  ]).html_safe
end

#collection_object_identifier_tag(collection_object) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
# File 'app/helpers/collection_objects_helper.rb', line 103

def collection_object_identifier_tag(collection_object)
  return nil if collection_object.nil?
  t, i = collection_object_visualized_identifier(collection_object)

  return (:span, i, class: [
    :feedback,
    'feedback-thin',
    (t == :collection_object ? 'feedback-primary' : 'feedback-warning')
  ]) if i
  (:span, 'no identifier assigned', class: [:feedback, 'feedback-thin', 'feedback-warning'])
end


53
54
55
56
# File 'app/helpers/collection_objects_helper.rb', line 53

def collection_object_link(collection_object)
  return nil if collection_object.nil?
  link_to(collection_object_tag(collection_object).html_safe, collection_object.metamorphosize)
end

#collection_object_loan_tag(collection_object) ⇒ Object



125
126
127
128
129
130
131
132
133
# File 'app/helpers/collection_objects_helper.rb', line 125

def collection_object_loan_tag(collection_object)
  return nil if collection_object.nil? || !collection_object.on_loan?
  msg = collection_object.loan_return_date ? 'On Loan until ' + collection_object.loan_return_date.to_s : 'Gifted'
  (:span, msg, class: [
    :feedback,
    'feedback-thin',
    'feedback-warning'
  ]).html_safe
end

#collection_object_metadata_badge(collection_object) ⇒ Object



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'app/helpers/collection_objects_helper.rb', line 226

def (collection_object)
  return nil if collection_object.nil?
  o = collection_object

  layout = Waxy::Geometry::Layout.new(
    Waxy::Geometry::Orientation::LAYOUT_POINTY,
    Waxy::Geometry::Point.new(14,14), # size
    Waxy::Geometry::Point.new(14,14), # start
    0 # padding
  )

  s = [
    (o.identifiers.any? ? 1 : 0),
    (o.taxon_determinations.any? ? 1 : 0),
    (o.collecting_event&.map_center.nil? ? 0 : 1),
    (o.collecting_event_id ? 1 : 0),
    (o.buffered_determinations.blank? ? 0 : 1),
    (o.buffered_collecting_event.blank? ? 0 : 1),
  ]

  a = Waxy::Meta.new
  a.size = s
  a.stroke = 'grey'
  a.link_title = "#{o.id} created #{time_ago_in_words(o.created_at)} ago by #{user_tag(o.creator)}"

  c = Waxy::Render::Svg::Canvas.new(28, 28)
  c.body << Waxy::Render::Svg.rectangle(layout, [a], 0)
  c.to_svg
end

#collection_object_preparation_by_classification(scope = nil) ⇒ Object

Perhaps a /lib/catalog method

Returns:

  • Hash



319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
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
365
366
367
368
# File 'app/helpers/collection_objects_helper.rb', line 319

def collection_object_preparation_by_classification(scope = nil)
  return [] if scope.nil?
  data = {}
  no_data = {}

  preparations = ::PreparationType.joins(:collection_objects).where(collection_objects: {project_id: sessions_current_project_id}).distinct

  i = 0
  scope.each do |n|

    j = []

    a = ::Queries::CollectionObject::Filter.new(
      project_id: sessions_current_project_id,
      taxon_name_id:  n.id,
      descendants: true
    )

    # Yes a custom query could do this much faster
    preparations.each do |p|
      a.preparation_type_id = p.id
      t = CollectionObject.where(id: a.all).calculate(:sum, :total)
      if t > 0
        j.push [p.name, t]
        i += 1
      end
    end

    if j.empty?
      # There are no data at all, don't query for Missing
      no_data[n.id] = n.cached
    else
      a.preparation_type_id = nil
      a.preparation_type = false
      w = CollectionObject.where(id: a.all).calculate(:sum, :total)
      if w > 0
        j.push ['Missing', w]
      end

      data[n.cached] = j
    end
  end

  return {
    labels: preparations.collect{|p| p.name} + ['Missing'],
    data:,
    no_data:
  }

end

#collection_object_scientific_name(collection_object) ⇒ Object

Text only, taxon name cached or OTU name for the most recent determination



81
82
83
84
85
86
87
88
89
90
91
92
# File 'app/helpers/collection_objects_helper.rb', line 81

def collection_object_scientific_name(collection_object)
  return nil if collection_object.nil?
  if a = collection_object.taxon_determinations.order(:position)&.first
    if a.otu.taxon_name
      a.otu.taxon_name.cached
    else
      a.otu.name
    end
  else
    nil
  end
end

#collection_object_tag(collection_object) ⇒ Object

Return [String, nil]

a descriptor including the identifier and determination


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/helpers/collection_objects_helper.rb', line 31

def collection_object_tag(collection_object)
  return nil if collection_object.nil?
  a = [
    collection_object_loan_tag(collection_object),
    collection_object_deaccession_tag(collection_object),
    collection_object_identifier_tag(collection_object),
    taxon_determination_tag(collection_object.taxon_determinations.order(:position).first)
  ].compact

  if a.empty?
    a << [
      collection_object.buffered_collecting_event,
      collection_object.buffered_determinations,
      collection_object.buffered_other_labels
    ].compact
  end

  a << "[#{collection_object.type[(0..2)].capitalize}]"

  a.join('&nbsp;').html_safe
end

#collection_object_taxon_determination_tag(collection_object) ⇒ Object



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

def collection_object_taxon_determination_tag(collection_object)
  return nil if collection_object.nil?
  i = taxon_determination_tag(collection_object.taxon_determinations.order(:position).first)
  return (:span, i, class: [:feedback, 'feedback-thin', 'feedback-secondary']) if i
  nil
end

#collection_object_to_geo_json_feature(collection_object, base = true) ⇒ GeoJSON feature?

Parameters:

  • base (Boolean) (defaults to: true)

    wehther to annotate the feature properties with TW ‘base’ attributes

Returns:

  • (GeoJSON feature, nil)


259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'app/helpers/collection_objects_helper.rb', line 259

def collection_object_to_geo_json_feature(collection_object, base = true)
  return nil if collection_object.nil?
  if a = collecting_event_to_geo_json_feature(collection_object.collecting_event)
    l = label_for_collection_object(collection_object)
    a['properties']['target'] = {
      'type' => 'CollectionObject',
      'id' => collection_object.id,
      'label' => l
    }
    if base
      a['properties']['base'] =  {
        'type' => 'CollectionObject',
        'id' => collection_object.id,
        'label' => l}
    end
    a
  else
    nil
  end
end

#collection_object_visualized_identifier(collection_object) ⇒ Array [Identifier, String (type)]?

Returns also checks virtual container for identifier by proxy.

Returns:

  • (Array [Identifier, String (type)], nil)

    also checks virtual container for identifier by proxy



138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'app/helpers/collection_objects_helper.rb', line 138

def collection_object_visualized_identifier(collection_object)
  return nil if collection_object.nil?
  # Get the Identifier::Local::Catalog number on collection_object, or immediate containing Container
  i = collection_object.preferred_catalog_number # see delegation in collection_object.rb

  # Get some other identifier on collection_object
  i ||= collection_object.identifiers.order(:position)&.first
  return  [:collection_object, identifier_tag(i)] if i

  # Get some other identifier on container
  j = collection_object&.container&.identifiers&.first
  return [:container, identifier_tag(j)] if j
  nil
end

#collection_objects_search_formObject



94
95
96
# File 'app/helpers/collection_objects_helper.rb', line 94

def collection_objects_search_form
  render('/collection_objects/quick_search_form')
end

#dwc_occurrence_table_body_tag(collection_objects) ⇒ Object



166
167
168
169
170
# File 'app/helpers/collection_objects_helper.rb', line 166

def dwc_occurrence_table_body_tag(collection_objects)
  collection_objects.collect do |c|
    dwc_occurrence_table_row_stub(c).html_safe
  end.join.html_safe
end

#dwc_occurrence_table_header_tagObject

TODO: Isolate into own helper TODO: syncronize with class methods



162
163
164
# File 'app/helpers/collection_objects_helper.rb', line 162

def dwc_occurrence_table_header_tag
  (:tr, CollectionObject::DwcExtensions::DWC_OCCURRENCE_MAP.keys.collect{|k| (:th, k)}.join.html_safe, class: [:error])
end

#dwc_occurrence_table_row_stub(collection_object) ⇒ Object



179
180
181
182
183
184
185
186
187
# File 'app/helpers/collection_objects_helper.rb', line 179

def dwc_occurrence_table_row_stub(collection_object)
  r = collection_object.dwc_occurrence
  if r
    dwc_occurrence_table_row_tag(r)
  else
    id = collection_object.to_param
    (:tr, nil, id: "dwc_row_stub_#{id}", data: {'collection-object-id': id}, class: 'dwc_row_stub' )
  end
end

#dwc_occurrence_table_row_tag(dwc_occurrence) ⇒ Object



189
190
191
192
193
194
195
196
197
# File 'app/helpers/collection_objects_helper.rb', line 189

def dwc_occurrence_table_row_tag(dwc_occurrence)
  o = metamorphosize_if(dwc_occurrence.dwc_occurrence_object)
  (:tr, class: :contextMenuCells) do
    [CollectionObject::DwcExtensions::DWC_OCCURRENCE_MAP.keys.collect{|k| (:td, dwc_occurrence.send(k))}.join,
     fancy_show_tag(o),
     fancy_edit_tag(o)
    ].join.html_safe
  end
end

#dwc_table(collection_objects) ⇒ Object



172
173
174
175
176
177
# File 'app/helpers/collection_objects_helper.rb', line 172

def dwc_table(collection_objects)
  (:table) do
    dwc_occurrence_table_header_tag +
      dwc_occurrence_table_body_tag(collection_objects)
  end
end

#label_for_collection_object(collection_object) ⇒ Object



62
63
64
65
66
67
# File 'app/helpers/collection_objects_helper.rb', line 62

def label_for_collection_object(collection_object)
  return nil if collection_object.nil?
  [ 'CollectionObject ' + collection_object.id.to_s,
    identifier_list_labels(collection_object)
  ].compact.join('; ')
end

#radial_quick_forms_tag(object) ⇒ Object



58
59
60
# File 'app/helpers/collection_objects_helper.rb', line 58

def radial_quick_forms_tag(object)
  (:span, '', data: { "global-id": object.to_global_id.to_s, 'radial-quick-forms': 'true'})
end

#table_example(collection_objects) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/helpers/collection_objects_helper.rb', line 3

def table_example(collection_objects)
  cols = %i{
    class
    order
    family
    genus
    scientificName
    sex
  }

  tag.table do
    tag.tr { cols.collect{|h| tag.th(h.to_s) }.join.html_safe } +

    collection_objects.collect{|co|
      tag.tr do
        (tag.td( co.dwc_class) +
        tag.td( co.dwc_order) +
        tag.td( co.dwc_family) +
        tag.td( co.dwc_genus) +
        tag.td( co.dwc_scientific_name) +
        tag.td( co.dwc_sex)).html_safe
      end
    }.join.html_safe
  end.html_safe
end


98
99
100
101
# File 'app/helpers/collection_objects_helper.rb', line 98

def verify_accessions_task_link(collection_object)
  priority = [collection_object.container, collection_object.identifiers.first, collection_object ].compact.first
  link_to('Verify', verify_accessions_task_path(by: priority.metamorphosize.class.name.tableize.singularize.to_sym, id: priority.to_param))
end