aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorZog2018-04-18 09:59:15 +0200
committerJohan Van Ryseghem2018-04-27 11:17:19 +0200
commit0046e5a01cb08c10118b01c50f3c52d159854ef0 (patch)
tree6e690461a9266716d31f14611235eab9f1f76e33
parent681f874e3150c35b03e74b25827b74791ae9ae95 (diff)
downloadchouette-core-0046e5a01cb08c10118b01c50f3c52d159854ef0.tar.bz2
Refs #6572; Use new states
-rw-r--r--app/models/clean_up.rb39
-rw-r--r--app/models/import/gtfs.rb3
-rw-r--r--app/models/merge.rb6
-rw-r--r--app/models/referential.rb54
-rw-r--r--spec/factories/clean_ups.rb1
-rw-r--r--spec/models/merge_spec.rb3
-rw-r--r--spec/models/referential_spec.rb20
7 files changed, 86 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
diff --git a/spec/factories/clean_ups.rb b/spec/factories/clean_ups.rb
index 7107769ff..e366a113b 100644
--- a/spec/factories/clean_ups.rb
+++ b/spec/factories/clean_ups.rb
@@ -1,5 +1,6 @@
FactoryGirl.define do
factory :clean_up do
+ referential
begin_date { Date.today}
end_date { Date.today + 1.month }
date_type { :before }
diff --git a/spec/models/merge_spec.rb b/spec/models/merge_spec.rb
index 242fcddcd..5d5320930 100644
--- a/spec/models/merge_spec.rb
+++ b/spec/models/merge_spec.rb
@@ -109,6 +109,9 @@ RSpec.describe Merge do
end
end
+ expect(output.state).to eq :ready
+ expect(referential.reload.state).to eq :archived
+
end
end
diff --git a/spec/models/referential_spec.rb b/spec/models/referential_spec.rb
index 2fb8b5cb0..220201d37 100644
--- a/spec/models/referential_spec.rb
+++ b/spec/models/referential_spec.rb
@@ -73,6 +73,26 @@ describe Referential, :type => :model do
expect(Referential.archived).to include referential
end
end
+
+ context 'pending_while' do
+ it "should preserve the state" do
+ referential = create :referential
+ referential.archived!
+ expect(referential.state).to eq :archived
+ referential.pending_while do
+ expect(referential.state).to eq :pending
+ end
+ expect(referential.state).to eq :archived
+ begin
+ referential.pending_while do
+ expect(referential.state).to eq :pending
+ raise
+ end
+ rescue
+ end
+ expect(referential.state).to eq :archived
+ end
+ end
end
context ".referential_ids_in_periode" do