diff options
| -rw-r--r-- | app/models/chouette/time_table.rb | 22 | ||||
| -rw-r--r-- | spec/models/chouette/time_table_spec.rb | 23 |
2 files changed, 45 insertions, 0 deletions
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 diff --git a/spec/models/chouette/time_table_spec.rb b/spec/models/chouette/time_table_spec.rb index fd9f70740..e45b3a205 100644 --- a/spec/models/chouette/time_table_spec.rb +++ b/spec/models/chouette/time_table_spec.rb @@ -11,6 +11,8 @@ describe Chouette::TimeTable, :type => :model do def time_table_to_state time_table time_table.slice('id', 'comment').tap do |item| item['day_types'] = "Di,Lu,Ma,Me,Je,Ve,Sa" + item['current_month'] = time_table.month_inspect(Date.today.beginning_of_month) + item['current_periode_range'] = Date.today.beginning_of_month.to_s end end @@ -28,6 +30,27 @@ describe Chouette::TimeTable, :type => :model do expect(subject.reload.valid_days).to include(7, 1, 4, 2) expect(subject.reload.valid_days).not_to include(3, 5, 6) end + + it 'should delete date if date is set to neither include or excluded date' do + updated = state['current_month'].map do |day| + day['include_date'] = false if day['include_date'] + end + + expect { + subject.state_update state + }.to change {subject.dates.count}.by(-updated.compact.count) + end + + it 'should update date if date is set to excluded date' do + updated = state['current_month'].map do |day| + next unless day['include_date'] + day['include_date'] = false + day['excluded_date'] = true + end + + subject.state_update state + expect(subject.reload.excluded_days.count).to eq (updated.compact.count) + end end describe "#periods_max_date" do |
