active_admin_import icon indicating copy to clipboard operation
active_admin_import copied to clipboard

[Idea] Add ability to provide csv template

Open sharshenov opened this issue 8 years ago • 5 comments

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 avatar Jun 30 '16 17:06 sharshenov

@sharshenov , what are you trying to achieve with csv_template ? How it can help?

Fivell avatar Jul 01 '16 09:07 Fivell

I think it is a good idea: a template of column names could be added which would allow

  1. Verification that the right number of columns are in the file
  2. 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.

scarroll32 avatar Dec 02 '16 12:12 scarroll32

@seanfcarroll , any ideas with dsl example for this new feature ?

Fivell avatar Dec 06 '16 14:12 Fivell

@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?

workgena avatar Apr 11 '18 14:04 workgena

+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

etozzato avatar Sep 13 '19 01:09 etozzato