diff options
| author | cedricnjanga | 2017-11-21 15:32:22 +0100 | 
|---|---|---|
| committer | cedricnjanga | 2017-11-22 14:03:51 +0100 | 
| commit | 9304f16e56c219fd63fa12b7d42a7cbf16b2914a (patch) | |
| tree | 0a3d2dddcde40892aa68c8d14e3bd0e2ca7cb24b /app/models/chouette/journey_pattern.rb | |
| parent | 40a5b7409d5214dd8361937c0c9d4726acb53222 (diff) | |
| download | chouette-core-9304f16e56c219fd63fa12b7d42a7cbf16b2914a.tar.bz2 | |
Change the way we name classes
We now always use modules for namespaces
=> same structure for models, decorators, policies...
Diffstat (limited to 'app/models/chouette/journey_pattern.rb')
| -rw-r--r-- | app/models/chouette/journey_pattern.rb | 187 | 
1 files changed, 94 insertions, 93 deletions
| diff --git a/app/models/chouette/journey_pattern.rb b/app/models/chouette/journey_pattern.rb index 31b7c56be..797b6adb1 100644 --- a/app/models/chouette/journey_pattern.rb +++ b/app/models/chouette/journey_pattern.rb @@ -26,124 +26,125 @@ module Chouette        "IBOO-#{self.referential.id}-#{self.route.line.get_objectid.local_id}-#{self.id}"      end -  def checksum_attributes -    values = self.slice(*['name', 'published_name', 'registration_number']).values -    values << self.stop_points.map(&:stop_area).map(&:user_objectid) -    values.flatten -  end +    def checksum_attributes +      values = self.slice(*['name', 'published_name', 'registration_number']).values +      values << self.stop_points.map(&:stop_area).map(&:user_objectid) +      values.flatten +    end -  def self.state_update route, state -    transaction do -      state.each do |item| -        item.delete('errors') -        jp = find_by(objectid: item['object_id']) || state_create_instance(route, item) -        next if item['deletable'] && jp.persisted? && jp.destroy -        # Update attributes and stop_points associations -        jp.update_attributes(state_permited_attributes(item)) unless item['new_record'] -        jp.state_stop_points_update(item) if !jp.errors.any? && jp.persisted? -        item['errors'] = jp.errors if jp.errors.any? +    def self.state_update route, state +      transaction do +        state.each do |item| +          item.delete('errors') +          jp = find_by(objectid: item['object_id']) || state_create_instance(route, item) +          next if item['deletable'] && jp.persisted? && jp.destroy +          # Update attributes and stop_points associations +          jp.update_attributes(state_permited_attributes(item)) unless item['new_record'] +          jp.state_stop_points_update(item) if !jp.errors.any? && jp.persisted? +          item['errors'] = jp.errors if jp.errors.any? +        end + +        if state.any? {|item| item['errors']} +          state.map {|item| item.delete('object_id') if item['new_record']} +          raise ::ActiveRecord::Rollback +        end        end -      if state.any? {|item| item['errors']} -        state.map {|item| item.delete('object_id') if item['new_record']} -        raise ActiveRecord::Rollback -      end +      state.map {|item| item.delete('new_record')} +      state.delete_if {|item| item['deletable']}      end -    state.map {|item| item.delete('new_record')} -    state.delete_if {|item| item['deletable']} -  end - -  def self.state_permited_attributes item -    { -      name: item['name'], -      published_name: item['published_name'], -      registration_number: item['registration_number'] -    } -  end - -  def self.state_create_instance route, item -    # Flag new record, so we can unset object_id if transaction rollback -    jp = route.journey_patterns.create(state_permited_attributes(item)) +    def self.state_permited_attributes item +      { +        name: item['name'], +        published_name: item['published_name'], +        registration_number: item['registration_number'] +      } +    end -    # FIXME -    # DefaultAttributesSupport will trigger some weird validation on after save -    # wich will call to valid?, wich will populate errors -    # In this case, we mark jp to be valid if persisted? return true -    jp.errors.clear if jp.persisted? +    def self.state_create_instance route, 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 -  end +      # FIXME +      # DefaultAttributesSupport will trigger some weird validation on after save +      # wich will call to valid?, wich will populate errors +      # In this case, we mark jp to be valid if persisted? return true +      jp.errors.clear if jp.persisted? -  def state_stop_points_update item -    item['stop_points'].each do |sp| -      exist = stop_area_ids.include?(sp['id']) -      next if exist && sp['checked'] +      item['object_id']  = jp.objectid +      item['new_record'] = true +      jp +    end -      stop_point = route.stop_points.find_by(stop_area_id: sp['id']) -      if !exist && sp['checked'] -        stop_points << stop_point -      end -      if exist && !sp['checked'] -        stop_points.delete(stop_point) +    def state_stop_points_update item +      item['stop_points'].each do |sp| +        exist = stop_area_ids.include?(sp['id']) +        next if exist && sp['checked'] + +        stop_point = route.stop_points.find_by(stop_area_id: sp['id']) +        if !exist && sp['checked'] +          stop_points << stop_point +        end +        if exist && !sp['checked'] +          stop_points.delete(stop_point) +        end        end      end -  end -  # TODO: this a workarround -  # otherwise, we loose the first stop_point -  # when creating a new journey_pattern -  def special_update -    bck_sp = self.stop_points.map {|s| s} -    self.update_attributes :stop_points => [] -    self.update_attributes :stop_points => bck_sp -  end +    # TODO: this a workarround +    # otherwise, we loose the first stop_point +    # when creating a new journey_pattern +    def special_update +      bck_sp = self.stop_points.map {|s| s} +      self.update_attributes :stop_points => [] +      self.update_attributes :stop_points => bck_sp +    end -  def departure_stop_point -    return unless departure_stop_point_id -    Chouette::StopPoint.find( departure_stop_point_id) -  end +    def departure_stop_point +      return unless departure_stop_point_id +      Chouette::StopPoint.find( departure_stop_point_id) +    end -  def arrival_stop_point -    return unless arrival_stop_point_id -    Chouette::StopPoint.find( arrival_stop_point_id) -  end +    def arrival_stop_point +      return unless arrival_stop_point_id +      Chouette::StopPoint.find( arrival_stop_point_id) +    end -  def shortcuts_update_for_add( stop_point) -    stop_points << stop_point unless stop_points.include?( stop_point) +    def shortcuts_update_for_add( stop_point) +      stop_points << stop_point unless stop_points.include?( stop_point) -    ordered_stop_points = stop_points -    ordered_stop_points = ordered_stop_points.sort { |a,b| a.position <=> b.position} unless ordered_stop_points.empty? +      ordered_stop_points = stop_points +      ordered_stop_points = ordered_stop_points.sort { |a,b| a.position <=> b.position} unless ordered_stop_points.empty? -    self.update_attributes( :departure_stop_point_id => (ordered_stop_points.first && ordered_stop_points.first.id), -                            :arrival_stop_point_id => (ordered_stop_points.last && ordered_stop_points.last.id)) -  end +      self.update_attributes( :departure_stop_point_id => (ordered_stop_points.first && ordered_stop_points.first.id), +                              :arrival_stop_point_id => (ordered_stop_points.last && ordered_stop_points.last.id)) +    end -  def shortcuts_update_for_remove( stop_point) -    stop_points.delete( stop_point) if stop_points.include?( stop_point) +    def shortcuts_update_for_remove( stop_point) +      stop_points.delete( stop_point) if stop_points.include?( stop_point) -    ordered_stop_points = stop_points -    ordered_stop_points = ordered_stop_points.sort { |a,b| a.position <=> b.position} unless ordered_stop_points.empty? +      ordered_stop_points = stop_points +      ordered_stop_points = ordered_stop_points.sort { |a,b| a.position <=> b.position} unless ordered_stop_points.empty? -    self.update_attributes( :departure_stop_point_id => (ordered_stop_points.first && ordered_stop_points.first.id), -                            :arrival_stop_point_id => (ordered_stop_points.last && ordered_stop_points.last.id)) -  end +      self.update_attributes( :departure_stop_point_id => (ordered_stop_points.first && ordered_stop_points.first.id), +                              :arrival_stop_point_id => (ordered_stop_points.last && ordered_stop_points.last.id)) +    end -  def vjas_add( stop_point) -    return if new_record? +    def vjas_add( stop_point) +      return if new_record? -    vehicle_journeys.each do |vj| -      vjas = vj.vehicle_journey_at_stops.create :stop_point_id => stop_point.id +      vehicle_journeys.each do |vj| +        vjas = vj.vehicle_journey_at_stops.create :stop_point_id => stop_point.id +      end      end -  end -  def vjas_remove( stop_point) -    return if new_record? +    def vjas_remove( stop_point) +      return if new_record? -    vehicle_journey_at_stops.where( :stop_point_id => stop_point.id).each do |vjas| -      vjas.destroy +      vehicle_journey_at_stops.where( :stop_point_id => stop_point.id).each do |vjas| +        vjas.destroy +      end      end    end  end | 
