Module: OtusHelper
- Includes:
- RecordNavigationHelper
- Defined in:
- app/helpers/otus_helper.rb
Instance Method Summary collapse
- #add_aggregate_geo_json(otu, target) ⇒ Object
- #add_distribution_geo_json(otu, target, seen_shapes = nil) ⇒ Object
-
#aggregate_geo_json(otu, target, cached_map_type = 'CachedMapItem::WebLevel1') ⇒ Object
TODO: cleanup.
-
#browse_otu_link(object) ⇒ Object
Stub a smart link to browse OTUs.
-
#build_geo_json_feature_deduped(seen, shape_key) ⇒ Object
private
Yields once with skip_geometry true or false depending on whether
shape_key[shape_type, shape_id] has already been recorded inseen. -
#dwc_gallery_data(otu, dwc_occurrence_id: [], pagination_headers: true) ⇒ Object
Hash { dwc_occurrence_id: [ image1, image2 … ], … }.
- #geojson_for_otu(otu) ⇒ Object
- #geojson_target_for_otu(otu) ⇒ Object
- #label_for_otu(otu) ⇒ Object
-
#next_records(otu) ⇒ Array
Of OTUs.
-
#otu_autocomplete_selected_tag(otu) ⇒ String
No HTML inside <input>.
-
#otu_cached_map(otu, target, cached_map_type = 'CachedMapItem::WebLevel1', cache = true, force = false) ⇒ Object
NOT USED Caching the cached map.
- #otu_common_names_label(common_names, term = nil) ⇒ Object
-
#otu_distribution(otu, children = true, cutoff = 200) ⇒ Object
TODO:.
-
#otu_extended_autocomplete_tag(target, otu, common_names, term) ⇒ Object
Used exclusively in /api/v1/otus/autocomplete.
- #otu_key_inventory(otu) ⇒ Object
- #otu_link(otu) ⇒ Object
- #otu_tag(otu) ⇒ Object
- #otu_tag_elements(otu) ⇒ Object
- #otus_link_list_tag(otus) ⇒ Object
- #otus_radial(object) ⇒ Object
- #otus_radial_disambiguate(object) ⇒ Object
- #otus_search_form ⇒ Object
- #parent_records(otu) ⇒ Object
-
#parents_by_nomenclature(otu) ⇒ Object
See also otus#ancestor_otu_ids ?.
-
#previous_records(otu) ⇒ Array
Some OTUs don’t have TaxonName, skip along until we hit one.
- #ranked_otu_table(otus) ⇒ Object
- #serialize_matrices(scope) ⇒ Object
Methods included from RecordNavigationHelper
Instance Method Details
#add_aggregate_geo_json(otu, target) ⇒ Object
251 252 253 254 255 256 257 258 259 260 261 262 |
# File 'app/helpers/otus_helper.rb', line 251 def add_aggregate_geo_json(otu, target) h = target if g = aggregate_geo_json(otu, h) g['properties'] = {'aggregate': true} g['properties']['target'] = geojson_target_for_otu(otu) h['features'].push g end h end |
#add_distribution_geo_json(otu, target, seen_shapes = nil) ⇒ Object
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 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 |
# File 'app/helpers/otus_helper.rb', line 313 def add_distribution_geo_json(otu, target, seen_shapes = nil) h = target o = otu # internal target t = geojson_target_for_otu(otu) o.current_field_occurrences.each do |f| shape_key = seen_shapes && f.collecting_event&.geo_json_shape_key g = build_geo_json_feature_deduped(seen_shapes&.fetch(:field_occurrences), shape_key) do |skip_geometry| field_occurrence_to_geo_json_feature(f, skip_geometry:) end next unless g g['properties']['target'] = t h['features'].push g end o.current_collection_objects.each do |c| shape_key = seen_shapes && c.collecting_event&.geo_json_shape_key g = build_geo_json_feature_deduped(seen_shapes&.fetch(:collection_objects), shape_key) do |skip_geometry| collection_object_to_geo_json_feature(c, skip_geometry:) end next unless g g['properties']['target'] = t h['features'].push g end o.asserted_distributions.each do |a| shape_key = seen_shapes && [a.asserted_distribution_shape_type, a.asserted_distribution_shape_id] g = build_geo_json_feature_deduped(seen_shapes&.fetch(:asserted_distributions), shape_key) do |skip_geometry| asserted_distribution_to_geo_json_feature(a, skip_geometry:) end next unless g g['properties']['target'] = t h['features'].push g end ba_ids = o.all_biological_associations.map(&:id) unless ba_ids.empty? ::AssertedDistribution .where( asserted_distribution_object_type: 'BiologicalAssociation', asserted_distribution_object_id: ba_ids ) .each do |a| shape_key = seen_shapes && [a.asserted_distribution_shape_type, a.asserted_distribution_shape_id] g = build_geo_json_feature_deduped(seen_shapes&.fetch(:asserted_distributions), shape_key) do |skip_geometry| asserted_distribution_to_geo_json_feature(a, skip_geometry:) end next unless g g['properties']['target'] = t h['features'].push g end bag_ids = ::BiologicalAssociationsGraph .joins(:biological_associations_biological_associations_graphs) .where(biological_associations_biological_associations_graphs: { biological_association_id: ba_ids }) .select(:id) unless bag_ids.empty? ::AssertedDistribution .where( asserted_distribution_object_type: 'BiologicalAssociationsGraph', asserted_distribution_object_id: bag_ids ) .each do |a| shape_key = seen_shapes && [a.asserted_distribution_shape_type, a.asserted_distribution_shape_id] g = build_geo_json_feature_deduped(seen_shapes&.fetch(:asserted_distributions), shape_key) do |skip_geometry| asserted_distribution_to_geo_json_feature(a, skip_geometry:) end next unless g g['properties']['target'] = t h['features'].push g end end end [ [o.depictions, 'Depiction', :depiction_object_id], [o.conveyances, 'Conveyance', :conveyance_object_id], [o.observations, 'Observation', :observation_object_id], ].each do |, object_type, _id_method| = .map(&:id) next if .empty? ::AssertedDistribution .where( asserted_distribution_object_type: object_type, asserted_distribution_object_id: ) .each do |a| shape_key = seen_shapes && [a.asserted_distribution_shape_type, a.asserted_distribution_shape_id] g = build_geo_json_feature_deduped(seen_shapes&.fetch(:asserted_distributions), shape_key) do |skip_geometry| asserted_distribution_to_geo_json_feature(a, skip_geometry:) end next unless g g['properties']['target'] = t h['features'].push g end end o.type_materials.includes(:protonym).each do |e| next unless type_material_is_primary_type(e) && o.taxon_name.cached_is_valid shape_key = seen_shapes && e.collection_object&.collecting_event&.geo_json_shape_key g = build_geo_json_feature_deduped(seen_shapes&.fetch(:type_materials), shape_key) do |skip_geometry| type_material_to_geo_json_feature(e, skip_geometry:) end next unless g g['properties']['target'] = t h['features'].push g end h end |
#aggregate_geo_json(otu, target, cached_map_type = 'CachedMapItem::WebLevel1') ⇒ Object
TODO: cleanup
280 281 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 |
# File 'app/helpers/otus_helper.rb', line 280 def aggregate_geo_json(otu, target, cached_map_type = 'CachedMapItem::WebLevel1') h = target if gj = otu.cached_map_geo_json(cached_map_type) i = { **gj, # 'type' => gj['type'], # 'Feature', 'properties' => { 'base' => geojson_target_for_otu(otu), # 'shape' => { # 'type' => cached_map_type, # 'id' => 99999 }, # was nil 'updated_at' => 'foo' # last updated at on CachedMapItem scope, possibly } } if gj.keys.include?('coordinates') i['coordinates'] = gj['coordinates'] # was 'coordinates' TODO: might not work elsif gj.keys.include?('geometries') i['geometries'] = gj['geometries'] # was 'coordinates' TODO: might not work end i else nil end end |
#browse_otu_link(object) ⇒ Object
Stub a smart link to browse OTUs
98 99 100 101 102 |
# File 'app/helpers/otus_helper.rb', line 98 def browse_otu_link(object) return nil if object.nil? otu = object. content_tag(: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 |
#build_geo_json_feature_deduped(seen, shape_key) ⇒ Object (private)
Yields once with skip_geometry true or false depending on whether shape_key
- shape_type, shape_id
-
has already been recorded in
seen.
First occurrence of a shape: yields skip_geometry=false (full geometry fetched).
The shape is recorded in +seen+ only if the block returns a non-nil feature,
so shapes that produce no feature (e.g. no geographic item) are never marked seen.
Subsequent occurrences: yields skip_geometry=true (geometry skipped, nil in feature).
When seen or shape_key is nil (deduplication disabled or no shape available), yields skip_geometry=false unconditionally.
529 530 531 532 533 534 535 536 537 538 539 540 541 542 |
# File 'app/helpers/otus_helper.rb', line 529 def build_geo_json_feature_deduped(seen, shape_key) if seen.nil? || shape_key.nil? || shape_key.first.nil? return yield(false) end key = "#{shape_key[0]}_#{shape_key[1]}" if seen.key?(key) yield(true) else g = yield(false) seen[key] = true if g g end end |
#dwc_gallery_data(otu, dwc_occurrence_id: [], pagination_headers: true) ⇒ Object
Returns Hash { dwc_occurrence_id: [ image1, image2 … ], … }.
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 |
# File 'app/helpers/otus_helper.rb', line 456 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 |
#geojson_for_otu(otu) ⇒ Object
233 234 235 236 237 238 239 240 241 |
# File 'app/helpers/otus_helper.rb', line 233 def geojson_for_otu(otu) { 'type' => 'FeatureCollection', 'features' => [], 'properties' => { 'target' => geojson_target_for_otu(otu) } } end |
#geojson_target_for_otu(otu) ⇒ Object
243 244 245 246 247 248 249 |
# File 'app/helpers/otus_helper.rb', line 243 def geojson_target_for_otu(otu) { 'id' => otu.id, 'label' => label_for_otu(otu), 'type' => 'Otu' } end |
#label_for_otu(otu) ⇒ Object
11 12 13 14 15 16 |
# File 'app/helpers/otus_helper.rb', line 11 def label_for_otu(otu) return nil if otu.nil? [otu.name, label_for_taxon_name(otu.taxon_name) ].compact.join(': ') end |
#next_records(otu) ⇒ Array
Returns of OTUs.
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'app/helpers/otus_helper.rb', line 115 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
Returns no HTML inside <input>.
75 76 77 78 79 80 |
# File 'app/helpers/otus_helper.rb', line 75 def otu_autocomplete_selected_tag(otu) return nil if otu.nil? || (otu.new_record? && !otu.changed?) [otu.name, Utilities::Strings.nil_wrap('[',taxon_name_autocomplete_selected_tag(otu.taxon_name), ']')&.html_safe ].compact.join(' ') end |
#otu_cached_map(otu, target, cached_map_type = 'CachedMapItem::WebLevel1', cache = true, force = false) ⇒ Object
NOT USED Caching the cached map
266 267 268 269 270 271 272 273 274 275 276 277 |
# File 'app/helpers/otus_helper.rb', line 266 def otu_cached_map(otu, target, cached_map_type = 'CachedMapItem::WebLevel1', cache = true, force = false) r = nil if force r = aggregate_geo_json(otu, target, cached_map_type) else # Check for map # TODO: extend with synced check if a = CachedMap.where(project_id: sessions_current_project_id).where(otu_id: otu.id, cached_map_type: ) end end end |
#otu_common_names_label(common_names, term = nil) ⇒ Object
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 |
# File 'app/helpers/otus_helper.rb', line 44 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_distribution(otu, children = true, cutoff = 200) ⇒ Object
TODO:
* make properties universal
type: 'Model',
id: id,
label: <label>
-
merge origin_otu_id: id to reference coordinate OTUs
target:
type: Otu
label:
id
base: # one level above target (or one level below shape?!)
type:
id:
shape: # Either GeographicArea or Georeference
type
id
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
# File 'app/helpers/otus_helper.rb', line 206 def otu_distribution(otu, children = true, cutoff = 200) return {} if otu.nil? otus = if children otu.coordinate_otus_with_children else Otu.coordinate_otus(otu.id) end h = geojson_for_otu(otu) if otu.taxon_name && otu.taxon_name.is_protonym? && !otu.taxon_name.is_species_rank? add_aggregate_geo_json(otu, h) else seen_shapes = { field_occurrences: {}, collection_objects: {}, asserted_distributions: {}, type_materials: {} } otus.each do |o| add_distribution_geo_json(o, h, seen_shapes) end end h end |
#otu_extended_autocomplete_tag(target, otu, common_names, term) ⇒ Object
Used exclusively in /api/v1/otus/autocomplete
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'app/helpers/otus_helper.rb', line 27 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
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 |
# File 'app/helpers/otus_helper.rb', line 498 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) .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 |
#otu_link(otu) ⇒ Object
82 83 84 85 |
# File 'app/helpers/otus_helper.rb', line 82 def otu_link(otu) return nil if otu.nil? link_to(otu_tag_elements(otu).join(' ').html_safe, otu) end |
#otu_tag(otu) ⇒ Object
4 5 6 7 8 9 |
# File 'app/helpers/otus_helper.rb', line 4 def otu_tag(otu) return nil if otu.nil? a = otu_tag_elements(otu) a.push taxon_name_type_short_tag(otu.taxon_name) content_tag(:span, a.compact.join(' ').html_safe, class: :otu_tag) end |
#otu_tag_elements(otu) ⇒ Object
18 19 20 21 22 23 24 |
# File 'app/helpers/otus_helper.rb', line 18 def otu_tag_elements(otu) return nil if otu.nil? [ ( otu.name ? content_tag(:span, otu.name, class: :otu_tag_otu_name, title: otu.id) : nil ), ( otu.taxon_name ? content_tag(:span, full_taxon_name_tag(otu.taxon_name).html_safe, class: :otu_tag_taxon_name, title: otu.taxon_name.id) : nil) ].compact end |
#otus_link_list_tag(otus) ⇒ Object
91 92 93 |
# File 'app/helpers/otus_helper.rb', line 91 def otus_link_list_tag(otus) otus.collect { |o| link_to(o.name, o) }.join(',') end |
#otus_radial(object) ⇒ Object
109 110 111 |
# File 'app/helpers/otus_helper.rb', line 109 def otus_radial(object) content_tag(:div, '', 'data-global-id' => object.to_global_id.to_s, 'data-otu-radial' => 'true') end |
#otus_radial_disambiguate(object) ⇒ Object
104 105 106 107 |
# File 'app/helpers/otus_helper.rb', line 104 def otus_radial_disambiguate(object) otu = object. content_tag(: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
87 88 89 |
# File 'app/helpers/otus_helper.rb', line 87 def otus_search_form render('/otus/quick_search_form') end |
#parent_records(otu) ⇒ Object
153 154 155 |
# File 'app/helpers/otus_helper.rb', line 153 def parent_records(otu) otu.taxon_name&.parent&.otus&.all || [] end |
#parents_by_nomenclature(otu) ⇒ Object
See also otus#ancestor_otu_ids ?
158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'app/helpers/otus_helper.rb', line 158 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.
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'app/helpers/otus_helper.rb', line 136 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
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 |
# File 'app/helpers/otus_helper.rb', line 429 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. ], col_sep: "\t", encoding: Encoding::UTF_8) end output.string end |
#serialize_matrices(scope) ⇒ Object
486 487 488 489 490 491 492 493 494 495 496 |
# File 'app/helpers/otus_helper.rb', line 486 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 |