active_admin_import
active_admin_import copied to clipboard
[Idea] Add ability to provide csv template
Just got an idea - put a link to template csv file in the upload form. Default attribute set can be taken from model (resource.resource_class.attribute_names - ["created_at", "updated_at"]
).
And provide options to configure attribute set:
active_admin_import csv_template: {
attributes: ["some", "custom", "attributes"],
filename: "products_template.csv"
}
@sharshenov , what are you trying to achieve with csv_template ? How it can help?
I think it is a good idea: a template of column names could be added which would allow
- Verification that the right number of columns are in the file
- Names for columns which can be used in later processing
A much larger feature would be the ability to specify types (or map them to actual table columns) and reject the row or whole file on a mismatch.
@seanfcarroll , any ideas with dsl example for this new feature ?
@sharshenov , latest release has one of features you've requested.
Skip CSV-columns. Basically it allows you to specify what columns to take from CSV-file.
Take a look, and share your thoughts about this implementation. Is it enough for your needs?
+1
I monkey-patched my temporary* solution as:
collection_action :template, method: :get do
respond_to do |format|
csv_template = CSV.generate do |csv|
csv << resource_class.column_names - %w[created_at updated_at].map do |attr|
resource_class.human_attribute_name(attr, default: attr.to_s.titleize)
end
end
format.csv do
send_data csv_template,
filename: "#{resource_class}_template.csv",
type: 'application/csv',
disposition: 'inline'
end
end
end
action_item :import, only: :index, if: options[:if] do
if authorized?(ActiveAdminImport::Auth::IMPORT, active_admin_config.resource_class)
span do
link_to(
I18n.t('active_admin_import.import_model', plural_model: options[:plural_resource_label]),
action: :import
)
end
span do
link_to(
I18n.t('active_admin_import.template'),
action: :template,
format: :csv
)
end
end
end
- I will look at how to whitelist only a subset of attributes for import and adapt the template accordingly