Skip to content

Console CSV exports

Petr Marek edited this page Oct 20, 2021 · 2 revisions

Add CSV exports to console index pages by using the csv: true argument on folio_console_controller_for. Example:

class Folio::Console::LeadsController < Folio::Console::BaseController
  folio_console_controller_for "Folio::Lead", csv: true

  ...
end

Adding it sets folio_console_controller_for_handle_csv controller class method accordingly and updates the default index action. It also adds a CSV button to the index header.

For the CSV export to work, two methods have to be defined on the corresponding model.

Class method to define the CSV columns:

def self.csv_attribute_names
  %i[id email phone note created_at name url aasm_state]
end

Method obtaining the data from individual models:

def csv_attributes(controller = nil)
  self.class.csv_attribute_names.map do |attr|
    case attr
    when :aasm_state
      aasm.human_state
    else
      send(attr)
    end
  end
end