app/models/importo/import.rb in importo-2.0.5 vs app/models/importo/import.rb in importo-3.0.9
- old
+ new
@@ -5,10 +5,11 @@
# include ActiveStorage::Downloading
belongs_to :importo_ownable, polymorphic: true
has_many :message_instances, as: :messagable
+ has_many :results, class_name: "Importo::Result", dependent: :delete_all
validates :kind, presence: true
validates :original, presence: true
validate :content_validator
@@ -18,24 +19,22 @@
rescue NoMethodError
# Weird loading sequence error, is fixed by the lib/importo/helpers
end
state_machine :state, initial: :new do
- audit_trail class: ResourceStateTransition, as: :resource if "ResourceStateTransition".safe_constantize
-
state :importing
state :scheduled
state :completed
state :failed
state :reverted
after_transition any => any do |imprt, transition|
- CallbackService.perform_later(import: imprt, callback: transition.to_name)
+ imprt.importer.state_changed(imprt, transition)
end
- after_transition any => :scheduled, do: :schedule_import
- after_transition any => :reverting, do: :schedule_revert
+ after_transition any => :scheduled, :do => :schedule_import
+ after_transition any => :reverting, :do => :schedule_revert
event :schedule do
transition new: :scheduled
end
@@ -44,11 +43,11 @@
transition scheduled: :importing
transition failed: :importing
end
event :complete do
- transition importing: :completed
+ transition importing: :completed, if: ->(import) { import.no_processing? && import.results.count == import.importer.send(:row_count) }
end
event :failure do
transition any => :failed
end
@@ -69,16 +68,44 @@
def allow_export?
importer.class.allow_export?
end
def content_validator
- errors.add(:original, I18n.t('importo.errors.structure_invalid', invalid_headers: importer.invalid_header_names.join(', '))) unless importer.structure_valid?
+ unless importer.structure_valid?
+ errors.add(:original,
+ I18n.t("importo.errors.structure_invalid",
+ invalid_headers: importer.invalid_header_names.join(", ")))
+ end
rescue => e
- errors.add(:original, I18n.t('importo.errors.parse_error', error: e.message))
+ errors.add(:original, I18n.t("importo.errors.parse_error", error: e.message))
end
def importer
@importer ||= "#{kind.camelize}Importer".constantize.new(self)
+ end
+
+ def failure?
+ results.where("details @> ?", '{"state":"failure"}').any?
+ end
+
+ def no_failure?
+ results.where("details @> ?", '{"state":"failure"}').none?
+ end
+
+ def success?
+ results.where("details @> ?", '{"state":"success"}').any?
+ end
+
+ def no_succes?
+ results.where("details @> ?", '{"state":"success"}').none?
+ end
+
+ def processing?
+ results.where("details @> ?", '{"state":"processing"}').any?
+ end
+
+ def no_processing?
+ results.where("details @> ?", '{"state":"processing"}').none?
end
private
def schedule_import