From 82ecc871b8b9409b09fc89f41ff9d65640e6f8ca Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Tue, 30 Jan 2018 14:48:01 +0100 Subject: VehicleJourney: Allow deletion of associated company If the state doesn't contain a company, that means it was deleted on the frontend. In that case, the backend code should correctly delete the associated company from the vehicle journey. Refs #5574 --- app/models/chouette/vehicle_journey.rb | 8 ++++++-- spec/models/chouette/vehicle_journey_spec.rb | 8 ++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/app/models/chouette/vehicle_journey.rb b/app/models/chouette/vehicle_journey.rb index 8a704d8c0..5cd87a66a 100644 --- a/app/models/chouette/vehicle_journey.rb +++ b/app/models/chouette/vehicle_journey.rb @@ -221,9 +221,13 @@ module Chouette def self.state_permited_attributes item attrs = item.slice('published_journey_identifier', 'published_journey_name', 'journey_pattern_id', 'company_id').to_hash - ['company', 'journey_pattern'].map do |association| - attrs["#{association}_id"] = item[association]['id'] if item[association] + + if item['journey_pattern'] + attrs['journey_pattern_id'] = item['journey_pattern']['id'] end + + attrs['company_id'] = item['company'] ? item['company']['id'] : nil + attrs["custom_field_values"] = Hash[*(item["custom_fields"] || {}).map{|k, v| [k, v["value"]]}.flatten] attrs end diff --git a/spec/models/chouette/vehicle_journey_spec.rb b/spec/models/chouette/vehicle_journey_spec.rb index 2a88ac3ce..21bbc1ba8 100644 --- a/spec/models/chouette/vehicle_journey_spec.rb +++ b/spec/models/chouette/vehicle_journey_spec.rb @@ -231,6 +231,14 @@ describe Chouette::VehicleJourney, :type => :model do expect(vehicle_journey.reload.company_id).to eq state['company']['id'] end + it "handles vehicle journey company deletion" do + vehicle_journey.update(company: create(:company)) + state.delete('company') + Chouette::VehicleJourney.state_update(route, collection) + + expect(vehicle_journey.reload.company_id).to be_nil + end + it 'should update vj attributes from state' do state['published_journey_name'] = 'edited_name' state['published_journey_identifier'] = 'edited_identifier' -- cgit v1.2.3