Skip to content

Commit 8f60ef7

Browse files
committed
Consolidate template object building and context application
Replaces `build_template_object` and `apply_import_context` with a single `prepare_import_model` orchestrator that handles the whole pipeline: build the template object → assign form params (if any) → apply context. Both `:import` and `:do_import` collection actions now call one method instead of two, and the form-params extraction is encapsulated inside the helper instead of being duplicated at the call site.
1 parent ae2be23 commit 8f60ef7

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

lib/active_admin_import/dsl.rb

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,18 @@ module ActiveAdminImport
2727
module DSL
2828
CONTEXT_METHOD = :active_admin_import_context
2929

30-
def self.build_template_object(template_object)
31-
template_object.is_a?(Proc) ? template_object.call : template_object
32-
end
33-
34-
def self.apply_import_context(model, controller)
35-
return unless controller.respond_to?(CONTEXT_METHOD, true)
30+
def self.prepare_import_model(template_object, controller, params: nil)
31+
model = template_object.is_a?(Proc) ? template_object.call : template_object
32+
if params
33+
params_key = ActiveModel::Naming.param_key(model.class)
34+
model.assign_attributes(params[params_key].try(:deep_symbolize_keys) || {})
35+
end
36+
return model unless controller.respond_to?(CONTEXT_METHOD, true)
3637
context = controller.send(CONTEXT_METHOD)
37-
return unless context.is_a?(Hash)
38+
return model unless context.is_a?(Hash)
3839
context = context.merge(file: model.file) if model.respond_to?(:file) && !context.key?(:file)
3940
model.assign_attributes(context)
41+
model
4042
end
4143

4244
DEFAULT_RESULT_PROC = lambda do |result, options|
@@ -71,8 +73,7 @@ def active_admin_import(options = {}, &block)
7173

7274
collection_action :import, method: :get do
7375
authorize!(ActiveAdminImport::Auth::IMPORT, active_admin_config.resource_class)
74-
@active_admin_import_model = ActiveAdminImport::DSL.build_template_object(options[:template_object])
75-
ActiveAdminImport::DSL.apply_import_context(@active_admin_import_model, self)
76+
@active_admin_import_model = ActiveAdminImport::DSL.prepare_import_model(options[:template_object], self)
7677
render template: options[:template]
7778
end
7879

@@ -89,10 +90,9 @@ def active_admin_import(options = {}, &block)
8990
authorize!(ActiveAdminImport::Auth::IMPORT, active_admin_config.resource_class)
9091
_params = params.respond_to?(:to_unsafe_h) ? params.to_unsafe_h : params
9192
params = ActiveSupport::HashWithIndifferentAccess.new _params
92-
@active_admin_import_model = ActiveAdminImport::DSL.build_template_object(options[:template_object])
93-
params_key = ActiveModel::Naming.param_key(@active_admin_import_model.class)
94-
@active_admin_import_model.assign_attributes(params[params_key].try(:deep_symbolize_keys) || {})
95-
ActiveAdminImport::DSL.apply_import_context(@active_admin_import_model, self)
93+
@active_admin_import_model = ActiveAdminImport::DSL.prepare_import_model(
94+
options[:template_object], self, params: params
95+
)
9696
# go back to form
9797
return render template: options[:template] unless @active_admin_import_model.valid?
9898
@importer = Importer.new(

0 commit comments

Comments
 (0)