Class: Tasks::Otus::MaterialExaminedController

Inherits:
ApplicationController
  • Object
show all
Includes:
TaskControllerConfiguration
Defined in:
app/controllers/tasks/otus/material_examined_controller.rb

Constant Summary collapse

MAX_OTUS =
100
VALID_ORDER_KEYS =
::Utilities::MaterialExamined::LOOP_VARIABLES.keys.map(&:to_s).freeze

Instance Method Summary collapse

Methods included from TaskControllerConfiguration

#set_is_task_controller

Instance Method Details

#indexObject



6
7
# File 'app/controllers/tasks/otus/material_examined_controller.rb', line 6

def index
end

#parse_order_paramObject (private)



37
38
39
40
41
42
43
# File 'app/controllers/tasks/otus/material_examined_controller.rb', line 37

def parse_order_param
  raw = params[:order].to_a.map(&:to_s)
  # 'identifier' from the UI represents the identifier_namespace+identifier pair.
  expanded = raw.flat_map { |k| k == 'identifier' ? %w[identifier_namespace identifier] : k }
  valid = expanded.select { |k| VALID_ORDER_KEYS.include?(k) }.map(&:to_sym)
  valid.any? ? valid : ::Utilities::MaterialExamined::DEFAULT_ORDER
end

#previewObject

POST /tasks/otus/material_examined/preview.json



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/controllers/tasks/otus/material_examined_controller.rb', line 10

def preview
  otu_ids = Array.wrap(params[:otu_id]).first(MAX_OTUS).map(&:to_i).uniq.compact
  order   = parse_order_param
  todo    = ActiveModel::Type::Boolean.new.cast(params[:todo])

  results = otu_ids.map do |otu_id|
    otu      = ::Otu.where(project_id: sessions_current_project_id).find(otu_id)
    renderer = ::Export::Helpers::MaterialExamined.renderer_for_otu(otu, order:, todo:)
    text     = renderer.render
    html     = text.empty? ? '' : MARKDOWN_HTML.render(text)

    todo_items = renderer.todo_occurrence_ids.uniq.filter_map do |occ_id|
      aug = renderer.augmentations[occ_id]
      next unless aug&.dig(:edit_link)
      { label: aug[:label] || occ_id, url: aug[:edit_link] }
    end

    { otu_id: otu.id, label: helpers.label_for_otu(otu), text:, html:, todo_items: }
  end

  render json: { results: }
end