diff options
| author | Xinhui | 2017-06-28 12:28:41 +0200 |
|---|---|---|
| committer | Xinhui | 2017-06-28 12:35:34 +0200 |
| commit | e954f7994d692919492c40b7bce4cce1e0293755 (patch) | |
| tree | 519730ddb78bdc8ffcfbd4cb81b8ad329de0610d /app/models/chouette/time_table.rb | |
| parent | b112d6e5446ae1677e9afe527a5fefd30620b14c (diff) | |
| download | chouette-core-e954f7994d692919492c40b7bce4cce1e0293755.tar.bz2 | |
Refactoring time_table#merge
Refs #3788
Diffstat (limited to 'app/models/chouette/time_table.rb')
| -rw-r--r-- | app/models/chouette/time_table.rb | 63 |
1 files changed, 11 insertions, 52 deletions
diff --git a/app/models/chouette/time_table.rb b/app/models/chouette/time_table.rb index 4186af6d2..c566452f4 100644 --- a/app/models/chouette/time_table.rb +++ b/app/models/chouette/time_table.rb @@ -477,63 +477,22 @@ class Chouette::TimeTable < Chouette::TridentActiveRecord # merge effective days from another timetable def merge!(another_tt) transaction do - # merge dates - self.dates ||= [] - another_tt.included_days.each do |d| - add_included_day d - end + self.periods = another_tt.clone_periods + self.periods + self.periods = self.optimize_periods - # if one tt has no period, just merge lists - if self.periods.empty? || another_tt.periods.empty? - if !another_tt.periods.empty? - # copy periods - self.periods = another_tt.clone_periods - end - else - # check if periods can be kept - common_day_types = self.int_day_types & another_tt.int_day_types & 508 - # if common day types : merge periods - if common_day_types != 0 - periods = self.optimize_periods - another_periods = another_tt.optimize_periods - # add not common days of both periods as peculiar days - self.effective_days_of_periods(self.class.valid_days(self.int_day_types ^ common_day_types)).each do |d| - self.dates |= [Chouette::TimeTableDate.new(:date => d, :in_out => true)] - end - another_tt.effective_days_of_periods(self.class.valid_days(another_tt.int_day_types ^ common_day_types)).each do |d| - add_included_day d - end - # merge periods - self.periods = periods | another_periods - self.int_day_types = common_day_types - self.periods = self.optimize_periods - else - # convert all period in days - self.effective_days_of_periods.each do |d| - self.dates << Chouette::TimeTableDate.new(:date => d, :in_out => true) unless self.include_in_dates?(d) - end - another_tt.effective_days_of_periods.each do |d| - add_included_day d + # For included dates + another_tt.included_days.map{ |d| add_included_day(d) } + + # For excluded dates + existing_out_date = self.dates.where(in_out: false).map(&:date) + another_tt.dates.where(in_out: false).each do |d| + unless existing_out_date.include?(d.date) + self.dates << Chouette::TimeTableDate.new(:date => d.date, :in_out => false) end end - end - # if remained excluded dates are valid in other tt , remove it from result - self.dates.each do |date| - date.in_out = true if date.in_out == false && another_tt.include_day?(date.date) + self.save! end - # if peculiar dates are valid in new periods, remove them - if !self.periods.empty? - days_in_period = self.effective_days_of_periods - dates = [] - self.dates.each do |date| - dates << date unless date.in_out && days_in_period.include?(date.date) - end - self.dates = dates - end - self.dates.to_a.sort! { |a,b| a.date <=> b.date} - self.save! - end self.convert_continuous_dates_to_periods end |
