Class: WorkflowInstance::IndexCsvBuilder

Inherits:
CsvBuilder
  • Object
show all
Defined in:
lib/generators/dunlop/install/workflow/templates/csv_builders/workflow_instance/index_csv_builder.rb

Constant Summary collapse

BASE_COLUMNS =
%i[

Instance Method Summary collapse

Methods included from Dunlop::CsvBuilder

#data, #date_number, #initialize, #record_attribute, #workflow_instances_in_batch

Instance Method Details

#file_nameObject



66
67
68
# File 'lib/generators/dunlop/install/workflow/templates/csv_builders/workflow_instance/index_csv_builder.rb', line 66

def file_name
  "#{Rails.application.config.application_name}-export-#{date_number}.csv"
end

#headersObject



4
5
6
7
8
9
10
11
12
13
# File 'lib/generators/dunlop/install/workflow/templates/csv_builders/workflow_instance/index_csv_builder.rb', line 4

def headers
  columns = [workflow_instance_class.model_name.human]
  columns += BASE_COLUMNS.map{|c| workflow_instance_class.human_attribute_name(c)}
  # Workflow steps
  workflow_step_mapping.each do |klass, attributes|
    columns << klass.model_name.human # state column
    columns += attributes.map{|attr| klass.human_attribute_name(attr)}
  end
  columns
end

#serialize_row(record) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/generators/dunlop/install/workflow/templates/csv_builders/workflow_instance/index_csv_builder.rb', line 32

def serialize_row(record)
  columns = [record.try(:presentation_name)]
  columns += BASE_COLUMNS.map{|c| record_attribute(record, c) }
  # Workflow steps
  workflow_step_mapping.each do |workflow_step_class, attributes|
    unless workflow_step = record.public_send(workflow_step_class.process_name)
      columns += Array.new(1 + attributes.size) # state + attributes Empty fill
      next
    end
    columns << workflow_step.state
    columns += attributes.map{|attr| record_attribute(workflow_step, attr) }
  end
  columns
end

#serialized_rowsObject

It could all be done using a .map instead of a find_each with fallback each into an array, but for large exports this does not blow up the queries that may have many association includes. Therefore the .find_each can be useful for exports of over 5000 records



23
24
25
26
27
28
29
30
# File 'lib/generators/dunlop/install/workflow/templates/csv_builders/workflow_instance/index_csv_builder.rb', line 23

def serialized_rows
  rows = []
  iteration_method = workflow_instances.respond_to?(:find_each) ? :find_each : :each
  workflow_instances.public_send(iteration_method) do |workflow_instance|
    rows.push serialize_row(workflow_instance)
  end
  rows
end

#workflow_instance_classObject



62
63
64
# File 'lib/generators/dunlop/install/workflow/templates/csv_builders/workflow_instance/index_csv_builder.rb', line 62

def workflow_instance_class
  controller.workflow_instance_class
end

#workflow_instancesObject



15
16
17
# File 'lib/generators/dunlop/install/workflow/templates/csv_builders/workflow_instance/index_csv_builder.rb', line 15

def workflow_instances
  resource
end

#workflow_step_mappingObject

Create a mapping of (klass => attributes):

{
  WorkflowStepClass => ['notes', 'workflow_step_action_executed', ...],
  ....
}


52
53
54
55
56
57
58
59
60
# File 'lib/generators/dunlop/install/workflow/templates/csv_builders/workflow_instance/index_csv_builder.rb', line 52

def workflow_step_mapping
  @workflow_step_mapping ||= begin
    workflow_instance_class.workflow_step_classes.each.with_object({}) do |klass, h|
      attributes = klass.collection.attributes.keys
      attributes -= ['notes'] unless options[:include_notes].present?
      h[klass] = attributes
    end
  end
end