aboutsummaryrefslogtreecommitdiffstats
path: root/app/models/calendar.rb
blob: 34ed513749b5baf1d82ed8c3cafc289dcf4c5f53 (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
require 'range_ext'
require_relative 'calendar/date_value'
require_relative 'calendar/period'

class Calendar < ActiveRecord::Base
  include DateSupport
  include PeriodSupport

  has_paper_trail
  belongs_to :organisation

  validates_presence_of :name, :short_name, :organisation
  validates_uniqueness_of :short_name

  has_many :time_tables

  scope :contains_date, ->(date) { where('date ? = any (dates) OR date ? <@ any (date_ranges)', date, date) }

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

  def convert_to_time_table
    Chouette::TimeTable.new.tap do |tt|
      self.dates.each do |d|
        tt.dates << Chouette::TimeTableDate.new(date: d, in_out: true)
      end
      self.periods.each do |p|
        tt.periods << Chouette::TimeTablePeriod.new(period_start: p.begin, period_end: p.end)
      end
      tt.int_day_types = 508
    end
  end

end