Skip to content

Instantly share code, notes, and snippets.

@lynndylanhurley
Last active September 7, 2017 23:39
Show Gist options
  • Save lynndylanhurley/dfdc64513c62e353850f8e1fa2ea2f78 to your computer and use it in GitHub Desktop.
Save lynndylanhurley/dfdc64513c62e353850f8e1fa2ea2f78 to your computer and use it in GitHub Desktop.
Instructions for setting up an export

steps to create export

  1. on the model, define a method called export_data. this should return an array of 2D arrays (aka a 3D array) of tables that contain all of the export data. there is a helper method called export_sheet defined in the app/models/application_record.rb that will help with formatting the data.

example:

# app/models/admin.rb

def self.export_data(_params)
  data = [
    # the first row should contain the column labels
    ['Name', 'Email', 'First Name', 'Last Name'],

    # subsequent rows should contain the resource data
    *pluck('name, email, first_name, last_name')
  ]

  # return a hash with the following keys:
  # name: The name of the file
  # sheets: an array of tables. using the `export_sheet` method, the first argument
  #         will be the table name, and the second argument will be the table data
  { name: 'Admins',
    sheets: [export_sheet('Admins', data)] }
end
  1. define a route for the resource export
# config/routes.rb
get 'admins/export', to: 'admins#export'
  1. create the job for the export
# make sure to name the job after the class

class ExportAdminJob < ApplicationJob
  queue_as :default

  def perform(*args)
    create_and_send_link(*args)
  end
end
  1. on the client, use the ExportButton component to begin the export
// import
import { ExportButton } from '~/components';

// render
<ExportButton ns="admin-export" entity="admins" endpoint="/admins" />

or you can just set enableExport on an existing SearchForm

<SearchForm
  /// ... ns, type, etc.
  enableExport
/>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment