- 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 calledexport_sheet
defined in theapp/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
- define a route for the resource export
# config/routes.rb
get 'admins/export', to: 'admins#export'
- 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
- 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
/>