diff options
5 files changed, 25 insertions, 9 deletions
diff --git a/app/assets/javascripts/routing_constraint_zones.coffee b/app/assets/javascripts/routing_constraint_zones.coffee index 7460a64a7..458189b36 100644 --- a/app/assets/javascripts/routing_constraint_zones.coffee +++ b/app/assets/javascripts/routing_constraint_zones.coffee @@ -4,18 +4,24 @@ fill_stop_points_options = -> referential_id = document.location.pathname.match(/\d+/g)[0] line_id = document.location.pathname.match(/\d+/g)[1] route_id = $('#routing_constraint_zone_route_id').val() + if errors_on_form() + stop_point_ids = eval($('#stop_point_ids').val()) $.ajax url: "/referentials/#{referential_id}/lines/#{line_id}/routes/#{route_id}/stop_points" dataType: 'json' success: (data, textStatus, jqXHR) -> for stop_point in data - stop_point_select.append "<option value='#{stop_point.id}'>#{stop_point.name}</option>" + selected = $.inArray(stop_point.id, stop_point_ids) != -1 + stop_point_select.append "<option value='#{stop_point.id}'" + "#{if selected then ' selected' else ''}" + ">#{stop_point.name}</option>" error: (jqXHR, textStatus, errorThrown) -> console.log textStatus console.log errorThrown +errors_on_form = -> + document.location.pathname.endsWith('routing_constraint_zones') && $('#new_routing_constraint_zone').length + $(document).on 'turbolinks:load', -> - if document.location.pathname.endsWith('new') + if document.location.pathname.endsWith('new') || errors_on_form() fill_stop_points_options() $('#routing_constraint_zone_route_id').change(fill_stop_points_options) diff --git a/app/models/chouette/routing_constraint_zone.rb b/app/models/chouette/routing_constraint_zone.rb index d548ce048..a2e5a06cc 100644 --- a/app/models/chouette/routing_constraint_zone.rb +++ b/app/models/chouette/routing_constraint_zone.rb @@ -4,9 +4,13 @@ class Chouette::RoutingConstraintZone < Chouette::TridentActiveRecord validates_presence_of :name, :stop_point_ids, :route_id validates :stop_point_ids, length: { minimum: 2, too_short: I18n.t('activerecord.errors.models.routing_constraint_zone.attributes.stop_points.not_enough_stop_points') } - validate :stop_points_belong_to_route + validate :stop_points_belong_to_route, :not_all_stop_points_selected def stop_points_belong_to_route - errors.add(:stop_points, I18n.t('activerecord.errors.models.routing_constraint_zone.attributes.stop_points.stop_points_not_from_route')) unless stop_points.all? { |sp| route.stop_points.include? sp } + errors.add(:stop_point_ids, I18n.t('activerecord.errors.models.routing_constraint_zone.attributes.stop_points.stop_points_not_from_route')) unless stop_points.all? { |sp| route.stop_points.include? sp } + end + + def not_all_stop_points_selected + errors.add(:stop_point_ids, I18n.t('activerecord.errors.models.routing_constraint_zone.attributes.stop_points.all_stop_points_selected')) if stop_points.length == route.stop_points.length end end diff --git a/app/views/routing_constraint_zones/_form.html.slim b/app/views/routing_constraint_zones/_form.html.slim index baffb6b49..b1c77a44b 100644 --- a/app/views/routing_constraint_zones/_form.html.slim +++ b/app/views/routing_constraint_zones/_form.html.slim @@ -4,7 +4,7 @@ = f.input :name .row .col-lg-6.col-sm-12 - = f.input :route_id, collection: @line.routes, include_blank: false + = f.input :route_id, collection: @line.routes.select { |route| route.stop_points.count > 2 }, include_blank: false .row .col-lg-6.col-sm-12 - stop_points_collection = @routing_constraint_zone.persisted? ? @routing_constraint_zone.route.stop_points : [] @@ -16,3 +16,4 @@ = f.button :submit, class: 'btn btn-danger' += hidden_field_tag 'stop_point_ids', @routing_constraint_zone.stop_point_ids.to_s, id: 'stop_point_ids' diff --git a/config/locales/routing_constraint_zones.fr.yml b/config/locales/routing_constraint_zones.fr.yml index 5b663e00c..556067710 100644 --- a/config/locales/routing_constraint_zones.fr.yml +++ b/config/locales/routing_constraint_zones.fr.yml @@ -16,6 +16,7 @@ fr: stop_points: not_enough_stop_points: "Il faut mettre au moins deux arrêts sur séquence d'arrêts." stop_points_not_from_route: "Arrêt sur séquence d'arrêts n'appartient pas à la Route de cette Zone de contrainte." + all_stop_points_selected: "Une zone de contrainte ne peut pas couvrir tous les arrêts d'une ligne." routing_constraint_zones: actions: new: Ajouter une zone de contrainte diff --git a/spec/models/chouette/routing_constraint_zone_spec.rb b/spec/models/chouette/routing_constraint_zone_spec.rb index c83942456..0f34db5f9 100644 --- a/spec/models/chouette/routing_constraint_zone_spec.rb +++ b/spec/models/chouette/routing_constraint_zone_spec.rb @@ -3,6 +3,7 @@ require 'spec_helper' describe Chouette::RoutingConstraintZone, type: :model do subject { create(:routing_constraint_zone) } + let!(:routing_constraint_zone) { create(:routing_constraint_zone) } it { is_expected.to validate_presence_of :name } # shoulda matcher to validate length of array ? @@ -10,31 +11,34 @@ describe Chouette::RoutingConstraintZone, type: :model do describe 'validations' do it 'validates the presence of route_id' do - routing_constraint_zone = create(:routing_constraint_zone) expect { routing_constraint_zone.update!(route_id: nil) }.to raise_error end it 'validates the presence of stop_point_ids' do - routing_constraint_zone = create(:routing_constraint_zone) expect { routing_constraint_zone.update!(stop_point_ids: []) }.to raise_error(ActiveRecord::RecordInvalid) end it 'validates that stop points belong to the route' do - routing_constraint_zone = create(:routing_constraint_zone) route = create(:route) expect { routing_constraint_zone.update!(route_id: route.id) }.to raise_error(ActiveRecord::RecordInvalid) end + + it 'validates that not all stop points from the route are selected' do + routing_constraint_zone.stop_points = routing_constraint_zone.route.stop_points + expect { + routing_constraint_zone.save! + }.to raise_error(ActiveRecord::RecordInvalid) + end end describe 'deleted stop areas' do it 'does not have them in stop_area_ids' do - routing_constraint_zone = create(:routing_constraint_zone) stop_point = routing_constraint_zone.route.stop_points.last routing_constraint_zone.stop_points << stop_point routing_constraint_zone.save! |
