diff options
| author | Zog | 2018-04-18 09:59:15 +0200 | 
|---|---|---|
| committer | Johan Van Ryseghem | 2018-04-27 11:17:19 +0200 | 
| commit | 0046e5a01cb08c10118b01c50f3c52d159854ef0 (patch) | |
| tree | 6e690461a9266716d31f14611235eab9f1f76e33 /app/models | |
| parent | 681f874e3150c35b03e74b25827b74791ae9ae95 (diff) | |
| download | chouette-core-0046e5a01cb08c10118b01c50f3c52d159854ef0.tar.bz2 | |
Refs #6572; Use new states
Diffstat (limited to 'app/models')
| -rw-r--r-- | app/models/clean_up.rb | 39 | ||||
| -rw-r--r-- | app/models/import/gtfs.rb | 3 | ||||
| -rw-r--r-- | app/models/merge.rb | 6 | ||||
| -rw-r--r-- | app/models/referential.rb | 54 | 
4 files changed, 62 insertions, 40 deletions
| diff --git a/app/models/clean_up.rb b/app/models/clean_up.rb index 761fc47be..ca4f6312b 100644 --- a/app/models/clean_up.rb +++ b/app/models/clean_up.rb @@ -24,28 +24,29 @@ class CleanUp < ApplicationModel    def clean      referential.switch - -    {}.tap do |result| -      if date_type -        processed = send("destroy_time_tables_#{self.date_type}") -        if processed -          result['time_table']      = processed[:time_tables].try(:count) -          result['vehicle_journey'] = processed[:vehicle_journeys].try(:count) -        end -        result['time_table_date']   = send("destroy_time_tables_dates_#{self.date_type}").try(:count) -        result['time_table_period'] = send("destroy_time_tables_periods_#{self.date_type}").try(:count) -        self.overlapping_periods.each do |period| -          exclude_dates_in_overlapping_period(period) +    referential.pending_while do +      {}.tap do |result| +        if date_type +          processed = send("destroy_time_tables_#{self.date_type}") +          if processed +            result['time_table']      = processed[:time_tables].try(:count) +            result['vehicle_journey'] = processed[:vehicle_journeys].try(:count) +          end +          result['time_table_date']   = send("destroy_time_tables_dates_#{self.date_type}").try(:count) +          result['time_table_period'] = send("destroy_time_tables_periods_#{self.date_type}").try(:count) +          self.overlapping_periods.each do |period| +            exclude_dates_in_overlapping_period(period) +          end          end -      end -      destroy_vehicle_journeys_outside_referential -      # Disabled for the moment. See #5372 -      # destroy_time_tables_outside_referential +        destroy_vehicle_journeys_outside_referential +        # Disabled for the moment. See #5372 +        # destroy_time_tables_outside_referential -      destroy_vehicle_journeys -      destroy_journey_patterns -      destroy_routes +        destroy_vehicle_journeys +        destroy_journey_patterns +        destroy_routes +      end      end    end diff --git a/app/models/import/gtfs.rb b/app/models/import/gtfs.rb index a20c468c1..ceb849bd8 100644 --- a/app/models/import/gtfs.rb +++ b/app/models/import/gtfs.rb @@ -10,12 +10,13 @@ class Import::Gtfs < Import::Base      import_without_status      update status: 'successful', ended_at: Time.now +    referential&.ready!    rescue Exception => e      update status: 'failed', ended_at: Time.now      Rails.logger.error "Error in GTFS import: #{e} #{e.backtrace.join('\n')}" +    referential&.failed!    ensure      notify_parent -    referential&.update ready: true    end    def self.accept_file?(file) diff --git a/app/models/merge.rb b/app/models/merge.rb index be1bbedcb..2824e1f83 100644 --- a/app/models/merge.rb +++ b/app/models/merge.rb @@ -39,10 +39,14 @@ class Merge < ApplicationModel    rescue => e      Rails.logger.error "Merge failed: #{e} #{e.backtrace.join("\n")}"      update status: :failed +    new&.failed!      raise e if Rails.env.test?    ensure      attributes = { ended_at: Time.now } -    attributes[:status] = :successful if status == :running +    if status == :running +      attributes[:status] = :successful +      referentials.each &:archived! +    end      update attributes    end diff --git a/app/models/referential.rb b/app/models/referential.rb index 486c3c495..c7c22604d 100644 --- a/app/models/referential.rb +++ b/app/models/referential.rb @@ -58,7 +58,7 @@ class Referential < ApplicationModel    belongs_to :referential_suite -  scope :pending, -> { where(ready: false, failed_at: nil, archived_at: nil, failed_at: nil) } +  scope :pending, -> { where(ready: false, failed_at: nil, archived_at: nil) }    scope :ready, -> { where(ready: true, failed_at: nil, archived_at: nil) }    scope :failed, -> { where.not(failed_at: nil) }    scope :archived, -> { where.not(archived_at: nil) } @@ -122,24 +122,6 @@ class Referential < ApplicationModel      @_models_with_checksum || []    end -  def state -    return :failed if failed_at.present? -    return :archived if archived_at.present? -    ready ? :ready : :pending -  end - -  def failed! -    update ready: false, failed_at: Time.now, archived_at: nil -  end - -  def ready! -    update ready: true, failed_at: nil, archived_at: nil -  end - -  def archived! -    update ready: true, failed_at: nil, archived_at: Time.now -  end -    def lines      if metadatas.blank?        workbench ? workbench.lines : associated_lines @@ -551,6 +533,40 @@ class Referential < ApplicationModel      ready.not_merged.not_in_referential_suite    end +  ### STATE + +  def state +    return :failed if failed_at.present? +    return :archived if archived_at.present? +    ready ? :ready : :pending +  end + +  def pending! +    update ready: false, failed_at: nil, archived_at: nil +  end + +  def failed! +    update ready: false, failed_at: Time.now, archived_at: nil +  end + +  def ready! +    update ready: true, failed_at: nil, archived_at: nil +  end + +  def archived! +    update failed_at: nil, archived_at: Time.now +  end + +  def pending_while +    vals = attributes.slice(*%w(ready archived_at failed_at)) +    pending! +    begin +      yield +    ensure +      update vals +    end +  end +    private    def lock_table | 
