2021-12-01 20:00:29 +01:00
|
|
|
class Zone < ApplicationRecord
|
|
|
|
validates :acronym, presence: true, uniqueness: true
|
2022-08-11 19:05:12 +02:00
|
|
|
has_many :labels, -> { order(designated_on: :desc) }, class_name: 'ZoneLabel', inverse_of: :zone
|
2022-08-24 12:39:07 +02:00
|
|
|
has_and_belongs_to_many :procedures, -> { order(published_at: :desc) }, inverse_of: :zone
|
2022-08-11 19:05:12 +02:00
|
|
|
|
2022-09-26 15:53:28 +02:00
|
|
|
def current_label
|
2022-10-24 16:43:18 +02:00
|
|
|
labels.where.not(name: 'Non attribué').first.name
|
2022-08-11 19:05:12 +02:00
|
|
|
end
|
2022-08-12 12:24:47 +02:00
|
|
|
|
|
|
|
def label_at(date)
|
2022-09-20 15:00:15 +02:00
|
|
|
label = labels.where('designated_on < ?', date)&.first || labels.last
|
|
|
|
label.name
|
2022-08-12 12:24:47 +02:00
|
|
|
end
|
2022-08-12 17:37:30 +02:00
|
|
|
|
|
|
|
def available_at?(date)
|
2022-09-20 14:52:22 +02:00
|
|
|
label_at(date) != 'Non attribué'
|
2022-08-12 17:37:30 +02:00
|
|
|
end
|
2022-08-12 17:54:41 +02:00
|
|
|
|
2023-05-16 09:02:38 +02:00
|
|
|
def self.available_at(date, without_zones = [])
|
|
|
|
(Zone.all - without_zones).filter { |zone| zone.available_at?(date) }.sort_by { |zone| zone.label_at(date) }
|
2022-08-24 15:16:58 +02:00
|
|
|
.map do |zone|
|
|
|
|
OpenStruct.new(id: zone.id, label: zone.label_at(date))
|
|
|
|
end
|
2022-08-12 17:54:41 +02:00
|
|
|
end
|
2023-03-27 15:38:25 +02:00
|
|
|
|
|
|
|
def self.default_for(tchap_hs)
|
|
|
|
sanitized_sql = ActiveRecord::Base.sanitize_sql "'#{tchap_hs}' = ANY (tchap_hs)"
|
|
|
|
Zone.where(sanitized_sql)
|
|
|
|
end
|
2021-12-01 20:00:29 +01:00
|
|
|
end
|