aboutsummaryrefslogtreecommitdiffstats
path: root/app/models/calendar.rb
diff options
context:
space:
mode:
Diffstat (limited to 'app/models/calendar.rb')
-rw-r--r--app/models/calendar.rb83
1 files changed, 82 insertions, 1 deletions
diff --git a/app/models/calendar.rb b/app/models/calendar.rb
index a7fd9220c..561a2e3f7 100644
--- a/app/models/calendar.rb
+++ b/app/models/calendar.rb
@@ -5,6 +5,8 @@ require_relative 'calendar/period'
class Calendar < ActiveRecord::Base
include DateSupport
include PeriodSupport
+ include ApplicationDaysSupport
+ include TimetableSupport
has_paper_trail class_name: 'PublicVersion'
belongs_to :organisation
@@ -16,10 +18,28 @@ class Calendar < ActiveRecord::Base
scope :contains_date, ->(date) { where('date ? = any (dates) OR date ? <@ any (date_ranges)', date, date) }
+ after_initialize :set_defaults
+
def self.ransackable_scopes(auth_object = nil)
[:contains_date]
end
+ def self.state_permited_attributes item
+ {name: item["comment"]}
+ end
+
+ def set_defaults
+ self.excluded_dates ||= []
+ self.int_day_types ||= EVERYDAY
+ end
+
+ def human_attribute_name(*args)
+ self.class.human_attribute_name(*args)
+ end
+
+ def shortcuts_update(date=nil)
+ end
+
def convert_to_time_table
Chouette::TimeTable.new.tap do |tt|
self.dates.each do |d|
@@ -28,8 +48,69 @@ class Calendar < ActiveRecord::Base
self.periods.each do |p|
tt.periods << Chouette::TimeTablePeriod.new(period_start: p.begin, period_end: p.end)
end
- tt.int_day_types = 508
+ tt.int_day_types = self.int_day_types
+ end
+ end
+
+ def include_in_dates?(day)
+ self.dates.include? day
+ end
+
+ def excluded_date?(day)
+ self.excluded_dates.include? day
+ end
+
+ def update_in_out date, in_out
+ if in_out
+ self.excluded_dates.delete date
+ self.dates << date unless include_in_dates?(date)
+ else
+ self.dates.delete date
+ self.excluded_dates << date unless excluded_date?(date)
+ end
+ date
+ end
+
+ def included_days
+ dates
+ end
+
+ def excluded_days
+ excluded_dates
+ end
+
+ def saved_dates
+ Hash[*self.dates.each_with_index.to_a.map(&:reverse).flatten]
+ end
+
+ def all_dates
+ (dates + excluded_dates).sort.each_with_index.map do |d, i|
+ OpenStruct.new(id: i, date: d, in_out: include_in_dates?(d))
end
end
+ def find_date_by_id id
+ self.dates[id]
+ end
+
+ def destroy_date date
+ self.dates -= [date]
+ end
+
+ def create_date in_out:, date:
+ update_in_out date, in_out
+ end
+
+ def find_period_by_id id
+ self.periods.find{|p| p.id == id}
+ end
+
+ def build_period
+ self.periods << Calendar::Period.new(id: self.periods.count + 1)
+ self.periods.last
+ end
+
+ def destroy_period period
+ @periods = self.periods.select{|p| p.end != period.end || p.begin != period.begin}
+ end
end