Skip to content

Commit

Permalink
Merge pull request #201 from BigG1947/fix_bugs_with_cashed_model
Browse files Browse the repository at this point in the history
Fix bugs with cached models
  • Loading branch information
senid231 authored Nov 1, 2023
2 parents e6eef78 + a71035e commit 642bb62
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
14 changes: 11 additions & 3 deletions lib/active_admin_import/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,14 @@ def active_admin_import(options = {}, &block)
options.assert_valid_keys(*Options::VALID_OPTIONS)

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

collection_action :import, method: :get do
authorize!(ActiveAdminImport::Auth::IMPORT, active_admin_config.resource_class)
@active_admin_import_model = options[:template_object]
@active_admin_import_model = if options[:template_object].is_a?(Proc)
options[:template_object].call
else
options[:template_object]
end
render template: options[:template]
end

Expand All @@ -75,7 +78,12 @@ def active_admin_import(options = {}, &block)
authorize!(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 = options[:template_object]
@active_admin_import_model = if options[:template_object].is_a?(Proc)
options[:template_object].call
else
options[: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: options[:template] unless @active_admin_import_model.valid?
Expand Down
2 changes: 1 addition & 1 deletion lib/active_admin_import/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ module Options

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

{
Expand Down
32 changes: 32 additions & 0 deletions spec/import_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,38 @@ def upload_file!(name, ext = 'csv')
end
include_examples 'successful inserts for author'
end

context 'when template object passed like proc' do
before do
add_post_resource(template_object: -> { ActiveAdminImport::Model.new(author_id: author.id) },
validate: true,
before_batch_import: lambda do |importer|
importer.csv_lines.map! { |row| row << importer.model.author_id }
importer.headers.merge!(:'Author Id' => :author_id)
end
)

visit '/admin/posts/import'
upload_file!(:posts_for_author)
end

include_examples 'successful inserts for author'

context 'after successful import try without file' do
let(:after_successful_import_do!) do
# reload page
visit '/admin/posts/import'
# submit form without file
find_button('Import').click
end

it 'should render validation error' do
after_successful_import_do!

expect(page).to have_content I18n.t('active_admin_import.no_file_error')
end
end
end
end

context 'for csv with author name' do
Expand Down

0 comments on commit 642bb62

Please sign in to comment.