Module: ActiveAdminImport::DSL
- Defined in:
- lib/active_admin_import/dsl.rb
Overview
Declares import functionality
Options
back
-
resource action to redirect after processing
csv_options
-
hash to override default CSV options
batch_size
-
integer value of max record count inserted by 1 query/transaction
batch_transaction
-
bool (false by default), if transaction is used when batch importing
and works when :validate is set to true
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
validate
-
true|false, means perform validations or not
on_duplicate_key_update
-
an Array or Hash, tells activerecord-import
to use MySQL’s ON DUPLICATE KEY UPDATE ability.
timestamps
-
true|false, tells activerecord-import to not add timestamps (if false)
even if record timestamps is disabled in ActiveRecord::Base
ignore
-
true|false, tells activerecord-import to use MySQL’s INSERT IGNORE ability
template
-
custom template rendering
template_object
-
object passing to view
resource_class
-
resource class name, override to import to another table (default config.resource_class)
resource_label
-
resource label value (default config.resource_label)
plural_resource_label
-
pluralized resource label value (default config.plural_resource_label)
Constant Summary collapse
- DEFAULT_RESULT_PROC =
lambda do |result, | model_name = [:resource_label].downcase plural_model_name = [:plural_resource_label].downcase if result.empty? flash[:warning] = I18n.t('active_admin_import.file_empty_error') else if result.failed? flash[:error] = I18n.t( 'active_admin_import.failed', count: result.failed.count, model: model_name, plural_model: plural_model_name, message: result.(limit: [:error_limit])) return if [:batch_transaction] end if result.imported? flash[:notice] = I18n.t( 'active_admin_import.imported', count: result.imported_qty, model: model_name, plural_model: plural_model_name) end end end
Instance Method Summary collapse
-
#active_admin_import(options = {}, &block) ⇒ Object
rubocop:disable Metrics/AbcSize.
Instance Method Details
#active_admin_import(options = {}, &block) ⇒ Object
rubocop:disable Metrics/AbcSize
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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/active_admin_import/dsl.rb', line 53 def active_admin_import( = {}, &block) .assert_valid_keys(*Options::VALID_OPTIONS) = Options.(config, ) collection_action :import, method: :get do (ActiveAdminImport::Auth::IMPORT, active_admin_config.resource_class) @active_admin_import_model = if [:template_object].is_a?(Proc) [:template_object].call else [:template_object] end render template: [:template] end action_item :import, only: :index, if: [:if] do if (ActiveAdminImport::Auth::IMPORT, active_admin_config.resource_class) link_to( I18n.t('active_admin_import.import_model', plural_model: [:plural_resource_label]), action: :import ) end end collection_action :do_import, method: :post do (ActiveAdminImport::Auth::IMPORT, active_admin_config.resource_class) _params = params.respond_to?(:to_unsafe_h) ? params.to_unsafe_h : params params = ActiveSupport::HashWithIndifferentAccess.new _params @active_admin_import_model = if [:template_object].is_a?(Proc) [:template_object].call else [:template_object] end params_key = ActiveModel::Naming.param_key(@active_admin_import_model.class) @active_admin_import_model.assign_attributes(params[params_key].try(:deep_symbolize_keys) || {}) # go back to form return render template: [:template] unless @active_admin_import_model.valid? @importer = Importer.new( [:resource_class], @active_admin_import_model, ) begin result = @importer.import if block_given? instance_eval(&block) else instance_exec result, , &DEFAULT_RESULT_PROC end rescue ActiveRecord::Import::MissingColumnError, NoMethodError, ArgumentError, ActiveRecord::StatementInvalid, CSV::MalformedCSVError, ActiveAdminImport::Exception => e Rails.logger.error(I18n.t('active_admin_import.file_error', message: e.)) Rails.logger.error(e.backtrace.join("\n")) flash[:error] = I18n.t('active_admin_import.file_error', message: e.[0..200]) end redirect_to [:back] end # rubocop:enable Metrics/AbcSize end |