aboutsummaryrefslogtreecommitdiffstats
path: root/app/models
diff options
context:
space:
mode:
authorZog2018-04-18 13:19:18 +0200
committerJohan Van Ryseghem2018-04-27 11:17:19 +0200
commitf7fe4c24e29ceb517abdda66899952a5d6aa73bf (patch)
tree7fdd8323e8e5a0189771976878310167b6cdac49 /app/models
parent0046e5a01cb08c10118b01c50f3c52d159854ef0 (diff)
downloadchouette-core-f7fe4c24e29ceb517abdda66899952a5d6aa73bf.tar.bz2
Refs #6572; New Referential#Show for noredy referentials
Diffstat (limited to 'app/models')
-rw-r--r--app/models/clean_up.rb4
-rw-r--r--app/models/concerns/iev_interfaces/task.rb4
-rw-r--r--app/models/referential.rb30
3 files changed, 33 insertions, 5 deletions
diff --git a/app/models/clean_up.rb b/app/models/clean_up.rb
index ca4f6312b..0f73e07b2 100644
--- a/app/models/clean_up.rb
+++ b/app/models/clean_up.rb
@@ -12,6 +12,10 @@ class CleanUp < ApplicationModel
validate :end_date_must_be_greater_that_begin_date
after_commit :perform_cleanup, :on => :create
+ scope :for_referential, ->(referential) do
+ where(referential_id: referential.id)
+ end
+
def end_date_must_be_greater_that_begin_date
if self.end_date && self.date_type == 'between' && self.begin_date >= self.end_date
errors.add(:base, I18n.t('activerecord.errors.models.clean_up.invalid_period'))
diff --git a/app/models/concerns/iev_interfaces/task.rb b/app/models/concerns/iev_interfaces/task.rb
index f052b3a8f..6be33734b 100644
--- a/app/models/concerns/iev_interfaces/task.rb
+++ b/app/models/concerns/iev_interfaces/task.rb
@@ -23,6 +23,10 @@ module IevInterfaces::Task
where('started_at BETWEEN :begin AND :end', begin: period_range.begin, end: period_range.end)
end
+ scope :for_referential, ->(referential) do
+ where(referential_id: referential.id)
+ end
+
scope :blocked, -> { where('created_at < ? AND status = ?', 4.hours.ago, 'running') }
before_save :initialize_fields, on: :create
diff --git a/app/models/referential.rb b/app/models/referential.rb
index c7c22604d..2449d767d 100644
--- a/app/models/referential.rb
+++ b/app/models/referential.rb
@@ -59,12 +59,12 @@ class Referential < ApplicationModel
scope :pending, -> { where(ready: false, failed_at: nil, archived_at: nil) }
- scope :ready, -> { where(ready: true, failed_at: nil, archived_at: nil) }
+ scope :active, -> { where(ready: true, failed_at: nil, archived_at: nil) }
scope :failed, -> { where.not(failed_at: nil) }
scope :archived, -> { where.not(archived_at: nil) }
- scope :ready, -> { where(ready: true, failed_at: nil, archived_at: nil) }
- scope :ready, -> { where(ready: true, failed_at: nil, archived_at: nil) }
+ scope :ready, -> { where(ready: true) }
+
scope :in_periode, ->(periode) { where(id: referential_ids_in_periode(periode)) }
scope :include_metadatas_lines, ->(line_ids) { where('referential_metadata.line_ids && ARRAY[?]::bigint[]', line_ids) }
scope :order_by_validity_period, ->(dir) { joins(:metadatas).order("unnest(periodes) #{dir}") }
@@ -122,6 +122,20 @@ class Referential < ApplicationModel
@_models_with_checksum || []
end
+ OPERATIONS = [Import::Netex, Import::Gtfs, CleanUp]
+
+ def last_operation
+ operations = []
+ Referential::OPERATIONS.each do |klass|
+ operations << klass.for_referential(self).limit(1).select("'#{klass.name}' as kind, id, created_at").order('created_at DESC').to_sql
+ end
+ sql = "SELECT * FROM ((#{operations.join(') UNION (')})) AS subquery ORDER BY subquery.created_at DESC"
+ res = ActiveRecord::Base.connection.execute(sql).first
+ if res
+ res["kind"].constantize.find(res["id"])
+ end
+ end
+
def lines
if metadatas.blank?
workbench ? workbench.lines : associated_lines
@@ -538,7 +552,7 @@ class Referential < ApplicationModel
def state
return :failed if failed_at.present?
return :archived if archived_at.present?
- ready ? :ready : :pending
+ ready? ? :active : :pending
end
def pending!
@@ -549,7 +563,7 @@ class Referential < ApplicationModel
update ready: false, failed_at: Time.now, archived_at: nil
end
- def ready!
+ def active!
update ready: true, failed_at: nil, archived_at: nil
end
@@ -557,6 +571,12 @@ class Referential < ApplicationModel
update failed_at: nil, archived_at: Time.now
end
+ %i(pending active failed archived).each do |s|
+ define_method "#{s}?" do
+ state == s
+ end
+ end
+
def pending_while
vals = attributes.slice(*%w(ready archived_at failed_at))
pending!