diff options
| author | Robert | 2017-06-21 14:52:46 +0200 |
|---|---|---|
| committer | Robert | 2017-06-22 09:05:19 +0200 |
| commit | d51985fc2a7c2138fd12cb9116ebf05d8b0e7dac (patch) | |
| tree | 678edcf8dc0c5f956e7969a6aaa1e77f35f73940 /app | |
| parent | 400898d14514aaf6df991dd2cb73e10b991ae34b (diff) | |
| download | chouette-core-d51985fc2a7c2138fd12cb9116ebf05d8b0e7dac.tar.bz2 | |
Refs: #3595@3h; fixing tests, evaluating timeliness gem :(
Diffstat (limited to 'app')
| -rw-r--r-- | app/models/calendar.rb | 4 | ||||
| -rw-r--r-- | app/models/calendar/calendar_date.rb | 4 | ||||
| -rw-r--r-- | app/models/calendar/date_value.rb | 4 | ||||
| -rw-r--r-- | app/models/calendar/period.rb | 15 |
4 files changed, 18 insertions, 9 deletions
diff --git a/app/models/calendar.rb b/app/models/calendar.rb index 3307cfe2f..02349b90b 100644 --- a/app/models/calendar.rb +++ b/app/models/calendar.rb @@ -1,8 +1,6 @@ require 'range_ext' class Calendar < ActiveRecord::Base - NullDate = Date.new - belongs_to :organisation has_many :time_tables @@ -70,8 +68,6 @@ class Calendar < ActiveRecord::Base def flatten_date_array attributes, key date_int = %w(1 2 3).map {|e| attributes["#{key}(#{e}i)"].to_i } - ::Date.new(*date_int) - rescue Calendar::CalendarDate.new(*date_int) end diff --git a/app/models/calendar/calendar_date.rb b/app/models/calendar/calendar_date.rb index b7c080db2..7537f562d 100644 --- a/app/models/calendar/calendar_date.rb +++ b/app/models/calendar/calendar_date.rb @@ -20,6 +20,10 @@ class Calendar 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 1bfa34c0e..709dc2c14 100644 --- a/app/models/calendar/date_value.rb +++ b/app/models/calendar/date_value.rb @@ -8,7 +8,7 @@ class Calendar::DateValue validate :validate_date def self.from_date(index, date) - DateValue.new id: index, value: date + new id: index, value: Calendar::CalendarDate.from_date(date) end # Stuff required for coocon @@ -25,7 +25,7 @@ class Calendar::DateValue end def validate_date - errors.add(:value, I18n.t('activerecord.errors.models.calendar.attributes.dates.illegal_date', date: value.to_s)) unless value.legal? + errors.add(:value, I18n.t('activerecord.errors.models.calendar.attributes.dates.illegal_date', date: value.to_s)) unless value.try(:legal?) end attribute :_destroy, type: Boolean diff --git a/app/models/calendar/period.rb b/app/models/calendar/period.rb index 5078f2db3..eb1bb5370 100644 --- a/app/models/calendar/period.rb +++ b/app/models/calendar/period.rb @@ -9,6 +9,12 @@ class Calendar::Period validates_presence_of :begin, :end validate :validate_dates + 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) @@ -16,7 +22,10 @@ class Calendar::Period end def self.from_range(index, range) - Period.new id: index, begin: range.begin, end: range.end + new \ + id: index, + begin: Calendar::CalendarDate.from_date(range.begin), + end: Calendar::CalendarDate.from_date(range.end) end def range @@ -44,11 +53,11 @@ class Calendar::Period 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.legal? + 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.legal? + 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 |
