Module: ActiveAdminImport::DSL
- Defined in:
- lib/active_admin_import/dsl.rb
Instance Method Summary collapse
-
#active_admin_import(options = {}) ⇒ Object
Declares import functionality.
Instance Method Details
#active_admin_import(options = {}) ⇒ Object
Declares import functionality
Options
backresource action to redirect after processing
col_sepcolumn separator used for CSV parsing
validatetrue|false, means perfoem validations or not
batch_sizeinteger value of max record count inserted by 1 query/transaction
before_importproc for before import action, hook called with importer object
after_importproc for after import action, hook called with importer object
before_batch_importproc for before each batch action, called with importer object
after_batch_importproc for after each batch action, called with importer object
on_duplicate_key_updatean Array or Hash, tells activerecord-import to use MySQL's ON DUPLICATE KEY UPDATE ability.
timestampstrue|false, tells activerecord-import to not add timestamps (if false) even if record timestamps is disabled in ActiveRecord::Base
ignoretrue|false, tells activerecord-import toto use MySQL's INSERT IGNORE ability
fetch_extra_options_from_paramsparams values available in callbacks in importer.extra_options hash
templatecustom template rendering
localslocal variables for template
resource_classresource class name
resource_labelresource label value
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/active_admin_import/dsl.rb', line 25 def active_admin_import = {} = { :back => :import, :col_sep => ',', :template => "admin/import", :template_object => ActiveAdminImport::Model.new, :fetch_extra_options_from_params => [], :resource_class => nil, :resource_label => nil, :headers_rewrites => {} } = .merge() params_key = ActiveModel::Naming.param_key([:template_object]) collection_action :import, :method => :get do @active_admin_import_model = [:template_object] render :template => [:template] end action_item :only => :index do link_to(I18n.t('active_admin_import.import_model', :model => ([:resource_label] || active_admin_config.resource_name)), :action => 'import') end collection_action :do_import, :method => :post do if params[params_key].blank? flash[:alert] = I18n.t('active_admin_import.no_file_error') return redirect_to :back end content_types_allow = [ 'text/csv', 'text/x-csv', 'text/comma-separated-values', 'application/csv', 'application/vnd.ms-excel', 'application/vnd.msexcel' ] unless params[params_key]['file'].try(:content_type) && params[params_key]['file'].content_type.in?(content_types_allow) flash[:alert] = I18n.t('active_admin_import.file_format_error') return redirect_to :back end importer = Importer.new(([:resource_class] || active_admin_config.resource_class), params[params_key][:file], , params[params_key].to_hash.slice(*[:fetch_extra_options_from_params]) ) result = importer.import flash[:notice] = I18n.t('active_admin_import.imported', :count=> result[:imported].to_i, :model => ([:resource_label] || active_admin_config.resource_label).downcase, :plural_model => ([:resource_label].present? ? [:resource_label].to_s.pluralize : active_admin_config.plural_resource_label).downcase ) if result[:imported].to_i > 0 flash[:error] = I18n.t('active_admin_import.failed', :count=> result[:failed].count, :model => ([:resource_label] || active_admin_config.resource_label).downcase, :plural_model => ([:resource_label].present? ? [:resource_label].to_s.pluralize : active_admin_config.plural_resource_label).downcase ) if result[:failed].count > 0 redirect_to :action => [:back] end end |