aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorXinhui2017-04-13 16:03:33 +0200
committerXinhui2017-04-13 16:03:38 +0200
commitdc692f9eb2cfe7181e561065c8fea2e4b19d1504 (patch)
treeaead5da3338dfc3c52c759b13232896bca082fb9
parent147cb5733741aff77b0cf191b18d56bc235a6e3b (diff)
downloadchouette-core-dc692f9eb2cfe7181e561065c8fea2e4b19d1504.tar.bz2
Vehicle journeys clear time_tableas association when remove from state
Refs #3121
-rw-r--r--app/models/chouette/vehicle_journey.rb10
-rw-r--r--spec/models/chouette/vehicle_journey_spec.rb11
2 files changed, 15 insertions, 6 deletions
diff --git a/app/models/chouette/vehicle_journey.rb b/app/models/chouette/vehicle_journey.rb
index 27830490f..913fdcd73 100644
--- a/app/models/chouette/vehicle_journey.rb
+++ b/app/models/chouette/vehicle_journey.rb
@@ -92,10 +92,12 @@ module Chouette
end
def update_time_tables_from_state item
- item['time_tables'].each do |tt|
- unless self.time_tables.map(&:id).include?(tt['id'])
- self.time_tables << Chouette::TimeTable.find(tt['id'])
- end
+ state_tt_ids = item['time_tables'].map{|tt| tt['id']}
+ self.time_tables.map(&:id).each do |id|
+ self.time_tables.delete(self.time_tables.find(id)) unless state_tt_ids.include?(id)
+ end
+ state_tt_ids.each do |id|
+ self.time_tables << Chouette::TimeTable.find(id) unless self.time_tables.map(&:id).include?(id)
end
end
diff --git a/spec/models/chouette/vehicle_journey_spec.rb b/spec/models/chouette/vehicle_journey_spec.rb
index 0208ac69b..0de74c0cc 100644
--- a/spec/models/chouette/vehicle_journey_spec.rb
+++ b/spec/models/chouette/vehicle_journey_spec.rb
@@ -83,15 +83,22 @@ describe Chouette::VehicleJourney, :type => :model do
expect(vehicle_journey.reload.journey_pattern_id).to eq state['journey_pattern']['id']
end
- it 'should update vj time_tables association' do
+ it 'should update vj time_tables association from state' do
state['time_tables'] = []
2.times{state['time_tables'] << create(:time_table).attributes.slice('id', 'comment', 'objectid')}
- Chouette::VehicleJourney.state_update(route, collection)
+ vehicle_journey.update_time_tables_from_state(state)
expect(state['errors']).to be_nil
expect(vehicle_journey.reload.time_tables.map(&:id)).to eq(state['time_tables'].map{|tt| tt['id']})
end
+ it 'should clear vj time_tableas association when remove from state' do
+ vehicle_journey.time_tables << create(:time_table)
+ state['time_tables'] = []
+ vehicle_journey.update_time_tables_from_state(state)
+ expect(vehicle_journey.reload.time_tables).to be_empty
+ end
+
it 'should update vj company' do
state['company'] = create(:company).attributes.slice('id', 'name', 'objectid')
Chouette::VehicleJourney.state_update(route, collection)