diff options
| author | Xinhui | 2017-05-15 11:32:56 +0200 |
|---|---|---|
| committer | Xinhui | 2017-05-15 11:50:25 +0200 |
| commit | 2c32ac2a3372dfaee531734d3cf94f6b522b7c1b (patch) | |
| tree | c11ebe9672e36d607e562788b555460ebcadeb39 | |
| parent | 8cfb32a40ae3f2c48c6a7884a1a8251ee4a48254 (diff) | |
| download | chouette-core-2c32ac2a3372dfaee531734d3cf94f6b522b7c1b.tar.bz2 | |
Calendar #convert_to_time_tablet
Refs #3372
| -rw-r--r-- | app/models/calendar.rb | 11 | ||||
| -rw-r--r-- | spec/models/calendar_spec.rb | 16 |
2 files changed, 23 insertions, 4 deletions
diff --git a/app/models/calendar.rb b/app/models/calendar.rb index 91a17e853..39e0d6d49 100644 --- a/app/models/calendar.rb +++ b/app/models/calendar.rb @@ -19,6 +19,17 @@ class Calendar < ActiveRecord::Base [: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.date_ranges.each do |p| + tt.periods << Chouette::TimeTablePeriod.new(period_start: p.begin, period_end: p.end) + end + end + end + class Period include ActiveAttr::Model diff --git a/spec/models/calendar_spec.rb b/spec/models/calendar_spec.rb index 36981961f..33d9676cd 100644 --- a/spec/models/calendar_spec.rb +++ b/spec/models/calendar_spec.rb @@ -9,6 +9,18 @@ RSpec.describe Calendar, :type => :model do it { is_expected.to validate_presence_of(:short_name) } it { is_expected.to validate_uniqueness_of(:short_name) } + describe '#to_time_table' do + let(:calendar) { create(:calendar, date_ranges: [Date.today..(Date.today + 1.month)]) } + + it 'should convert calendar to an instance of Chouette::TimeTable' do + time_table = calendar.convert_to_time_table + expect(time_table).to be_an_instance_of(Chouette::TimeTable) + expect(time_table.periods[0].period_start).to eq(calendar.date_ranges[0].begin) + expect(time_table.periods[0].period_end).to eq(calendar.date_ranges[0].end) + expect(time_table.dates.map(&:date)).to match_array(calendar.dates) + end + end + describe 'validations' do it 'validates that dates and date_ranges do not overlap' do calendar = build(:calendar, dates: [Date.today], date_ranges: [Date.today..Date.tomorrow]) @@ -69,7 +81,6 @@ RSpec.describe Calendar, :type => :model do end describe 'intersect?' do - it 'should detect date in common with other date_ranges' do november = period(begin: '2016-11-01', end: '2016-11-30') mid_november_mid_december = period(begin: '2016-11-15', end: '2016-12-15') @@ -111,7 +122,6 @@ RSpec.describe Calendar, :type => :model do end describe 'DateValue' do - subject { date_value } def date_value(attributes = {}) @@ -141,8 +151,6 @@ RSpec.describe Calendar, :type => :model do end it { is_expected.to validate_presence_of(:value) } - end - end |
