diff options
Diffstat (limited to 'app/models')
| -rw-r--r-- | app/models/concerns/iev_interfaces/task.rb | 43 | ||||
| -rw-r--r-- | app/models/export/base.rb | 25 | ||||
| -rw-r--r-- | app/models/export/message.rb | 8 | ||||
| -rw-r--r-- | app/models/export/resource.rb | 8 | ||||
| -rw-r--r-- | app/models/import/base.rb | 37 |
5 files changed, 86 insertions, 35 deletions
diff --git a/app/models/concerns/iev_interfaces/task.rb b/app/models/concerns/iev_interfaces/task.rb index 2eb0c532f..b142850fb 100644 --- a/app/models/concerns/iev_interfaces/task.rb +++ b/app/models/concerns/iev_interfaces/task.rb @@ -6,6 +6,11 @@ module IevInterfaces::Task belongs_to :workbench, class_name: "::Workbench" belongs_to :referential + mount_uploader :file, ImportUploader + validates :file, presence: true + + has_many :children, foreign_key: :parent_id, class_name: self.name, dependent: :destroy + extend Enumerize enumerize :status, in: %w(new pending successful warning failed running aborted canceled), scope: true, default: :new @@ -20,6 +25,8 @@ module IevInterfaces::Task end scope :blocked, -> { where('created_at < ? AND status = ?', 4.hours.ago, 'running') } + + before_create :initialize_fields end module ClassMethods @@ -48,4 +55,40 @@ module IevInterfaces::Task parent.child_change update(notified_parent_at: DateTime.now) end + + def children_succeedeed + children.with_status(:successful, :warning).count + end + + def update_status + status = + if children.where(status: self.class.failed_statuses).count > 0 + 'failed' + elsif children.where(status: "warning").count > 0 + 'warning' + elsif children.where(status: "successful").count == children.count + 'successful' + end + + attributes = { + current_step: children.count, + status: status + } + + if self.class.finished_statuses.include?(status) + attributes[:ended_at] = Time.now + end + + update attributes + end + + def child_change + return if self.class.finished_statuses.include?(status) + + update_status + end + + private + def initialize_fields + end end diff --git a/app/models/export/base.rb b/app/models/export/base.rb new file mode 100644 index 000000000..d809055db --- /dev/null +++ b/app/models/export/base.rb @@ -0,0 +1,25 @@ +class Export::Base < ActiveRecord::Base + self.table_name = "exports" + + def self.messages_class_name + "Export::Message" + end + + def self.resources_class_name + "Export::Resource" + end + + include IevInterfaces::Task + + def self.model_name + ActiveModel::Name.new Export::Base, Export::Base, "Export::Base" + end + + private + + def initialize_fields + super + self.token_upload = SecureRandom.urlsafe_base64 + end + +end diff --git a/app/models/export/message.rb b/app/models/export/message.rb new file mode 100644 index 000000000..b64b524ac --- /dev/null +++ b/app/models/export/message.rb @@ -0,0 +1,8 @@ +class Export::Message < ActiveRecord::Base + self.table_name = :export_messages + + include IevInterfaces::Message + + belongs_to :export, class_name: Export::Base + belongs_to :resource, class_name: Export::Resource +end diff --git a/app/models/export/resource.rb b/app/models/export/resource.rb new file mode 100644 index 000000000..98f103be4 --- /dev/null +++ b/app/models/export/resource.rb @@ -0,0 +1,8 @@ +class Export::Resource < ActiveRecord::Base + self.table_name = :export_resources + + include IevInterfaces::Resource + + belongs_to :export, class_name: Export::Base + has_many :messages, class_name: "ExportMessage", foreign_key: :resource_id +end diff --git a/app/models/import/base.rb b/app/models/import/base.rb index 0e1eae2c0..e2ae129be 100644 --- a/app/models/import/base.rb +++ b/app/models/import/base.rb @@ -11,51 +11,17 @@ class Import::Base < ActiveRecord::Base include IevInterfaces::Task - mount_uploader :file, ImportUploader - - has_many :children, foreign_key: :parent_id, class_name: "Import::Base", dependent: :destroy - - validates :file, presence: true - - before_create :initialize_fields - def self.model_name ActiveModel::Name.new Import::Base, Import::Base, "Import::Base" end - def children_succeedeed - children.with_status(:successful, :warning).count - end - def child_change return if self.class.finished_statuses.include?(status) - update_status + super update_referentials end - def update_status - status = - if children.where(status: self.class.failed_statuses).count > 0 - 'failed' - elsif children.where(status: "warning").count > 0 - 'warning' - elsif children.where(status: "successful").count == children.count - 'successful' - end - - attributes = { - current_step: children.count, - status: status - } - - if self.class.finished_statuses.include?(status) - attributes[:ended_at] = Time.now - end - - update attributes - end - def update_referentials return unless self.class.finished_statuses.include?(status) @@ -67,6 +33,7 @@ class Import::Base < ActiveRecord::Base private def initialize_fields + super self.token_download = SecureRandom.urlsafe_base64 end |
