aboutsummaryrefslogtreecommitdiffstats
path: root/app/models/import/base.rb
blob: 7c1cfaa50d6bbf094fb813e1ff8f85035fe31317 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
class Import::Base < ApplicationModel
  self.table_name = "imports"

  def self.messages_class_name
    "Import::Message"
  end

  def self.resources_class_name
    "Import::Resource"
  end

  def self.file_extension_whitelist
    %w(zip)
  end

  include IevInterfaces::Task

  def self.model_name
    ActiveModel::Name.new Import::Base, Import::Base, "Import"
  end

  def child_change
    Rails.logger.info "child_change for #{inspect}"
    return if self.class.finished_statuses.include?(status)

    super
    update_referentials
  end

  def update_referentials
    Rails.logger.info "update_referentials for #{inspect}"
    return unless self.class.finished_statuses.include?(status)

    # We treat all created referentials in a batch
    # If a single fails, we consider they all failed
    # Ohana means family !
    if self.successful?
      children.map(&:referential).compact.each &:active!
    else
      children.map(&:referential).compact.each &:failed!
    end
  end

  private

  def initialize_fields
    super
    self.token_download ||= SecureRandom.urlsafe_base64
  end

end