Skip to content

Mongoid compatibility. #182

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions app/views/admin/import.html.erb
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<p class="active_admin_import_hint">
<%= raw(@active_admin_import_model.hint) %>
<%= raw(@active_admin_import_model.hint) %>
</p>
<%= semantic_form_for @active_admin_import_model, url: {action: :do_import}, html: {multipart: true} do |f| %>
<%= f.inputs name: t("active_admin_import.details") do %>
<%= f.input :file, as: :file %>
<%= f.input :col_sep, collection: [';', ','], include_blank: false, selected: ';' %>
<% end %>

<%= f.actions do %>
<%= f.action :submit, label: t("active_admin_import.import_btn"), button_html: {data: {disable_with: t("active_admin_import.import_btn_disabled")}} %>
<% end %>
<% end %>
<% end %>
6 changes: 1 addition & 5 deletions lib/active_admin_import/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,7 @@ def active_admin_import(options = {}, &block)
else
instance_exec result, options, &DEFAULT_RESULT_PROC
end
rescue ActiveRecord::Import::MissingColumnError,
NoMethodError,
ActiveRecord::StatementInvalid,
CSV::MalformedCSVError,
ActiveAdminImport::Exception => e
rescue StandardError => e
Rails.logger.error(I18n.t('active_admin_import.file_error', message: e.message))
Rails.logger.error(e.backtrace.join("\n"))
flash[:error] = I18n.t('active_admin_import.file_error', message: e.message[0..200])
Expand Down
41 changes: 27 additions & 14 deletions lib/active_admin_import/importer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def file
end

def cycle(lines)
@csv_lines = CSV.parse(lines.join, @csv_options)
@csv_lines = lines
import_result.add(batch_import, lines.count)
end

Expand Down Expand Up @@ -113,17 +113,20 @@ def header_index(header_key)
def process_file
lines = []
batch_size = options[:batch_size].to_i
File.open(file.path) do |f|
# capture headers if not exist
prepare_headers { CSV.parse(f.readline, @csv_options).first }
f.each_line do |line|
lines << line if line.present?
if lines.size == batch_size || f.eof?
cycle(lines)
lines = []
end

csv = CSV.read(file.path, @csv_options)

prepare_headers { csv.shift }

csv.each do |line|
lines << line

if lines.size == batch_size
cycle(lines)
lines = []
end
end

cycle(lines) unless lines.blank?
end

Expand All @@ -139,12 +142,21 @@ def run_callback(name)
end

def batch_import
@resource.respond_to?(:transaction) ? batch_improt_with_transaction : batch_import_without_transaction

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@resource.respond_to?(:transaction) ? batch_improt_with_transaction : batch_import_without_transaction
@resource.respond_to?(:transaction) ? batch_import_with_transaction : batch_import_without_transaction

end

def batch_import_without_transaction
run_callback(:before_batch_import)
batch_result = resource.import(headers.values, csv_lines, import_options)
raise ActiveRecord::Rollback if Object.const_defined?('ActiveRecord::Rollback') && import_options[:batch_transaction] && batch_result.failed_instances.any?
run_callback(:after_batch_import)
batch_result
end

def batch_improt_with_transaction

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def batch_improt_with_transaction
def batch_import_with_transaction

batch_result = nil
@resource.transaction do
run_callback(:before_batch_import)
batch_result = resource.import(headers.values, csv_lines, import_options)
raise ActiveRecord::Rollback if import_options[:batch_transaction] && batch_result.failed_instances.any?
run_callback(:after_batch_import)
batch_result = batch_import_without_transaction
end
batch_result
end
Expand All @@ -163,6 +175,7 @@ def detect_csv_options
else
options[:csv_options] || {}
end.reject { |_, value| value.nil? || value == "" }
@csv_options[:col_sep] = model.col_sep if model.respond_to?(:col_sep)
end
end
end
3 changes: 2 additions & 1 deletion lib/active_admin_import/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ def default_attributes
csv_headers: [],
file: nil,
force_encoding: 'UTF-8',
hint: ''
hint: '',
col_sep: ';'
}
end

Expand Down