Module: Dunlop::CsvBuilder

Extended by:
ActiveSupport::Concern
Included in:
CsvBuilder
Defined in:
lib/dunlop/csv_builder.rb

Instance Method Summary collapse

Instance Method Details

#dataObject



38
39
40
41
42
43
44
45
46
# File 'lib/dunlop/csv_builder.rb', line 38

def data
  str = CSV.generate csv_options do |csv|
    csv << headers if headers.present?
    serialized_rows.each do |serialized_row|
      csv << serialized_row
    end
  end
  str
end

#date_numberObject



48
49
50
# File 'lib/dunlop/csv_builder.rb', line 48

def date_number
  Date.today.to_s(:number)
end

#file_nameObject



17
18
19
# File 'lib/dunlop/csv_builder.rb', line 17

def file_name
  options[:file_name] || "#{Rails.application.config.application_name}-#{date_number}-data.csv"
end

#headersObject



21
22
23
# File 'lib/dunlop/csv_builder.rb', line 21

def headers
  []
end

#initialize(resource, options = {}) ⇒ Object



11
12
13
14
15
# File 'lib/dunlop/csv_builder.rb', line 11

def initialize(resource, options = {})
  @resource, @options = resource, options
  @csv_options = default_csv_options.merge(options.slice(:col_sep, :row_sep, :encoding, :field_size_limit, :quote_char))
  @controller = options[:controller]
end

#record_attribute(record, attribute_name) ⇒ Object

Do a lookup on a record of an attribute and map the value to the csv required version if appliccable. At the moment booleans are converted to zeros and ones



60
61
62
63
64
65
66
67
68
69
# File 'lib/dunlop/csv_builder.rb', line 60

def record_attribute(record, attribute_name)
  result = record.public_send(attribute_name)
  case result
  when TrueClass then 1
  when FalseClass then 0
  when String then result.strip.gsub(/\r\n?|\n/, " ")
  when DayTimeBase then result.number
  else result
  end
end

#serialize_row(record) ⇒ Object



34
35
36
# File 'lib/dunlop/csv_builder.rb', line 34

def serialize_row(record)
  [record.id]
end

#serialized_rowsObject



25
26
27
28
29
30
31
32
# File 'lib/dunlop/csv_builder.rb', line 25

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

#workflow_instances_in_batchObject

TODO: this is a separate include, not part of the core CsvBuilder object



53
54
55
# File 'lib/dunlop/csv_builder.rb', line 53

def workflow_instances_in_batch
  resource.workflow_instances.for_filtered_export
end