Module: ActiveAdminImport::DSL
- Defined in:
- lib/active_admin_import/dsl.rb
Overview
Declares import functionality
Options
backresource action to redirect after processing
csv_optionshash to override default CSV options
batch_sizeinteger value of max record count inserted by 1 query/transaction
batch_transactionbool (false by default), if transaction is used when batch importing
and works when :validate is set to true
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
validatetrue|false, means perform validations or not
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 to use MySQL's INSERT IGNORE ability
templatecustom template rendering
template_objectobject passing to view
resource_classresource class name, override to import to another table (default config.resource_class)
resource_labelresource label value (default config.resource_label)
plural_resource_labelpluralized resource label value (default config.plural_resource_label)
Constant Summary collapse
- CONTEXT_METHOD =
:active_admin_import_context- ACTIVE_ADMIN_V4 =
Gem::Version.new(ActiveAdmin::VERSION) >= Gem::Version.new('4.0.0.beta1')
- DEFAULT_RESULT_PROC =
lambda do |result, | model_name = [:resource_label].downcase plural_model_name = [:plural_resource_label].downcase if result.empty? flash[ACTIVE_ADMIN_V4 ? :alert : :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
Class Method Summary collapse
Instance Method Summary collapse
-
#active_admin_import(options = {}, &block) ⇒ Object
rubocop:disable Metrics/AbcSize.
Class Method Details
.prepare_import_model(template_object, controller, params: nil) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/active_admin_import/dsl.rb', line 32 def self.prepare_import_model(template_object, controller, params: nil) model = template_object.is_a?(Proc) ? template_object.call : template_object if params params_key = ActiveModel::Naming.param_key(model.class) model.assign_attributes(params[params_key].try(:deep_symbolize_keys) || {}) end return model unless controller.respond_to?(CONTEXT_METHOD, true) context = controller.send(CONTEXT_METHOD) return model unless context.is_a?(Hash) context = context.merge(file: model.file) if model.respond_to?(:file) && !context.key?(:file) model.assign_attributes(context) model end |
Instance Method Details
#active_admin_import(options = {}, &block) ⇒ Object
rubocop:disable Metrics/AbcSize
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 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/active_admin_import/dsl.rb', line 71 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 = ActiveAdminImport::DSL.prepare_import_model([:template_object], self) 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 }, [:action_item_html_options] ) 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 = ActiveAdminImport::DSL.prepare_import_model( [:template_object], self, params: params ) # 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_exec result, , &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 |