From 2c8690d30b851a48bc2ce797f4ac44c7bfedc024 Mon Sep 17 00:00:00 2001 From: Xinhui Date: Fri, 17 Mar 2017 11:30:19 +0100 Subject: Wip vj save new vj from state && update vj journey_pattern Ref #2777 --- app/models/chouette/journey_pattern.rb | 8 +++----- app/models/chouette/vehicle_journey.rb | 29 ++++++++++++++++------------- 2 files changed, 19 insertions(+), 18 deletions(-) (limited to 'app/models') diff --git a/app/models/chouette/journey_pattern.rb b/app/models/chouette/journey_pattern.rb index c57d42e71..34b0d9345 100644 --- a/app/models/chouette/journey_pattern.rb +++ b/app/models/chouette/journey_pattern.rb @@ -25,9 +25,7 @@ class Chouette::JourneyPattern < Chouette::TridentActiveRecord state.each do |item| item.delete('errors') jp = find_by(objectid: item['object_id']) || state_create_instance(route, item) - if item['deletable'] && jp.persisted? - next if jp.destroy - end + next if item['deletable'] && jp.persisted? && jp.destroy # Update attributes and stop_points associations jp.update_attributes(state_permited_attributes(item)) @@ -40,7 +38,7 @@ class Chouette::JourneyPattern < Chouette::TridentActiveRecord raise ActiveRecord::Rollback end end - # clean + state.map {|item| item.delete('new_record')} state.delete_if {|item| item['deletable']} end @@ -54,8 +52,8 @@ class Chouette::JourneyPattern < Chouette::TridentActiveRecord end def self.state_create_instance route, item - jp = route.journey_patterns.create(state_permited_attributes(item)) # Flag new record, so we can unset object_id if transaction rollback + jp = route.journey_patterns.create(state_permited_attributes(item)) item['object_id'] = jp.objectid item['new_record'] = true jp diff --git a/app/models/chouette/vehicle_journey.rb b/app/models/chouette/vehicle_journey.rb index f22ddd390..0aab9d0ed 100644 --- a/app/models/chouette/vehicle_journey.rb +++ b/app/models/chouette/vehicle_journey.rb @@ -74,38 +74,41 @@ module Chouette end end - def update_company_from_state state - if state['company']['id'] != state['company_id'] - self.company = Company.find(state['company']['id']) - state['company_id'] = self.company.id - self.save - end - end - def self.state_update route, state transaction do state.each do |item| item.delete('errors') - vj = find_by(objectid: item['objectid']) + vj = find_by(objectid: item['objectid']) || state_create_instance(route, item) next if item['deletable'] && vj.persisted? && vj.destroy vj.update_vjas_from_state(item['vehicle_journey_at_stops']) vj.update_attributes(state_permited_attributes(item)) - vj.update_company_from_state(item) if item['company'] - item['errors'] = vj.errors if vj.errors.any? end if state.any? {|item| item['errors']} + state.map {|item| item.delete('objectid') if item['new_record']} raise ::ActiveRecord::Rollback end end - # Cleanup + state.map {|item| item.delete('new_record')} state.delete_if {|item| item['deletable']} end + def self.state_create_instance route, item + # Flag new record, so we can unset object_id if transaction rollback + vj = route.vehicle_journeys.create(state_permited_attributes(item)) + item['objectid'] = vj.objectid + item['new_record'] = true + vj + end + def self.state_permited_attributes item - item.slice('published_journey_identifier', 'published_journey_name').to_hash + 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] + end + attrs end def increasing_times -- cgit v1.2.3