diff options
| author | cedricnjanga | 2017-07-31 10:24:56 +0200 |
|---|---|---|
| committer | cedricnjanga | 2017-07-31 11:09:51 +0200 |
| commit | bba83cb56a56d2f26e9c380b22fc542228d2eabc (patch) | |
| tree | e77d3ce0ae356769294e7f12e43176982723e2b7 | |
| parent | 8d3e2d9dcbc61ce17053f5b66b742189f885ba06 (diff) | |
| download | chouette-core-bba83cb56a56d2f26e9c380b22fc542228d2eabc.tar.bz2 | |
Delete Timetable unnacessary methods and update specs
| -rw-r--r-- | app/models/chouette/time_table.rb | 63 | ||||
| -rw-r--r-- | spec/models/chouette/time_table_spec.rb | 13 | ||||
| -rw-r--r-- | spec/models/time_table_combination_spec.rb | 54 |
3 files changed, 11 insertions, 119 deletions
diff --git a/app/models/chouette/time_table.rb b/app/models/chouette/time_table.rb index e3c003b91..1753cbed5 100644 --- a/app/models/chouette/time_table.rb +++ b/app/models/chouette/time_table.rb @@ -461,59 +461,6 @@ class Chouette::TimeTable < Chouette::TridentActiveRecord optimized.sort { |a,b| a.period_start <=> b.period_start} end - def continuous_periods - periods = self.periods.sort_by(&:period_start) - chunk = {} - group = nil - periods.each_with_index do |period, index| - group ||= index - group = (period.period_start - 1.day == periods[index - 1].period_end) ? group : group + 1 - chunk[group] ||= [] - chunk[group] << period - end - chunk.values.delete_if {|periods| periods.count < 2} - end - - def convert_continuous_periods_into_one - chunks = self.continuous_periods - - transaction do - chunks.each do |chunk| - self.periods.create!(period_start: chunk.first.period_start, period_end: chunk.last.period_end) - self.periods.delete chunk - end - end - end - - #update a period if a in_day is just before or after - def optimize_continuous_dates_and_periods - return self.periods if self.included_days.empty? || periods.empty? - - periods = self.clone_periods - optimized = [] - - i = 0 - while i < periods.length - period = periods[i] - j = 0 - in_days = self.reload.dates.where(in_out: true).sort_by(&:date) - while j < in_days.length - day = in_days[j] - if period.period_start - 1.day === day.date - period.period_start = day.date - self.dates.delete day - elsif period.period_end + 1.day === day.date - period.period_end = day.date - self.dates.delete day - end - j += 1 - end - i += 1 - optimized << period - end - optimized - end - # add a peculiar day or switch it from excluded to included def add_included_day(d) if self.excluded_date?(d) @@ -530,7 +477,7 @@ class Chouette::TimeTable < Chouette::TridentActiveRecord # merge effective days from another timetable def merge!(another_tt) transaction do - days = Array.new.tap do |array| + days = [].tap do |array| array.push(*self.included_days_in_dates_and_periods, *another_tt.effective_days) array.uniq! end @@ -556,10 +503,10 @@ class Chouette::TimeTable < Chouette::TridentActiveRecord days end - # remove dates form tt which aren't in another_tt + # keep common dates with another_tt def intersect!(another_tt) transaction do - days = Array.new.tap do |array| + days = [].tap do |array| array.push(*self.included_days_in_dates_and_periods) array.delete_if {|day| !another_tt.effective_days.include?(day) } array.uniq! @@ -576,10 +523,10 @@ class Chouette::TimeTable < Chouette::TridentActiveRecord self.convert_continuous_dates_to_periods end - # remove days from another calendar + # remove common dates with another_tt def disjoin!(another_tt) transaction do - days = Array.new.tap do |array| + days = [].tap do |array| array.push(*self.included_days_in_dates_and_periods) array.delete_if {|day| another_tt.effective_days.include?(day) } array.uniq! diff --git a/spec/models/chouette/time_table_spec.rb b/spec/models/chouette/time_table_spec.rb index 304cb0184..f13e13d52 100644 --- a/spec/models/chouette/time_table_spec.rb +++ b/spec/models/chouette/time_table_spec.rb @@ -52,20 +52,19 @@ describe Chouette::TimeTable, :type => :model do expect(subject.int_day_types).to eq int_day_types end - it 'should merge date in_out false' do + it 'should not merge date in_out false' do another_tt.dates.last.in_out = false another_tt.save subject.merge!(another_tt) - expect(subject.dates.map(&:date)).to include(another_tt.dates.last.date) + expect(subject.dates.map(&:date)).not_to include(another_tt.dates.last.date) end - it 'should remove date in_out false if other tt doesnt have them' do + it 'should remove all date in_out false' do subject.dates.create(in_out: false, date: Date.today + 5.day + 1.year) - - expect { - subject.merge!(another_tt) - }.to change {subject.reload.excluded_days.count}.by(-1) + another_tt.dates.last.in_out = false + subject.merge!(another_tt) + expect(subject.reload.excluded_days.count).to eq(0) end end diff --git a/spec/models/time_table_combination_spec.rb b/spec/models/time_table_combination_spec.rb index ee934f50d..81f9dd7a6 100644 --- a/spec/models/time_table_combination_spec.rb +++ b/spec/models/time_table_combination_spec.rb @@ -44,60 +44,6 @@ describe TimeTableCombination, :type => :model do end end - describe '#continuous_periods' do - it 'should group continuous periods' do - source.periods.clear - - start_date = Date.today + 1.year - end_date = start_date + 10 - - # 6 more continuous dates, 2 isolated dates - 0.upto(4) do |i| - source.periods.create(period_start: start_date, period_end: end_date) - start_date = end_date + 1 - end_date = start_date + 10 - end - - expect(source.reload.continuous_periods.flatten.count).to eq(5) - end - end - - describe '#convert_continuous_periods_into_one' do - it 'should convert continuous periods into one' do - source.periods.clear - - start_date = Date.today + 1.year - end_date = start_date + 10 - - # 6 more continuous dates, 2 isolated dates - 0.upto(4) do |i| - source.periods.create(period_start: start_date, period_end: end_date) - start_date = end_date + 1 - end_date = start_date + 10 - end - - expect { - source.reload.convert_continuous_periods_into_one - }.to change {source.periods.count}.by(-4) - end - end - - describe '#optimize_continuous_dates_and_periods' do - it 'should update period if timetable has in_date just before or after ' do - source.dates.clear - source.periods.clear - - source.periods.create(period_start: Date.today, period_end: Date.today + 10.day) - source.dates.create(date: Date.today - 1.day, in_out: true) - - expect { - source.periods = source.optimize_continuous_dates_and_periods - }.to change {source.dates.count}.by(-1) - - expect(source.reload.periods.first.period_start).to eq(Date.today - 1.day) - end - end - describe "#combine" do context "when operation is union" do before(:each) do |
