diff --git a/app/controllers/imports_controller.rb b/app/controllers/imports_controller.rb index c1b51c23..20e5f9c4 100644 --- a/app/controllers/imports_controller.rb +++ b/app/controllers/imports_controller.rb @@ -5,6 +5,8 @@ class ImportsController < ApplicationController @import.publish_later redirect_to import_path(@import), notice: "Your import has started in the background." + rescue Import::MaxRowCountExceededError + redirect_back_or_to import_path(@import), alert: "Your import exceeds the maximum row count of #{@import.max_row_count}." end def index diff --git a/app/models/account_import.rb b/app/models/account_import.rb index f3455e8a..aa4c6dfe 100644 --- a/app/models/account_import.rb +++ b/app/models/account_import.rb @@ -1,7 +1,5 @@ class AccountImport < Import def import! - raise "Account import is limited to 50 rows" if rows.count > 50 - transaction do rows.each do |row| mapping = mappings.account_types.find_by(key: row.entity_type) @@ -56,4 +54,8 @@ class AccountImport < Import CSV.parse(template, headers: true) end + + def max_row_count + 50 + end end diff --git a/app/models/import.rb b/app/models/import.rb index e96d1fc1..b0a02ea0 100644 --- a/app/models/import.rb +++ b/app/models/import.rb @@ -1,4 +1,6 @@ class Import < ApplicationRecord + MaxRowCountExceededError = Class.new(StandardError) + TYPES = %w[TransactionImport TradeImport AccountImport MintImport].freeze SIGNAGE_CONVENTIONS = %w[inflows_positive inflows_negative] SEPARATORS = [ [ "Comma (,)", "," ], [ "Semicolon (;)", ";" ] ].freeze @@ -52,6 +54,7 @@ class Import < ApplicationRecord end def publish_later + raise MaxRowCountExceededError if row_count_exceeded? raise "Import is not publishable" unless publishable? update! status: :importing @@ -60,6 +63,8 @@ class Import < ApplicationRecord end def publish + raise MaxRowCountExceededError if row_count_exceeded? + import! family.sync_later @@ -220,7 +225,15 @@ class Import < ApplicationRecord ) end + def max_row_count + 10000 + end + private + def row_count_exceeded? + rows.count > max_row_count + end + def import! # no-op, subclasses can implement for customization of algorithm end