1+ # frozen_string_literal: true
12module ActiveAdminImport
3+ # Declares import functionality
4+ #
5+ # Options
6+ # +back+:: resource action to redirect after processing
7+ # +csv_options+:: hash to override default CSV options
8+ # +batch_size+:: integer value of max record count inserted by 1 query/transaction
9+ # +batch_transaction+:: bool (false by default), if transaction is used when batch importing
10+ # and works when :validate is set to true
11+ # +before_import+:: proc for before import action, hook called with importer object
12+ # +after_import+:: proc for after import action, hook called with importer object
13+ # +before_batch_import+:: proc for before each batch action, called with importer object
14+ # +after_batch_import+:: proc for after each batch action, called with importer object
15+ # +validate+:: true|false, means perform validations or not
16+ # +on_duplicate_key_update+:: an Array or Hash, tells activerecord-import
17+ # to use MySQL's ON DUPLICATE KEY UPDATE ability.
18+ # +timestamps+:: true|false, tells activerecord-import to not add timestamps (if false)
19+ # even if record timestamps is disabled in ActiveRecord::Base
20+ # +ignore+:: true|false, tells activerecord-import to use MySQL's INSERT IGNORE ability
21+ # +template+:: custom template rendering
22+ # +template_object+:: object passing to view
23+ # +resource_class+:: resource class name, override to import to another table (default config.resource_class)
24+ # +resource_label+:: resource label value (default config.resource_label)
25+ # +plural_resource_label+:: pluralized resource label value (default config.plural_resource_label)
26+ #
227 module DSL
3-
4-
5- # Declares import functionality
6- #
7- # Options
8- # +back+:: resource action to redirect after processing
9- # +csv_options+:: hash to override default CSV options
10- # +batch_size+:: integer value of max record count inserted by 1 query/transaction
11- # +batch_transaction+:: bool (false by default), if transaction is used when batch importing and works when :validate is set to true
12- # +before_import+:: proc for before import action, hook called with importer object
13- # +after_import+:: proc for after import action, hook called with importer object
14- # +before_batch_import+:: proc for before each batch action, called with importer object
15- # +after_batch_import+:: proc for after each batch action, called with importer object
16- # +validate+:: true|false, means perform validations or not
17- # +on_duplicate_key_update+:: an Array or Hash, tells activerecord-import to use MySQL's ON DUPLICATE KEY UPDATE ability.
18- # +timestamps+:: true|false, tells activerecord-import to not add timestamps (if false) even if record timestamps is disabled in ActiveRecord::Base
19- # +ignore+:: true|false, tells activerecord-import to use MySQL's INSERT IGNORE ability
20- # +template+:: custom template rendering
21- # +template_object+:: object passing to view
22- # +resource_class+:: resource class name, override to import to another table (default config.resource_class)
23- # +resource_label+:: resource label value (default config.resource_label)
24- # +plural_resource_label+:: pluralized resource label value (default config.plural_resource_label)
25- #
26-
27-
28- DEFAULT_RESULT_PROC = -> ( result , options ) do
29-
28+ DEFAULT_RESULT_PROC = lambda do |result , options |
3029 model_name = options [ :resource_label ] . downcase
3130 plural_model_name = options [ :plural_resource_label ] . downcase
3231 if result . empty?
3332 flash [ :warning ] = I18n . t ( 'active_admin_import.file_empty_error' )
3433 else
35- if result . has_failed?
36- flash [ :error ] = I18n . t ( 'active_admin_import.failed' , count : result . failed . count , model : model_name , plural_model : plural_model_name , message : result . failed_message ( limit : 5 ) )
34+ if result . failed?
35+ flash [ :error ] = I18n . t (
36+ 'active_admin_import.failed' ,
37+ count : result . failed . count ,
38+ model : model_name ,
39+ plural_model : plural_model_name ,
40+ message : result . failed_message ( limit : 5 ) )
3741 return if options [ :batch_transaction ]
3842 end
39- if result . has_imported?
40- flash [ :notice ] = I18n . t ( 'active_admin_import.imported' , count : result . imported_qty , model : model_name , plural_model : plural_model_name )
43+ if result . imported?
44+ flash [ :notice ] = I18n . t (
45+ 'active_admin_import.imported' ,
46+ count : result . imported_qty ,
47+ model : model_name ,
48+ plural_model : plural_model_name )
4149 end
4250 end
4351 end
44-
45-
52+ # rubocop:disable Metrics/AbcSize
4653 def active_admin_import ( options = { } , &block )
4754 options . assert_valid_keys ( *Options ::VALID_OPTIONS )
4855
@@ -57,7 +64,10 @@ def active_admin_import(options = {}, &block)
5764
5865 action_item :import , only : :index do
5966 if authorized? ( ActiveAdminImport ::Auth ::IMPORT , active_admin_config . resource_class )
60- link_to ( I18n . t ( 'active_admin_import.import_model' , plural_model : options [ :plural_resource_label ] ) , action : :import )
67+ link_to (
68+ I18n . t ( 'active_admin_import.import_model' , plural_model : options [ :plural_resource_label ] ) ,
69+ action : :import
70+ )
6171 end
6272 end
6373
@@ -67,24 +77,28 @@ def active_admin_import(options = {}, &block)
6777 params = ActiveSupport ::HashWithIndifferentAccess . new _params
6878 @active_admin_import_model = options [ :template_object ]
6979 @active_admin_import_model . assign_attributes ( params [ params_key ] . try ( :deep_symbolize_keys ) || { } )
70- #go back to form
80+ # go back to form
7181 return render template : options [ :template ] unless @active_admin_import_model . valid?
72- @importer = Importer . new ( options [ :resource_class ] , @active_admin_import_model , options )
82+ @importer = Importer . new (
83+ options [ :resource_class ] ,
84+ @active_admin_import_model ,
85+ options
86+ )
7387 begin
7488 result = @importer . import
7589
7690 if block_given?
77- instance_eval &block
91+ instance_eval ( &block )
7892 else
7993 instance_exec result , options , &DEFAULT_RESULT_PROC
8094 end
81- rescue ActiveRecord ::Import ::MissingColumnError , NoMethodError , ActiveRecord ::StatementInvalid , CSV ::MalformedCSVError => e
95+ rescue ActiveRecord ::Import ::MissingColumnError , NoMethodError , ActiveRecord ::StatementInvalid , CSV ::MalformedCSVError => e
8296 Rails . logger . error ( I18n . t ( 'active_admin_import.file_error' , message : e . message ) )
8397 flash [ :error ] = I18n . t ( 'active_admin_import.file_error' , message : e . message [ 0 ..200 ] )
8498 end
8599 redirect_to options [ :back ]
86100 end
87-
101+ # rubocop:enable Metrics/AbcSize
88102 end
89103 end
90104end
0 commit comments