Module: CollectionObjectsHelper

Defined in:
app/helpers/collection_objects_helper.rb

Instance Method Summary collapse

Instance Method Details

#collection_object_autocomplete_tag(collection_object) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'app/helpers/collection_objects_helper.rb', line 50

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



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

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



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

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



267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'app/helpers/collection_objects_helper.rb', line 267

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



96
97
98
99
100
101
102
103
104
# File 'app/helpers/collection_objects_helper.rb', line 96

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



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

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


27
28
29
30
# File 'app/helpers/collection_objects_helper.rb', line 27

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



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

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



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'app/helpers/collection_objects_helper.rb', line 211

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



304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
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
# File 'app/helpers/collection_objects_helper.rb', line 304

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



62
63
64
65
66
67
68
69
70
71
72
73
# File 'app/helpers/collection_objects_helper.rb', line 62

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


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

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



138
139
140
141
142
143
# File 'app/helpers/collection_objects_helper.rb', line 138

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)


244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'app/helpers/collection_objects_helper.rb', line 244

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



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'app/helpers/collection_objects_helper.rb', line 119

def collection_object_visualized_identifier(collection_object)
  return nil if collection_object.nil?

  # We now return the first Local identifier by default here
  # This accomodates RecordNumber vs CatalogNumber
  i = collection_object.identifiers.order(Arel.sql("CASE \
            WHEN identifiers.type IN ('Identifier::Local::CatalogNumber', 'Identifier::Local::RecordNumber') THEN 0  \
            ELSE 1                                                                                        \
          END, \
        identifiers.position")).first

  return [:collection_object, identifier_tag(i)] if i

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

#collection_objects_search_formObject



75
76
77
# File 'app/helpers/collection_objects_helper.rb', line 75

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

#dwc_occurrence_table_body_tag(collection_objects) ⇒ Object



151
152
153
154
155
# File 'app/helpers/collection_objects_helper.rb', line 151

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: synchronize with class methods



147
148
149
# File 'app/helpers/collection_objects_helper.rb', line 147

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



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

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



174
175
176
177
178
179
180
181
182
# File 'app/helpers/collection_objects_helper.rb', line 174

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



157
158
159
160
161
162
# File 'app/helpers/collection_objects_helper.rb', line 157

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



36
37
38
39
40
41
# File 'app/helpers/collection_objects_helper.rb', line 36

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

#label_for_collection_object_container(collection_object) ⇒ Object



43
44
45
46
47
48
# File 'app/helpers/collection_objects_helper.rb', line 43

def label_for_collection_object_container(collection_object)
  return nil if collection_object.nil?
  collection_object.dwc_catalog_number ||
    collection_object.dwc_scientific_name ||
    collection_object.id
end

#radial_quick_forms_tag(object) ⇒ Object



32
33
34
# File 'app/helpers/collection_objects_helper.rb', line 32

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



355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
# File 'app/helpers/collection_objects_helper.rb', line 355

def table_example(collection_objects)
  cols = %i{
    class
    b
    c
  }

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

    collection_objects.collect{|co|
      tag.tr +
        tag.td( co.dwc_class) +
        tag.td( co.dwc_order) +
        tag.td( co.dwc_family) +
        tag.td( co.dwc_sex)

    }.join.html_safe
  end.html_safe
end


79
80
81
82
# File 'app/helpers/collection_objects_helper.rb', line 79

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