aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--app/controllers/time_tables_controller.rb1
-rw-r--r--app/models/chouette/time_table.rb27
-rw-r--r--spec/models/chouette/time_table_spec.rb24
3 files changed, 41 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,
diff --git a/spec/models/chouette/time_table_spec.rb b/spec/models/chouette/time_table_spec.rb
index e45b3a205..1117eecc0 100644
--- a/spec/models/chouette/time_table_spec.rb
+++ b/spec/models/chouette/time_table_spec.rb
@@ -51,6 +51,30 @@ describe Chouette::TimeTable, :type => :model do
subject.state_update state
expect(subject.reload.excluded_days.count).to eq (updated.compact.count)
end
+
+ it 'should create new include date' do
+ day = state['current_month'].first
+ date = Date.parse(day['date'])
+ day['include_date'] = true
+ expect(subject.included_days).not_to include(date)
+
+ expect {
+ subject.state_update state
+ }.to change {subject.dates.count}.by(1)
+ expect(subject.reload.included_days).to include(date)
+ end
+
+ it 'should create new exclude date' do
+ day = state['current_month'].first
+ date = Date.parse(day['date'])
+ day['excluded_date'] = true
+ expect(subject.excluded_days).not_to include(date)
+
+ expect {
+ subject.state_update state
+ }.to change {subject.dates.count}.by(1)
+ expect(subject.reload.excluded_days).to include(date)
+ end
end
describe "#periods_max_date" do