diff options
| author | Xinhui | 2017-12-27 14:19:16 +0100 | 
|---|---|---|
| committer | Xinhui | 2017-12-27 14:37:30 +0100 | 
| commit | 4bc32eb584ed669a8859b4897d020781d2d4ff43 (patch) | |
| tree | 796245f694c0cd6c0187297f2a1818ab864b467b | |
| parent | 1e9b030411b11c1027695196b878b69d8301ddc8 (diff) | |
| download | chouette-core-4bc32eb584ed669a8859b4897d020781d2d4ff43.tar.bz2 | |
Add validation on stop_area parent_id area_type
Refs #5313
| -rw-r--r-- | app/assets/javascripts/select2.coffee | 4 | ||||
| -rw-r--r-- | app/models/chouette/area_type.rb | 5 | ||||
| -rw-r--r-- | app/models/chouette/stop_area.rb | 8 | ||||
| -rw-r--r-- | app/views/stop_areas/_form.html.slim | 2 | ||||
| -rw-r--r-- | config/locales/stop_areas.en.yml | 1 | ||||
| -rw-r--r-- | config/locales/stop_areas.fr.yml | 1 | 
6 files changed, 18 insertions, 3 deletions
| diff --git a/app/assets/javascripts/select2.coffee b/app/assets/javascripts/select2.coffee index 8e3c3cbdd..4cf5f42d0 100644 --- a/app/assets/javascripts/select2.coffee +++ b/app/assets/javascripts/select2.coffee @@ -28,8 +28,8 @@ bind_select2_ajax = (el, cfg = {}) ->      escapeMarkup: (markup) ->        markup      initSelection : (item, callback) -> -      if _this.data('saved') -        callback(_this.data('saved')) +      if _this.data('initvalue') +        callback(_this.data('initvalue'))    bind_select2(el, cfg) diff --git a/app/models/chouette/area_type.rb b/app/models/chouette/area_type.rb index f295fe7ae..30a813fd2 100644 --- a/app/models/chouette/area_type.rb +++ b/app/models/chouette/area_type.rb @@ -1,4 +1,5 @@  class Chouette::AreaType +  include Comparable    ALL = %i(zdep zder zdlp zdlr lda).freeze @@ -30,6 +31,10 @@ class Chouette::AreaType      @code = code    end +  def <=>(other) +    all.index(code) <=> all.index(other.code) +  end +    def label      I18n.translate code, scope: 'area_types.label'    end diff --git a/app/models/chouette/stop_area.rb b/app/models/chouette/stop_area.rb index 1dfdac724..08a5d0826 100644 --- a/app/models/chouette/stop_area.rb +++ b/app/models/chouette/stop_area.rb @@ -40,12 +40,20 @@ module Chouette      validates_format_of :url, :with => %r{\Ahttps?:\/\/([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?\Z}, :allow_nil => true, :allow_blank => true      validates_numericality_of :waiting_time, greater_than_or_equal_to: 0, only_integer: true, if: :waiting_time +    validate :parent_area_type_must_be_greater      def self.nullable_attributes        [:registration_number, :street_name, :country_code, :fare_code,        :nearest_topic_name, :comment, :long_lat_type, :zip_code, :city_name, :url, :time_zone]      end +    def parent_area_type_must_be_greater +      return unless self.parent +      if Chouette::AreaType.find(self.area_type) > Chouette::AreaType.find(self.parent.area_type) +        errors.add(:parent_id, I18n.t('stop_areas.errors.parent_area_type', area_type: self.parent.area_type)) +      end +    end +      after_update :clean_invalid_access_links      before_save :coordinates_to_lat_lng diff --git a/app/views/stop_areas/_form.html.slim b/app/views/stop_areas/_form.html.slim index 078df5daf..399f57b3b 100644 --- a/app/views/stop_areas/_form.html.slim +++ b/app/views/stop_areas/_form.html.slim @@ -7,7 +7,7 @@          = f.input :id, as: :hidden          = f.input :name, :input_html => {:title => t("formtastic.titles#{format_restriction_for_locales(@referential)}.stop_area.name")} -        = f.input :parent_id, as: :select, input_html: { data: { select2_ajax: 'true', url: referential_autocomplete_stop_areas_path(@stop_area_referential), saved: {id: f.object.parent_id, text: f.object.parent.try(:full_name)}}} +        = f.input :parent_id, as: :select, :collection => [f.object.parent_id], input_html: { data: { select2_ajax: 'true', url: referential_autocomplete_stop_areas_path(@stop_area_referential), initvalue: {id: f.object.parent_id, text: f.object.parent.try(:full_name)}}}          = f.input :area_type, as: :select, :input_html => {:disabled => !@stop_area.new_record?}, :collection => Chouette::AreaType.options, :include_blank => false diff --git a/config/locales/stop_areas.en.yml b/config/locales/stop_areas.en.yml index 4d84d1191..f6fe97c9a 100644 --- a/config/locales/stop_areas.en.yml +++ b/config/locales/stop_areas.en.yml @@ -4,6 +4,7 @@ en:      search_no_results: "No stop area matching your query"      errors:        empty: Aucun stop_area_id +      parent_area_type: can not be of type %{area_type}      default_geometry_success: "%{count} modified stop areas"      stop_area:        no_position: "No Position" diff --git a/config/locales/stop_areas.fr.yml b/config/locales/stop_areas.fr.yml index eda1e4e3d..9dd7e0264 100644 --- a/config/locales/stop_areas.fr.yml +++ b/config/locales/stop_areas.fr.yml @@ -4,6 +4,7 @@ fr:      search_no_results: "Aucun arrêt ne correspond à votre recherche"      errors:        empty: Aucun stop_area_id +      parent_area_type: ne peut être de type %{area_type}      default_geometry_success: "%{count} arrêts édités"      stop_area:        no_position: "Pas de position" | 
