aboutsummaryrefslogtreecommitdiffstats
path: root/app/models/clean_up.rb
blob: a44bb46a42191e9d194b21d3df385c4ef5d4e050 (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
class CleanUp < ActiveRecord::Base
  include AASM
  belongs_to :referential
  has_one :clean_up_result

  validates :begin_date, presence: true
  after_commit :perform_cleanup, :on => :create

  def perform_cleanup
    CleanUpWorker.perform_async(self.id)
  end

  def clean
    result = {}
    result['time_table_count']      = self.clean_time_tables
    result['vehicle_journey_count'] = self.clean_vehicle_journeys
    result['journey_pattern_count'] = self.clean_journey_patterns
    result
  end

  def clean_time_tables
    Chouette::TimeTable.validity_out_between?(begin_date, end_date).delete_all
  end

  def clean_vehicle_journeys
    ids = Chouette::VehicleJourney.includes(:time_tables).where(:time_tables => {id: nil}).pluck(:id)
    Chouette::VehicleJourney.where(id: ids).delete_all
  end

  def clean_journey_patterns
    ids = Chouette::JourneyPattern.includes(:vehicle_journeys).where(:vehicle_journeys => {id: nil}).pluck(:id)
    Chouette::JourneyPattern.where(id: ids).delete_all
  end

  aasm column: :status do
    state :new, :initial => true
    state :pending
    state :successful
    state :failed

    event :run, after: :log_pending do
      transitions :from => [:new, :failed], :to => :pending
    end

    event :successful, after: :log_successful do
      transitions :from => [:pending, :failed], :to => :successful
    end

    event :failed, after: :log_failed do
      transitions :from => :pending, :to => :failed
    end
  end

  def log_pending
    update_attribute(:started_at, Time.now)
  end

  def log_successful message_attributs
    update_attribute(:ended_at, Time.now)
    CleanUpResult.create(clean_up: self, message_key: :successfull, message_attributs: message_attributs)
  end

  def log_failed message_attributs
    update_attribute(:ended_at, Time.now)
    CleanUpResult.create(clean_up: self, message_key: :failed, message_attributs: message_attributs)
  end
end