Giter Club home page Giter Club logo

active_admin_import's Introduction

ActiveAdminImport

Build Status   Coverage Status Code Climate   Gem Version    License

The most fastest and efficient CSV import for Active Admin with support of validations, bulk inserts and encodings handling.

For more about ActiveAdminImport installation and usage, check Documentation website and Wiki pages for some specific cases and caveats.

Installation

Add this line to your application's Gemfile:

gem "active_admin_import"

or

gem "active_admin_import" , github: "activeadmin-plugins/active_admin_import"

And then execute:

$ bundle

Features

  • Replacements/Updates support
  • Encoding handling
  • CSV options
  • Ability to describe/change CSV headers
  • Bulk import (activerecord-import)
  • Callbacks
  • Zip files
  • and more...

Basic usage

ActiveAdmin.register Post
  active_admin_import options
end

Options

Tool Description
:back resource action to redirect after processing
:csv_options hash with column separator, row separator, etc
:validate bool (true by default), perform validations or not
:batch_transaction bool (false by default), if transaction is used when batch importing and works when :validate is set to true
:batch_size integer value of max record count inserted by 1 query/transaction
:before_import proc for before import action, hook called with importer object
:after_import proc for after import action, hook called with importer object
:before_batch_import proc for before each batch action, called with importer object
:after_batch_import proc for after each batch action, called with importer object
:on_duplicate_key_update an Array or Hash, tells activerecord-import to use MySQL's ON DUPLICATE KEY UPDATE or Postgres 9.5+/SQLite 3.24.0+ ON CONFLICT DO UPDATE ability
:on_duplicate_key_ignore bool, tells activerecord-import to use MySQL's INSERT IGNORE or Postgres 9.5+ ON CONFLICT DO NOTHING or SQLite's INSERT OR IGNORE ability
:ignore bool, alias for on_duplicate_key_ignore
:timestamps bool, tells activerecord-import to not add timestamps (if false) even if record timestamps is disabled in ActiveRecord::Base
:template custom template rendering
:template_object object passing to view
:resource_class resource class name
:resource_label resource label value
:plural_resource_label pluralized resource label value (default config.plural_resource_label)
:error_limit Limit the number of errors reported (default 5, set to nil for all)
:headers_rewrites hash with key (csv header) - value (db column name) rows mapping
:if Controls whether the 'Import' button is displayed. It supports a proc to be evaluated into a boolean value within the activeadmin render context.

Wiki

Check various examples

Dependencies

Tool Description
rchardet Character encoding auto-detection in Ruby. As smart as your browser. Open source.
activerecord-import Powerful library for bulk inserting data using ActiveRecord.

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

active_admin_import's People

Contributors

aurelien-iapp avatar bitdeli-chef avatar chechaoyang avatar clinejj avatar dmitry-sinina avatar doredesign avatar enomotodev avatar fivell avatar gigorok avatar gitusp avatar herenow avatar ifyouseewendy avatar jkowens avatar kennym avatar linqueta avatar morris-frank avatar naokirin avatar owyongsk avatar pnghai avatar pskl avatar raeno avatar ricardojudo avatar senid231 avatar sharshenov avatar sinansh avatar stadniklksndr avatar workgena avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

active_admin_import's Issues

Error: undefined method `*=' for #<*>

Hey, I'm having an issues with this plugin and just cannot manage to fix it, even after reading all the past issues.

Here is my import.csv:
"testname",1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0

My blend.rb (active admin):

ActiveAdmin.register Blend do

  #menu :if => proc{ current_admin.admin_type == 1}

  permit_params :Descriptor, :N, :Pav, :Pt, :K, :sS, :eS, :tS, :Ca, :Mg, :B, :Co, :Cu, :Fe, :Mn, :Mo, :Se, :Zn, :UREA, :SOA, :DAP, :MAP, :ZincStar, :MAPStar, :TSP, :SSP, :MOP, :SOP, :BS90, :HiMag, :B10, :B15, :Co21, :Cu25, :CuZn, :Fe25, :Mg55, :Mn32, :Mo13, :Mo39, :Se1, :Zn35, :AMS, :AMT, :UAT, :wSOA, :GUANO, :Agrotain, :Basis, :Flufol, :Orun, :Sweep

  #actions :all, :except => [:destroy]
  active_admin_import validate: true,
    csv_options: {},
    template: "admin/import",
    before_import: ->(importer){ Blend.delete_all },
    timestamps: true,
  template_object: ActiveAdminImport::Model.new(
    force_encoding: :auto,
    :csv_headers => ["Descriptor", "N", "Pav", "Pt", "K", "sS", "eS", "tS", "Ca", "Mg", "B", "Co", "Cu", "Fe", "Mn", "Mo", "Se", "Zn", "UREA", "SOA", "DAP", "MAP", "ZincStar", "MAPStar", "TSP", "SSP", "MOP", "SOP", "BS90", "HiMag", "B10", "B15", "Co21", "Cu25", "CuZn", "Fe25", "Mg55", "Mn32", "Mo13", "Mo39", "Se1", "Zn35", "AMS", "AMT", "UAT", "wSOA", "GUANO", "Agrotain", "Basis", "Flufol", "Orun", "Sweep"]
  )

and my schema.rb:

  create_table "blends", force: :cascade do |t|
    t.string   "Descriptor",               null: false
    t.decimal  "N",          default: 0.0
    t.decimal  "Pt",         default: 0.0
    t.decimal  "K",          default: 0.0
    t.decimal  "tS",         default: 0.0
    t.decimal  "Zn",         default: 0.0
    t.decimal  "Cu",         default: 0.0
    t.datetime "created_at",               null: false
    t.datetime "updated_at",               null: false
    t.decimal  "Pav",        default: 0.0
    t.decimal  "sS",         default: 0.0
    t.decimal  "eS",         default: 0.0
    t.decimal  "Ca",         default: 0.0
    t.decimal  "Mg",         default: 0.0
    t.decimal  "B",          default: 0.0
    t.decimal  "Co",         default: 0.0
    t.decimal  "Fe",         default: 0.0
    t.decimal  "Mn",         default: 0.0
    t.decimal  "Mo",         default: 0.0
    t.decimal  "Se",         default: 0.0
    t.decimal  "UREA",       default: 0.0
    t.decimal  "SOA",        default: 0.0
    t.decimal  "DAP",        default: 0.0
    t.decimal  "MAP",        default: 0.0
    t.decimal  "ZincStar",   default: 0.0
    t.decimal  "MAPStar",    default: 0.0
    t.decimal  "TSP",        default: 0.0
    t.decimal  "SSP",        default: 0.0
    t.decimal  "MOP",        default: 0.0
    t.decimal  "SOP",        default: 0.0
    t.decimal  "BS90",       default: 0.0
    t.decimal  "HiMag",      default: 0.0
    t.decimal  "B10",        default: 0.0
    t.decimal  "B15",        default: 0.0
    t.decimal  "Co21",       default: 0.0
    t.decimal  "Cu25",       default: 0.0
    t.decimal  "CuZn",       default: 0.0
    t.decimal  "Fe25",       default: 0.0
    t.decimal  "Mg55",       default: 0.0
    t.decimal  "Mn32",       default: 0.0
    t.decimal  "Mo13",       default: 0.0
    t.decimal  "Mo39",       default: 0.0
    t.decimal  "Se1",        default: 0.0
    t.decimal  "Zn35",       default: 0.0
    t.decimal  "AMS",        default: 0.0
    t.decimal  "AMT",        default: 0.0
    t.decimal  "UAT",        default: 0.0
    t.decimal  "wSOA",       default: 0.0
    t.decimal  "GUANO",      default: 0.0
    t.decimal  "Agrotain",   default: 0.0
    t.decimal  "Basis",      default: 0.0
    t.decimal  "Flufol",     default: 0.0
    t.decimal  "Orun",       default: 0.0
    t.decimal  "Sweep",      default: 0.0
  end

  add_index "blends", ["Descriptor"], name: "index_blends_on_Descriptor", unique: true, using: :btree

I just cannot get it to work. I have tried combinations of quotes and no quotes in the .csv, changed the lines around within blend.rb etc etc. I keep getting this error! Adding a new blend through 'New Blend' in active admin works fine.

Does anybody have any ideas?

False flash message

Using (with bundle show) :
active_admin_import-8d78ff9388ab (latest)
activeadmin-28e8a8f36640 (latest)
rails-4.2.0

I have a activerecord model name Dog which belongs to another model Shelter. When I try to import dogs to shelter, I get -

  1. "Failed to import 82 dogs" - Which makes sense as my csv had 82 dog records.
  2. "Successfully imported 342 dogs" - Which makes no sense at all as Shelter only has 82 dogs which I tried to export and import. 342 sounds like a totally random number coming out from no where. Just to confirm the issue I tried the import again but "Successfully imported 342 dogs" appears again.

import___barkloudly_dashboard

Also I am having trouble debugging the issue, since I was expecting a simple export - import of records to work smoothly.

Postgres text array import

I've been trying with no success to upload an attribute that is being stored in Postgres as a text type, with array true.

The schema line for that attribute: t.text "verification_codes", default: [], array: true

When creating the model via seeds or the console, the following creates the attribute as expected:
:verification_codes => ["social_full", "birth_date"],

I've tried many variations in the CSV, with the array brackets, without them, one double quote enclosed string. There is no error and the object saves with the proper data with the exception of this attribute, which is input in the SQL as '{}'. I didn't see any examples listing this and don't see anything in the code for this specific case.

Please let me know if I'm going about this wrong or if there's a clear problem from the code above. Thanks!

Replace with id on import for a column that has multiple entries separated by comma

I'm trying to do a replace with id on import for a column that has multiple entries (hence multiple ids).

 Course.where(local_course_id: importer.values_at('local_course_id')).delete_all
                 subject_names = importer.values_at(:subjectarea_id)
                 # replacing subject area name with subject area id
                 subject_names_split = subject_names.map {|subject_names|   {subject_names => subject_names.split(', ')}.flatten } 
                   subjects   = SubjectArea.where(name: subject_names_split).pluck(:id)
                   subjects_join = subjects.join(',')
                # options = Hash[*subjects_join.flatten] # #{"Jane" => 2, "John" => 1}
                 importer.batch_replace(:subjectarea_id, subjects_join)

csv column looks like this:

subjectarea_id
Business, Communications
The Arts, History

When the data goes into the database in need that column to have multiple ids.
subjectarea_id
3,6
2,1

I can split them for the lookup and then rejoin them, I just can't seem to get them into the db.

Thanks,

Amelia

Add values to befor_import

if I want to insert to values on the fly while importing rows.
i.e I want to add to more columns but without the need to specify them on the csv file, one of the values is uid column which is the same user's email, so while import I want to create for every row, another uid that get's the email value from csv's email header, and provider, which is just a string email.

[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"
                                  }

You can import only valid csv file

You can import only valid csv file, I dont get this error, is there a way that i could see the logs?

I have a csv file which I also have an importer table

this is my csv example but it doesnt get imported.

"name";"btype";"reg_no";"body_no";"plate_no";"capacity"
"BUS1";"1";"REGNO-xxx-xxxx";"BODY NO";"PLATE-XXX";"15"
"BUS2";"0";"REGNO-xxx-xxxx";"BODY NO";"LPATE-xxx";"15"
"BUS3";"1";"REGNO-xxx-xxxx";"BODY NO";"PLATE-XXX";"15"

and this is my import code

ActiveAdmin.register Bus do
  active_admin_import validate: true,
                      csv_options: {col_sep: ";" },
                      resource_class: BusImport ,  # we import data into another resource
                      # before_import: proc{ .delete_all },
                      after_import: proc{
                        Bus.transaction do
                          Bus.connection.execute("INSERT INTO buses (name,btype,regno,body_no,plate_no,capacity) (SELECT name,btype,regno,body_no,plate_no,capacity FROM bus_imports)")
                        end
                      },
                      back: proc { config.namespace.resource_for(Bus).route_collection_path } # redirect to bus index

Is there something im missing?

Many to many relationships

Is there any way to import a has_many or has_and_belongs_to_many relationship? A collection of ids? I can't figure out how to build the csv also.

Thanks for this great gem.

Semantic Errors

Is there a way to display the errors of each model instance that fails to import?

Import with relations

Is there away to import but by fetching id from another model first ?
for example, I want to insert into users model, the user have licence_id that point to a license into License model.
the csv will have like username, license.
so before import I want to fetch the license id from license model first, then insert it to the user model when importing..
I guess you got what I mean, Importing with relation.

Authorization error

i am doing authorization with strongbolt Here.
the way of authorization is working for models not requests, so I have to givestrong bolt, the Model that will have some authorization to get it work, Here
and if there's a controller that I shouldn't authorize in like registration and sessions, I should put it Here
so, with that said, Strong bolt give me a can? method, but only with the usual ReSTful actions create, find, update, delete
I couldn't get the gem work in my case, so any hints ?

Passing params to import

How about passing params to the import? So the web interface could hook up with the import.

How about something like this:

# importer.rb
   # add attr_reader
    attr_reader :params

   # pass params:
    def initialize(resource, model, options, params)
      @params = params
# dls.rb 
    # pass params:
     @importer = Importer.new(options[:resource_class], @active_admin_import_model, options, params)

Just an idea.

Active Record 4

Bundler could not find compatible versions for gem "activerecord":
In Gemfile:
active_admin_import (>= 0) ruby depends on
activerecord (~> 3.0) ruby

paperclip (>= 0) ruby depends on
  activerecord (4.0.0)

Error: undefined method `underscore' for nil:NilClass

I'm having issues getting files to import. What format should the csv be in for the default import to work?

using: active_admin_import in resource file

I keep getting this error on import "Error: undefined method `underscore' for nil:NilClass." I tried another file but then I get "You can't import empty file."

File one:
1,Credit,325,,,,,,Approved,CPD,,,

File two:
"school_id","trans_type","points","payment","attendee","event_start","event_end","course","approved","points_type","trans_date","approver_comments"
"1","Credit","250","","","","","","Approved","CPD","",

I tried adding in the quotes to see if that would make a difference.

Here is the table schema:

create_table "points_transactions", force: :cascade do |t|
t.integer "school_id"
t.string "trans_type"
t.integer "points"
t.integer "payment"
t.string "attendee"
t.date "event_start"
t.date "event_end"
t.string "course"
t.string "approved"
t.string "points_type"
t.string "trans_date"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "approver_comments"
end

Any help you can give would be great!

No import controls

Hi!

I just added this gem and enabled it for one of my ActiveAdmin objects like this:

ActiveAdmin.register Payment do
  active_admin_import
end

But now I can't find any controls that allow me to upload CSV. I expected it to appear on an index page for this object, is it correct?

Splitting an import field into two columns

Hello Again,

This should be my last question. :)

I'm trying to import one column from the csv and split it into two in the database. For example, import code (101-VA) insert into code (101) insert into section (VA)

codes = importer.values_at(:code)
options_codes = codes.map {|codes|   {codes => codes.split('-').first}.flatten } 
options_sections = codes.map {|codes| {codes => codes.split('-').last}.flatten } 
options_codes = Hash[*options_codes.flatten]
options_sections = Hash[*options_sections.flatten]
importer.batch_replace(:'code', options_codes)
importer.csv_lines.map! { |row| row << options_sections}  #not entering one per row
importer.headers.merge!({:'section' => :section})

This is what I've been playing with, I have the code portion going into the code field, however I can't seem to get the section hash to enter one section per row. Instead, the whole hash is going in a row. Here is the section field from one of my records.

{"code"=>"code", "111-VA"=>"VA", "112-VA"=>"VA", "114-VA"=>"VA", "114-VB"=>"VB", "115-VA"=>"VA", "115-VB"=>"VB", "121-VA"=>"VA", "122-VB"=>"VB", "141-VA"=>"VA", "142-VA"=>"VA", "225-VB"=>"VB", "232-VB"=>"VB", "238-VA"=>"VA", "251-VA"=>"VA", "251-VB"=>"VB", "265-VB"=>"VB", "151-VA"=>"VA", "215-VB"=>"VB", "250-VA"=>"VA", "111-VB"=>"VB", "112-VB"=>"VB", "228-VB"=>"VB", "236-VA"=>"VA", "110-VB"=>"VB", "101-VA"=>"VA", "102-VB"=>"VB", "100-VA"=>"VA", "121-VB"=>"VB", "130-VA"=>"VA", "130-VB"=>"VB", "158-VB"=>"VB", "172-VB"=>"VB", "113-VB"=>"VB", "095-VB"=>"VB", "123-VB"=>"VB", "124-VA"=>"VA", "124-VB"=>"VB", "127-VA"=>"VA", "127-VB"=>"VB", "141-VB"=>"VB", "141-VS"=>"VS", "245-VB"=>"VB", "109-VA"=>"VA", "116-VA"=>"VA", "117-VA"=>"VA", "117-VB"=>"VB", "117-VS"=>"VS", "118-VA"=>"VA", "118-VB"=>"VB", "125-VA"=>"VA", "125-VB"=>"VB", "133-VS"=>"VS", "151-VB"=>"VB", "161-VA"=>"VA", "161-VB"=>"VB", "162-VA"=>"VA", "162-VB"=>"VB", "166-VS"=>"VS", "258-VS"=>"VS", "121-VS"=>"VS", "152-VA"=>"VA", "152-VB"=>"VB", "155-VA"=>"VA", "163-VB"=>"VB", "167-VB"=>"VB", "170-VA"=>"VA", "185-VA"=>"VA", "186-VB"=>"VB", "234-VA"=>"VA", "235-VS"=>"VS", "236-VS"=>"VS", "241-VA"=>"VA"}

Any thoughts on how to write one line at a time?

Using tab as delimiter results in "Error: undefined method"

When I set csv_options: { col_sep: "\t" } and use a file with tabs, I get an error such as "Error: undefined method `location_name_=' for #TmpItemImport:0x007ff315d29be0 Did you mean? location=", where location and name are individual columns. Any idea what the issue might be?

CSV created with Excel on Mac Silently Fails

There are no import errors, and it just appears to do nothing.

After a bunch of debug time reviewing the internals of the gem, I found that the Importer.import method cannot handle the format of the "default" CSV created by excel on mac. The failure occurs when the headers are properly discovered, but then the File.open block cannot find any lines. If you print out what it finds, it looks like the \r delimiter that Excel on mac CSV is not treated as a new line (I'm guessing it expects \r\n or similar), so the data all appears as a single line and thus it imports 0 rows without an error message.

The fix was to save in "CSV format for windows" inside excel, and everything works as it should.

I was able to see this on Rails 4.2, Ruby 2.1.3

Import link throws routing error for nested resource

I have confirmed that your gem works great on a non-nested resource. The resource I want to use your gem for, however, is nested via

# app/admin/item.rb
belongs_to :store

rake routes returns a proper URL:

            import_admin_store_items GET        /admin/stores/:store_id/items/import(.:format)             admin/items#import

Yet I get an error on these URLs:

No route matches {:action=>"new", :controller=>"admin/items", :store_id=>nil}

CSV export with localised column headers and inhibiting from import

When exporting data via CSV from the active_admin admin and your environment locale is set to something other than English, like this in an initializer:

I18n.default_locale = :es

The column headers of the exported CSV are in the localised language. When you then try to import that CSV with active_admin_import you'll get an error because it cannot match the headers with the column names which are in English.

I am on master (95fdfe1)

Can't import Devise users

I can't seem to import users that are created through Devise. I get an error
Missing column for value at index 3
which makes sense since "password" isn't an actual field its "encrypted_password", but I am providing a default password for these users that I am importing which they are required to change after the import. Is there anyway around this?

Associations

How can I go about importing associations? I've tried adding it to the CSV like this

 first_name,last_name,email,encrypted_password,industry
Joe,Blogs,[email protected],12345678,gardening 

but i just get an "Missing column for value at index 4" error. I guess because there isn't a Industry column on the user model.

Hook after_create doesn't run after importing

Hi @Fivell!

I came across a problem with running code for my imported objects.

I have a quite simple import setup:

ActiveAdmin.register Payment do
    ...
    active_admin_import validate: true,
        timestamps: true,
        csv_options: { col_sep: ";", row_sep: nil, quote_char: nil }
    ...
end

I tried to use after_import hook but didn't figure it out how to obtain list of created records.
Is it possible to either enable after_create callback or run my code explicitly for newly created records?

Import only specific columns

I have a CSV file that the client wishes to upload with dozens of columns but I only need two of them. Is there a way to isolate the columns I want to import?

What I currently have now is this

  active_admin_import validate: true,
                                     template_object: ActiveAdminImport::Model.new(
                                       csv_headers: ['SERIAL', 'NAME']
                                     ),
                                     headers_rewrites: { :'SERIAL' => :serial_number, :'NAME' => :name }

... which does not seem to work and I get this error in the ActiveAdmin UI

Failed to import 2733 products: ; ; ; ;

Duplicate entry

How can i avoid the duplicate entry error when import a file?

Doesn't see file

I have this:

UPDATE: I am on Rails 5.0.2 with AA 1.0.0.pre5

active_admin_import validate: true, batch_transaction: true, batch_size: 100, before_import: :before_import_check

And when I attach a file and doimport, I get this:

https://screencast.com/t/t37k88jGy

This is using 3.0.0.pre as recommended in the read me.

The backend received this:

Started POST "/admin/jobs/do_import" for ::1 at 2017-04-20 10:59:32 -0500
ActiveRecord::SchemaMigration Load (0.8ms) SELECT "schema_migrations".* FROM "schema_migrations"
Processing by Admin::JobsController#do_import as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"4xnvXwT2FgUiPF83hG35SoUxrW126Xm2pbLDycHDvSNVlpJYz46Ac0/B3ahyaIt6Cc9mmlX9iWwkgvphYLF4/g==", "active_admin_import_model"=>{"file"=>#<ActionDispatch::Http::UploadedFile:0x007f915ec01ab0 @tempfile=#Tempfile:/var/folders/3j/2wcv7bl925l8s496d7st54540000gn/T/RackMultipart20170420-37614-1jjeds5.csv, @original_filename="healthCareJobs.csv", @content_type="text/csv", @headers="Content-Disposition: form-data; name="active_admin_import_model[file]"; filename="healthCareJobs.csv"\r\nContent-Type: text/csv\r\n">}, "commit"=>"Import"}
Geokit is using the domain: localhost
AdminUser Load (1.6ms) SELECT "admin_users".* FROM "admin_users" WHERE "admin_users"."id" = $1 ORDER BY "admin_users"."id" ASC LIMIT $2 [["id", 1], ["LIMIT", 1]]
Rendering /Users/arkavon/.rvm/gems/ruby-2.3.0@meddery/gems/active_admin_import-2.1.2/app/views/admin/import.html.erb within layouts/active_admin
DEPRECATION WARNING: input_class is deprecated and will be removed from Formtastic 4.0 (configure Formtastic::FormBuilder.input_class_finder instead (upgrade guide on wiki: http://bit.ly/1F9QtKc )) (called from input at /Users/arkavon/.rvm/gems/ruby-2.3.0@meddery/gems/formtastic-3.1.5/lib/formtastic/helpers/input_helper.rb:240)
DEPRECATION WARNING: action_class is deprecated and will be removed from Formtastic 4.0 (configure Formtastic::FormBuilder.action_class_finder instead (upgrade guide on wiki: http://bit.ly/1F9QtKc )) (called from action at /Users/arkavon/.rvm/gems/ruby-2.3.0@meddery/gems/formtastic-3.1.5/lib/formtastic/helpers/action_helper.rb:85)
Rendered /Users/arkavon/.rvm/gems/ruby-2.3.0@meddery/gems/active_admin_import-2.1.2/app/views/admin/import.html.erb within layouts/active_admin (29.2ms)
Completed 200 OK in 614ms (Views: 545.6ms | ActiveRecord: 13.6ms)

Not sure where to go here.

Howto add default additional data to csv parsed from file?

Howto add default additional data to csv parsed from file?
I can add additional headers in :before_batch_import
but how i can fill column with dynamic data
for example:
I got csv file with 2 headers:
name, address
then in :before_batch_import i add header
client_id
so we got
name, address, client_id
and i want to fill additional column with data from
params[:client_id]

Is there a simple solution?

`undefined method `underscore' for nil:NilClass`

I keep getting an error stating:

undefined method `name' for nil:NilClass

This is my current code:

active_admin_import validate: false,
    on_duplicate_key_update: [:description, :retail_price, :wholesale_price, :notes],
    timestamps: false,
    template_object: ActiveAdminImport::Model.new(
      force_encoding: "UTF-8",
      csv_headers: ["id", "description", "retail_price", "wholesale_price", "notes"]
    )

I've also tried:

active_admin_import validate: false,
    csv_options: { col_sep: ";", quote_char: '"'  },
    on_duplicate_key_update: [:description, :retail_price, :wholesale_price, :notes],
    timestamps: false,
    template_object: ActiveAdminImport::Model.new(
      force_encoding: "UTF-8",
      csv_headers: ["id", "description", "retail_price", "wholesale_price", "notes"]
    )

and I get this error:

Unquoted fields do not allow \r or \n (line 4).

Any ideas what may be causing this? I'm attempting to do a mass update from a CSV rather than create new records

Change date format on import

Hi,

The dates I'm importing are in mm/dd/yyyy format and I can't seem to save them unless they are in yyyy/mm/dd format. I'm looking for a simple why to read in the date and replace it with the new date.

Right now I can read the date in and change the format, but I'm struggling with getting the new date into the database.

This is what I've been playing with:

                start_dates = importer.values_at(:start_date)      
                  start_date = Hash[*start_dates.flatten]
                  date = "#{start_date['start_date']}"
                  date = Course.strptime(date).to_s   # this changes the format to yyyy-mm-dd succesfully

                  importer.csv_lines.map! { |row| row << date}
                  importer.headers.merge!({:'start_date' => :start_date})

sqlite3 vs postgress

Hi there,

I have a development environment with sqlite3 and a production (Heroku) environment with postgress. The gem works fine for the local version, but tells me You can import only valid csv file when I try to import the same file into the production environment.

Here is part of my heroku logs:

2015-11-24T11:55:26.504300+00:00 app[web.1]: Processing by Admin::ProductsController#do_import as HTML
2015-11-24T11:55:26.504458+00:00 app[web.1]:   Parameters: {"utf8"=>"✓", "authenticity_token"=>"Nnqs3Jb/LfqrIOUvrPUT9Y4lRzh9YpaSGyFct2d19WNMYz/RzAsi59G99/LvyXXLuolAB5jgCiKDTzYvzTGa8Q==", "active_admin_import_model"=>{"file"=>#<ActionDispatch::Http::UploadedFile:0x007f6e05b1c040 @tempfile=#<Tempfile:/tmp/RackMultipart20151124-3-a4lxja.csv>, @original_filename="algemene-test-products.csv", @content_type="text/x-comma-separated-values", @headers="Content-Disposition: form-data; name=\"active_admin_import_model[file]\"; filename=\"algemene-test-products.csv\"\r\nContent-Type: text/x-comma-separated-values\r\n">}, "commit"=>"Import"}
2015-11-24T11:55:26.512911+00:00 app[web.1]:   AdminUser Load (1.8ms)  SELECT  "admin_users".* FROM "admin_users" WHERE "admin_users"."id" = $1  ORDER BY "admin_users"."id" ASC LIMIT 1  [["id", 2]]
2015-11-24T11:55:26.528978+00:00 app[web.1]: DEPRECATION WARNING: action_class is deprecated and will be removed from Formtastic 4.0 (configure Formtastic::FormBuilder.action_class_finder instead (upgrade guide on wiki: http://bit.ly/1F9QtKc )). (called from realtime at /app/vendor/ruby-2.0.0/lib/ruby/2.0.0/benchmark.rb:296)

Any ideas on what's going on?

Cheers,
Sebastian

Always no file to import in Rails 5 beta 3

Hi guys,

Not sure about your plans on supporting Rails 5 but you need to know, this code:

 @active_admin_import_model.assign_attributes(params[params_key].try(:deep_symbolize_keys) || {})

will not work as expected because params[params_key].try(:deep_symbolize_keys) will always return nil.

I guess it's because of

DEPRECATION WARNING: Method deep_symbolize_keys is deprecated and will be removed in Rails 5.1, as ActionController::Parameters no longer inherits from hash. Using this deprecated behavior exposes potential security problems. If you continue to use this method you may be creating a security vulnerability in your app that can be exploited. Instead, consider using one of these documented methods which are not deprecated: http://api.rubyonrails.org/v5.0.0.beta3/classes/ActionController/Parameters.html.

so simple transforming params[params_key] into hash (to_unsafe_h, for example), can do the trick.

Is there a way to return error messages?

Apologies for posting a question here.. couldn't find a google group.
I'm trying to add some "user-friendly" features to the bulk import process and I was wondering if we can return helpful messages back to the user..
e.g. CSV line that the import failed, Validation Messages that stoped the process, etc.

No errors, but no rows created

This gem looks great, but when I tried to use it, no data got imported.

I tried everything I could think of - I used the before/after import procs to confirm that it was running. Tried multiple options, turning validation off, setting permit_params. I used the before_import proc to confirm that the file was getting uploaded and the file was readable on the server.

Not sure - maybe this just doesn't work with the latest master branch version of ActiveAdmin?

Question on how to use.

I kind of had a hard time implementing this on my end. I wish there was some example online.
What Im trying to do is , customizing what column to insert and before inserting I can customized some column values.

Is this possible?

Belongs to

Hello,
I have two active record based models, user and application where each user belongs to an application (every user must be associated with one application). I have configured the active admin artifacts on the following way.

ActiveAdmin.register User do
  belongs_to :application
  active_admin_import    before_batch_import: proc { |import|

  }

I can navigate through the applications and when I show the users, the . Besides, when I create a new single user this is associated automatically with the application do to the belongs_to sentence (so far, so good).
However when I try to import several users through active_admin_import, I receive a message telling me that I can not insert a user with a blank application_id. In other words application is not set on users.
The question is.... Is there any configuration in order to honor the belongs_to association in the same way active_admin does?.
I was thinking a walk around were I pass the application_id from the request parameters to before_batch_import callaback, but I was not able to do that either.

Send email based on row imported

Hi,

I'm trying to import several rows in a csv and send an email regarding any of them that have the "status" of "pending". On manual entry of a record, I have the email sending successfully. I need to figure out how to send the email based on the contents of a row being imported.

sample file:

event_id event_date attendee amount trans_type points_type institution_id status
Course 1: Assessment for Improvement of Teaching and Learning 9/14/15 225 Debit CPD Adirondack Community College Pending

Here is the method I can call to pass the data to an email

PointsMailer.pending_email(row params).deliver_now

Records not being created

I am not sure if this is a bug or ID10T error on my part but I can upload the file with out problem, but according to the logging it never inserts records into the database. I am using slightly modified sample code.

ActiveAdmin.register Voter do
 active_admin_import :validate => true,
                  :col_sep => ',',
                  :before_import => proc{|importer|  Voter.delete_all},
                  :back => :index,
                  :batch_size => 1000

end




Started POST "/admin/voters/do_import" for 127.0.0.1 at 2013-05-25 14:48:46 -0400
Processing by Admin::VotersController#do_import as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"Uhsrm4F/k1zG1qpiczCEX7HxMAauPgWB/n9z8I6FPi4=",     "active_admin_import_model"=>{"file"=>#<ActionDispatch::Http::UploadedFile:0x007ff2d5fda690        @original_filename="MyExport.csv", @content_type="text/csv", @headers="Content-Disposition: form-data; name=\"active_admin_import_model[file]\"; filename=\"MyExport.csv\"\r\nContent-Type: text/csv\r\n", @tempfile=#   <Tempfile:/var/folders/tz/0w8zcxxj6ys8cvdns7ztcs2r0000gn/T/RackMultipart20130525-38523-1mghnl4>>}, "commit"=>"Import"}
 AdminUser Load (0.3ms)  SELECT `admin_users`.* FROM `admin_users` WHERE `admin_users`.`id` = 1 LIMIT 1
 SQL (0.2ms)  DELETE FROM `voters`

Redirected to http://0.0.0.0:3000/admin/voters/import
Completed 302 Found in 389ms (ActiveRecord: 5.1ms)

Is it possible to add nested resource id on the fly?

I have a nested resource, like /libraries/15/commits and want to import commits from css.

Is it possible to get the parent resource id and add it on the fly?

I could see the parent id in the Better Errors' dump as a Request parameters > library_id but can't figure how to catch it.

Thank you in advance.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.