Module: Export::CSV::Dwc::Extension::Checklist::Description

Defined in:
lib/export/csv/dwc/extension/checklist/description.rb

Overview

CSV for Description extension (for checklist archives). See http://rs.gbif.org/extension/gbif/1.0/description.xml

Exports Content records (OTU text descriptions by topic) as html.

Constant Summary collapse

GBIF =
Export::Dwca::GbifProfile::SpeciesDescription
CHECKLIST_FIELDS =

Fields used in checklist exports (subset of full GBIF profile).

[
  :id, # Required for DwC-A star joins (taxonID, an OTU UUID)
  :description,
  :type,
  :language,
  :created
].freeze
HEADERS =
CHECKLIST_FIELDS
HEADERS_NAMESPACES =
CHECKLIST_FIELDS.map do |field|
  field == :id ? '' : GBIF::NAMESPACES[field]
end.freeze

Class Method Summary collapse

Class Method Details

.csv(core_otu_scope, taxon_name_id_to_taxon_id, accepted_name_mode:, description_topics: []) ⇒ String

Generate CSV for description extension from Content records.

Parameters:

  • core_otu_scope (Hash)

    OTU query params from Checklist::Data

  • taxon_name_id_to_taxon_id (Hash)

    taxon_name_id => OTU UUID (used as dwc:taxonID in the checklist core)

  • accepted_name_mode (String)

    checklist synonym handling mode

  • description_topics (Array<Integer>) (defaults to: [])

    ordered array of topic IDs to include

Returns:

  • (String)

    CSV content



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
77
78
79
80
81
# File 'lib/export/csv/dwc/extension/checklist/description.rb', line 30

def self.csv(core_otu_scope, taxon_name_id_to_taxon_id, accepted_name_mode:, description_topics: [])
  tbl = []
  tbl[0] = HEADERS

  return ::Export::Dwca.output_csv(tbl) if description_topics.empty?

  otu_scope = ::Queries::Otu::Filter.new(core_otu_scope).all

  # Only include published (public) contents
  contents = Content
    .joins(:otu)
    .merge(otu_scope)
    .where(topic_id: description_topics)
    .joins(:public_content)
    .includes(:language, :topic, otu: :taxon_name)

  # Sort by topic order as specified by user
  topic_order = description_topics.each_with_index.to_h
  contents = contents.sort_by { |c| topic_order[c.topic_id] || Float::INFINITY }

  contents.each do |content|
    taxon_name = content.otu.taxon_name
    taxon_name_id = if accepted_name_mode == ::Export::Dwca::Checklist::Data::ACCEPTED_NAME_USAGE_ID
      taxon_name.id
    else
      taxon_name.cached_valid_taxon_name_id || taxon_name.id
    end
    taxon_id = taxon_name_id_to_taxon_id[taxon_name_id]
    next unless taxon_id

    html_description = if content.text.present?
      content.to_html
    else
      nil
    end

    # Format created date from updated_at
    created_date = content.updated_at.strftime('%Y-%m-%d')

    row = [
      taxon_id,
      html_description,
      content.topic.name,
      content.language&.alpha_2,
      created_date
    ]

    tbl << row
  end

  ::Export::Dwca.output_csv(tbl)
end