Skip to content

Commit 642bb62

Browse files
authored
Merge pull request #201 from BigG1947/fix_bugs_with_cashed_model
Fix bugs with cached models
2 parents e6eef78 + a71035e commit 642bb62

File tree

3 files changed

+44
-4
lines changed

3 files changed

+44
-4
lines changed

lib/active_admin_import/dsl.rb

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,14 @@ def active_admin_import(options = {}, &block)
5454
options.assert_valid_keys(*Options::VALID_OPTIONS)
5555

5656
options = Options.options_for(config, options)
57-
params_key = ActiveModel::Naming.param_key(options[:template_object])
5857

5958
collection_action :import, method: :get do
6059
authorize!(ActiveAdminImport::Auth::IMPORT, active_admin_config.resource_class)
61-
@active_admin_import_model = options[:template_object]
60+
@active_admin_import_model = if options[:template_object].is_a?(Proc)
61+
options[:template_object].call
62+
else
63+
options[:template_object]
64+
end
6265
render template: options[:template]
6366
end
6467

@@ -75,7 +78,12 @@ def active_admin_import(options = {}, &block)
7578
authorize!(ActiveAdminImport::Auth::IMPORT, active_admin_config.resource_class)
7679
_params = params.respond_to?(:to_unsafe_h) ? params.to_unsafe_h : params
7780
params = ActiveSupport::HashWithIndifferentAccess.new _params
78-
@active_admin_import_model = options[:template_object]
81+
@active_admin_import_model = if options[:template_object].is_a?(Proc)
82+
options[:template_object].call
83+
else
84+
options[:template_object]
85+
end
86+
params_key = ActiveModel::Naming.param_key(@active_admin_import_model.class)
7987
@active_admin_import_model.assign_attributes(params[params_key].try(:deep_symbolize_keys) || {})
8088
# go back to form
8189
return render template: options[:template] unless @active_admin_import_model.valid?

lib/active_admin_import/options.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ module Options
2626

2727
def self.options_for(config, options = {})
2828
unless options.key? :template_object
29-
options[:template_object] = ActiveAdminImport::Model.new
29+
options[:template_object] = -> { ActiveAdminImport::Model.new }
3030
end
3131

3232
{

spec/import_spec.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,38 @@ def upload_file!(name, ext = 'csv')
9696
end
9797
include_examples 'successful inserts for author'
9898
end
99+
100+
context 'when template object passed like proc' do
101+
before do
102+
add_post_resource(template_object: -> { ActiveAdminImport::Model.new(author_id: author.id) },
103+
validate: true,
104+
before_batch_import: lambda do |importer|
105+
importer.csv_lines.map! { |row| row << importer.model.author_id }
106+
importer.headers.merge!(:'Author Id' => :author_id)
107+
end
108+
)
109+
110+
visit '/admin/posts/import'
111+
upload_file!(:posts_for_author)
112+
end
113+
114+
include_examples 'successful inserts for author'
115+
116+
context 'after successful import try without file' do
117+
let(:after_successful_import_do!) do
118+
# reload page
119+
visit '/admin/posts/import'
120+
# submit form without file
121+
find_button('Import').click
122+
end
123+
124+
it 'should render validation error' do
125+
after_successful_import_do!
126+
127+
expect(page).to have_content I18n.t('active_admin_import.no_file_error')
128+
end
129+
end
130+
end
99131
end
100132

101133
context 'for csv with author name' do

0 commit comments

Comments
 (0)