Class: OnboardOnRails::CompletionsCsvExporter

Inherits:
Object
  • Object
show all
Defined in:
app/services/onboard_on_rails/completions_csv_exporter.rb

Constant Summary collapse

TIMESTAMP_FORMAT =
"%Y-%m-%d %H:%M:%S".freeze
BOM =
"\uFEFF".freeze
BASE_COLUMNS =
%i[
  user_id user_email status
  last_step_position last_step_title
  started_at completed_at
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(tour) ⇒ CompletionsCsvExporter

Returns a new instance of CompletionsCsvExporter.



13
14
15
# File 'app/services/onboard_on_rails/completions_csv_exporter.rb', line 13

def initialize(tour)
  @tour = tour
end

Instance Method Details

#filenameObject



17
18
19
# File 'app/services/onboard_on_rails/completions_csv_exporter.rb', line 17

def filename
  "tour-#{@tour.id}-completions-#{Date.current.strftime('%Y%m%d')}.csv"
end

#to_csvObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'app/services/onboard_on_rails/completions_csv_exporter.rb', line 21

def to_csv
  attribute_defs = OnboardOnRails.configuration.registered_attributes.values
  completions = @tour.completions.includes(:step).order(updated_at: :desc).to_a
  users = preload_users(completions)

  body = CSV.generate do |csv|
    csv << header_row(attribute_defs)
    completions.each do |completion|
      csv << data_row(completion, users[completion.user_id], attribute_defs)
    end
  end

  BOM + body
end