Skip to content

Commit

Permalink
Check records count against allowed limit when importing
Browse files Browse the repository at this point in the history
  • Loading branch information
codealchemy committed Feb 15, 2016
1 parent 8946cc8 commit c11f58f
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions lib/rails_admin_import/importer.rb
Expand Up @@ -16,10 +16,13 @@ def import(records)
begin
init_results

# TODO: re-implement file size check
# if file_check.readlines.size > RailsAdminImport.config.line_item_limit
# return results = { :success => [], :error => ["Please limit upload file to #{RailsAdminImport.config.line_item_limit} line items."] }
# end

if records.count > RailsAdminImport.config.line_item_limit
return results = {
success: [],
error: ["Please limit upload file to #{RailsAdminImport.config.line_item_limit} line items."]
}
end

with_transaction do
records.each do |record|
Expand Down Expand Up @@ -64,7 +67,7 @@ def rollback_if_error
def import_record(record)
if update_lookup && !record.has_key?(update_lookup)
raise UpdateLookupError, I18n.t("admin.import.missing_update_lookup")
end
end

object = find_or_create_object(record, update_lookup)
action = object.new_record? ? :create : :update
Expand Down Expand Up @@ -142,7 +145,7 @@ def format_result_message(type, array)
name: result_count,
action: I18n.t("admin.actions.import.done"))
end

def perform_model_callback(object, method_name, record)
if object.respond_to?(method_name)
# Compatibility: Old import hook took 2 arguments.
Expand Down Expand Up @@ -175,7 +178,7 @@ def find_or_create_object(record, update)
model = import_model.model
object = if update.present?
model.where(update => record[update]).first
end
end

if object.nil?
object = model.new(new_attrs)
Expand Down Expand Up @@ -221,4 +224,3 @@ def extract_mapping(value, mapping_key)
end
end
end

0 comments on commit c11f58f

Please sign in to comment.