aboutsummaryrefslogtreecommitdiffstats
path: root/app/models/chouette/purchase_window.rb
blob: 8a4a858c43913a6f4c17e991baa31e7608878e22 (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
require 'range_ext'
require_relative '../calendar/period'

module Chouette
  class PurchaseWindow < Chouette::TridentActiveRecord
    # include ChecksumSupport
    include ObjectidSupport
    include PeriodSupport
    include ChecksumSupport
    extend Enumerize

    enumerize :color, in: %w(#9B9B9B #FFA070 #C67300 #7F551B #41CCE3 #09B09C #3655D7 #6321A0 #E796C6 #DD2DAA)

    has_metadata
    belongs_to :referential
    has_and_belongs_to_many :vehicle_journeys, :class_name => 'Chouette::VehicleJourney'

    validates_presence_of :name, :referential

    scope :contains_date, ->(date) { where('date ? <@ any (date_ranges)', date) }
    scope :overlap_dates, ->(date_range) { where('daterange(?, ?) && any (date_ranges)', date_range.first, date_range.last + 1.day) }
    scope :matching_dates, ->(date_range) { where('ARRAY[daterange(?, ?)] = date_ranges', date_range.first, date_range.last + 1.day) }

    # VehicleJourneys include PurchaseWindow checksums in their checksums
    # OPTIMIZEME
    def update_vehicle_journey_checksums
      vehicle_journeys.find_each(&:update_checksum!)
    end
    after_commit :update_vehicle_journey_checksums

    def self.ransackable_scopes(auth_object = nil)
      [:contains_date]
    end

    def self.colors_i18n
      Hash[*color.values.map{|c| [I18n.t("enumerize.purchase_window.color.#{c[1..-1]}"), c]}.flatten]
    end

    def local_id
      "IBOO-#{self.referential.id}-#{self.id}"
    end

    def checksum_attributes
      attrs = ['name', 'color', 'referential_id']
      ranges_attrs = date_ranges.map{|r| [r.min, r.max]}.flatten.sort
      self.slice(*attrs).values + ranges_attrs
    end

    def bounding_dates
      [
        date_ranges.map(&:first).min,
        date_ranges.map(&:max).max,
      ]
    end

    def color
      _color = read_attribute(:color)
      _color.present? ? _color : nil
    end
  end
end