aboutsummaryrefslogtreecommitdiffstats
path: root/app
diff options
context:
space:
mode:
authorXinhui2017-04-27 15:14:05 +0200
committerXinhui2017-04-27 15:14:16 +0200
commit5cf914aec14e39507a3d7c05834fbd945340be92 (patch)
tree9db472edccaa93035cc4feedd0d777cd2ab75ebd /app
parentcaab8418c994194d52f2685514644cddd177083e (diff)
downloadchouette-core-5cf914aec14e39507a3d7c05834fbd945340be92.tar.bz2
Wip TimeTables#update save TimeTableDate
Refs #2899
Diffstat (limited to 'app')
-rw-r--r--app/controllers/time_tables_controller.rb1
-rw-r--r--app/models/chouette/time_table.rb27
2 files changed, 17 insertions, 11 deletions
diff --git a/app/controllers/time_tables_controller.rb b/app/controllers/time_tables_controller.rb
index 487c14e9f..ea53df35f 100644
--- a/app/controllers/time_tables_controller.rb
+++ b/app/controllers/time_tables_controller.rb
@@ -58,6 +58,7 @@ class TimeTablesController < ChouetteController
def update
state = JSON.parse request.raw_post
+ resource.state_update state
respond_to do |format|
format.json { render json: state, status: state['errors'] ? :unprocessable_entity : :ok }
end
diff --git a/app/models/chouette/time_table.rb b/app/models/chouette/time_table.rb
index 0b45341d6..319abfa47 100644
--- a/app/models/chouette/time_table.rb
+++ b/app/models/chouette/time_table.rb
@@ -45,23 +45,27 @@ class Chouette::TimeTable < Chouette::TridentActiveRecord
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
+ date = Date.parse(d['date'])
+ checked = d['include_date'] || d['excluded_date']
+ in_out = d['include_date'] ? true : false
+
+ date_id = saved_dates.key(date)
+ time_table_date = self.dates.find(date_id) if date_id
+
+ next if !checked && !time_table_date
+
+ # Destroy date if no longer checked
+ next if !checked && time_table_date.destroy
+
+ # Create new date
+ unless time_table_date
+ time_table_date = self.dates.create({in_out: in_out, date: date})
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
@@ -84,6 +88,7 @@ class Chouette::TimeTable < Chouette::TridentActiveRecord
(date.beginning_of_month..date.end_of_month).map do |d|
{
day: I18n.l(d, format: '%A'),
+ date: d.to_s,
wday: d.wday,
wnumber: d.strftime("%W").to_s,
mday: d.mday,