Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Idea] Add ability to provide csv template #111

Open
sharshenov opened this issue Jun 30, 2016 · 5 comments
Open

[Idea] Add ability to provide csv template #111

sharshenov opened this issue Jun 30, 2016 · 5 comments

Comments

@sharshenov
Copy link
Contributor

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"
                                  }
@Fivell
Copy link
Member

Fivell commented Jul 1, 2016

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

@scarroll32
Copy link

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.

@Fivell
Copy link
Member

Fivell commented Dec 6, 2016

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

@workgena
Copy link
Contributor

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

@etozzato
Copy link

+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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants