From ea4e77062126a22ad4032f1fd85f79caedaf4bbf Mon Sep 17 00:00:00 2001 From: Xinhui Date: Thu, 27 Apr 2017 12:40:15 +0200 Subject: Wip TimeTables#update update dates associations Refs #2899 --- app/models/chouette/time_table.rb | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'app') diff --git a/app/models/chouette/time_table.rb b/app/models/chouette/time_table.rb index acce96681..0b45341d6 100644 --- a/app/models/chouette/time_table.rb +++ b/app/models/chouette/time_table.rb @@ -40,6 +40,28 @@ class Chouette::TimeTable < Chouette::TridentActiveRecord prefix = human_attribute_name(name).first(2) send("#{name}=", days.include?(prefix)) end + + saved_dates = Hash[self.dates.collect{ |d| [d.id, d.date]}] + cmonth = Date.parse(state['current_periode_range']) + + state['current_month'].each do |d| + date = Date.parse("#{d['mday']}-#{cmonth.strftime("%B-%Y")}") + id = saved_dates.key(date) + next unless id + + time_table_date = self.dates.find(id) + # Destroy removed date + unless d['include_date'] || d['excluded_date'] + next if time_table_date.destroy + end + # Update in_out + in_out = d['include_date'] ? true : false + if in_out != time_table_date.in_out + time_table_date.update_attributes({in_out: in_out}) + end + + end + self.save end -- cgit v1.2.3