diff options
| author | Robert | 2017-06-27 14:25:35 +0200 |
|---|---|---|
| committer | Robert | 2017-06-27 14:25:35 +0200 |
| commit | f20df3c08dfec0e3dda68401204f7d49470119a7 (patch) | |
| tree | 4f27a34130d4ff044d48729f16d2dff154047ce5 /app/models/calendar | |
| parent | 970954938043d8d73c4457ee2d91e22c0e422e65 (diff) | |
| parent | eabd56ff9f2c7979192e54b4ae11673f1cc778c1 (diff) | |
| download | chouette-core-f20df3c08dfec0e3dda68401204f7d49470119a7.tar.bz2 | |
conflicts resolved
Diffstat (limited to 'app/models/calendar')
| -rw-r--r-- | app/models/calendar/calendar_date.rb | 29 | ||||
| -rw-r--r-- | app/models/calendar/date_value.rb | 47 | ||||
| -rw-r--r-- | app/models/calendar/period.rb | 108 |
3 files changed, 67 insertions, 117 deletions
diff --git a/app/models/calendar/calendar_date.rb b/app/models/calendar/calendar_date.rb deleted file mode 100644 index cfee95a25..000000000 --- a/app/models/calendar/calendar_date.rb +++ /dev/null @@ -1,29 +0,0 @@ -class Calendar - class CalendarDate < ::Date - - module IllegalDate - attr_reader :year, :month, :day - def to_s(*_args) - "%d-%02d-%02d" % [year, month, day] - end - end - - def self.new(*args) - super(*args) - rescue - o = allocate() - o.instance_exec do - @illegal = true - @year, @month, @day = args - extend IllegalDate - end - o - end - - def self.from_date(date) - new date.year, date.month, date.day - end - - def legal?; !!!@illegal end - end -end diff --git a/app/models/calendar/date_value.rb b/app/models/calendar/date_value.rb index 709dc2c14..a4a405d43 100644 --- a/app/models/calendar/date_value.rb +++ b/app/models/calendar/date_value.rb @@ -1,33 +1,32 @@ -class Calendar::DateValue - include ActiveAttr::Model +class Calendar < ActiveRecord::Base - attribute :id, type: Integer - attribute :value, type: Date + class DateValue + include ActiveAttr::Model - validates_presence_of :value - validate :validate_date - - def self.from_date(index, date) - new id: index, value: Calendar::CalendarDate.from_date(date) - end + attribute :id, type: Integer + attribute :value, type: Date - # Stuff required for coocon - def new_record? - !persisted? - end + validates_presence_of :value - def persisted? - id.present? - end + def self.from_date(index, date) + new id: index, value: date + end - def mark_for_destruction - self._destroy = true - end + # Stuff required for coocon + def new_record? + !persisted? + end + + def persisted? + id.present? + end + + def mark_for_destruction + self._destroy = true + end - def validate_date - errors.add(:value, I18n.t('activerecord.errors.models.calendar.attributes.dates.illegal_date', date: value.to_s)) unless value.try(:legal?) + attribute :_destroy, type: Boolean + alias_method :marked_for_destruction?, :_destroy end - attribute :_destroy, type: Boolean - alias_method :marked_for_destruction?, :_destroy end diff --git a/app/models/calendar/period.rb b/app/models/calendar/period.rb index eb1bb5370..bfde242f3 100644 --- a/app/models/calendar/period.rb +++ b/app/models/calendar/period.rb @@ -1,83 +1,63 @@ -class Calendar::Period - include ActiveAttr::Model +class Calendar < ActiveRecord::Base + + class Period + include ActiveAttr::Model - attribute :id, type: Integer - attribute :begin, type: Date - attribute :end, type: Date + attribute :id, type: Integer + attribute :begin, type: Date + attribute :end, type: Date - validate :check_end_greather_than_begin - validates_presence_of :begin, :end - validate :validate_dates + validates_presence_of :begin, :end + validate :check_end_greather_than_begin - def initialize(args={}) - super - self.begin = Calendar::CalendarDate.from_date(self.begin) if Date === self.begin - self.end = Calendar::CalendarDate.from_date(self.end) if Date === self.end - end - - def check_end_greather_than_begin - if self.begin and self.end and self.begin > self.end - errors.add(:end, :invalid) + def check_end_greather_than_begin + if self.begin and self.end and self.begin > self.end + errors.add(:end, :invalid) + end end - end - def self.from_range(index, range) - new \ - id: index, - begin: Calendar::CalendarDate.from_date(range.begin), - end: Calendar::CalendarDate.from_date(range.end) - end + def self.from_range(range, index) + last = range.exclude_end? ? range.end - 1.day : range.end + Period.new id: index, begin: range.begin, end: last + end - def range - if self.begin and self.end and self.begin <= self.end - Range.new self.begin, self.end + def range + if self.begin and self.end and self.begin <= self.end + Range.new self.begin, self.end + end end - end - def intersect?(*other) - return false if range.nil? + def intersect?(*other) + return false if range.nil? - other = other.flatten - other = other.delete_if { |o| o.id == id } if id + other = other.flatten + other = other.delete_if { |o| o.id == id } if id - other.any? do |period| - if other_range = period.range - (range & other_range).present? + other.any? do |period| + if other_range = period.range + (range & other_range).present? + end end end - end - def validate_dates - validate_begin - validate_end - end - - def validate_begin - errors.add(:begin, I18n.t('activerecord.errors.models.calendar.attributes.dates.illegal_date', date: self.begin.to_s)) unless self.begin.try( :legal? ) - end - - def validate_end - errors.add(:end, I18n.t('activerecord.errors.models.calendar.attributes.dates.illegal_date', date: self.end.to_s)) unless self.end.try( :legal? ) - end + def cover? date + range.cover? date + end - def cover? date - range.cover? date - end + # Stuff required for coocon + def new_record? + !persisted? + end - # Stuff required for coocon - def new_record? - !persisted? - end + def persisted? + id.present? + end - def persisted? - id.present? - end + def mark_for_destruction + self._destroy = true + end - def mark_for_destruction - self._destroy = true + attribute :_destroy, type: Boolean + alias_method :marked_for_destruction?, :_destroy end - - attribute :_destroy, type: Boolean - alias_method :marked_for_destruction?, :_destroy end - |
