diff options
| author | Xinhui | 2017-03-17 11:30:19 +0100 | 
|---|---|---|
| committer | Xinhui | 2017-03-17 11:30:51 +0100 | 
| commit | 2c8690d30b851a48bc2ce797f4ac44c7bfedc024 (patch) | |
| tree | 7e7b0dbf17aacb6e9381d7e54c4d491e910918d3 /app | |
| parent | 085c1271814a01a87c8f3367e45c200766dc8738 (diff) | |
| download | chouette-core-2c8690d30b851a48bc2ce797f4ac44c7bfedc024.tar.bz2 | |
Wip vj save new vj from state && update vj journey_pattern
Ref #2777
Diffstat (limited to 'app')
| -rw-r--r-- | app/models/chouette/journey_pattern.rb | 8 | ||||
| -rw-r--r-- | app/models/chouette/vehicle_journey.rb | 29 | ||||
| -rw-r--r-- | app/views/vehicle_journeys/show.rabl | 2 | 
3 files changed, 20 insertions, 19 deletions
| 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 diff --git a/app/views/vehicle_journeys/show.rabl b/app/views/vehicle_journeys/show.rabl index 398bb3b16..86edfafa8 100644 --- a/app/views/vehicle_journeys/show.rabl +++ b/app/views/vehicle_journeys/show.rabl @@ -1,6 +1,6 @@  object @vehicle_journey -[:objectid, :published_journey_name, :published_journey_identifier].each do |attr| +[:objectid, :published_journey_name, :published_journey_identifier, :company_id].each do |attr|    attributes attr, :unless => lambda { |m| m.send( attr).nil?}  end | 
