aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorZog2018-04-04 15:41:37 +0200
committerZog2018-04-04 15:41:37 +0200
commitb7a70ae0cea3221667b4b490dbae8bc0712321b0 (patch)
tree12d2ad997f4255ac2a8dfe7ebd58347efcc04f02
parent23318bbe07b73cef50ec69d5e597386a116e2b82 (diff)
downloadchouette-core-b7a70ae0cea3221667b4b490dbae8bc0712321b0.tar.bz2
Refs 6226; Add a button to create opposite routes
-rw-r--r--app/controllers/routes_controller.rb3
-rw-r--r--app/decorators/route_decorator.rb17
-rw-r--r--app/models/chouette/route.rb24
-rw-r--r--app/models/chouette/stop_point.rb3
-rw-r--r--app/policies/route_policy.rb4
-rw-r--r--config/locales/routes.fr.yml4
-rw-r--r--spec/controllers/routes_controller_spec.rb18
7 files changed, 62 insertions, 11 deletions
diff --git a/app/controllers/routes_controller.rb b/app/controllers/routes_controller.rb
index 96a23c938..ac243c8eb 100644
--- a/app/controllers/routes_controller.rb
+++ b/app/controllers/routes_controller.rb
@@ -63,7 +63,8 @@ class RoutesController < ChouetteController
end
def duplicate
- route = Chouette::Route.find(params[:id]).duplicate
+ source = Chouette::Route.find(params[:id])
+ route = source.duplicate params[:opposite]
flash[:notice] = t('routes.duplicate.success')
redirect_to referential_line_path(@referential, route.line)
end
diff --git a/app/decorators/route_decorator.rb b/app/decorators/route_decorator.rb
index fa6367924..4a173cbb9 100644
--- a/app/decorators/route_decorator.rb
+++ b/app/decorators/route_decorator.rb
@@ -71,6 +71,23 @@ class RouteDecorator < AF83::Decorator
end
end
+ instance_decorator.action_link(
+ secondary: :show,
+ policy: :create_opposite,
+ if: ->{h.has_feature?(:create_opposite_routes) && object.opposite_route.nil?}
+ ) do |l|
+ l.content h.t('routes.create_opposite.title')
+ l.method :post
+ l.href do
+ h.duplicate_referential_line_route_path(
+ context[:referential],
+ context[:line],
+ object,
+ opposite: true
+ )
+ end
+ end
+
instance_decorator.destroy_action_link do |l|
l.data confirm: h.t('routes.actions.destroy_confirm')
end
diff --git a/app/models/chouette/route.rb b/app/models/chouette/route.rb
index 13288bc6b..65947c392 100644
--- a/app/models/chouette/route.rb
+++ b/app/models/chouette/route.rb
@@ -68,28 +68,34 @@ module Chouette
validates_presence_of :published_name
validates_presence_of :line
validates :wayback, inclusion: { in: self.wayback.values }
-
after_save :calculate_costs!, if: ->() { TomTom.enabled? }
-
- def duplicate
+
+ def duplicate opposite=false
overrides = {
'opposite_route_id' => nil,
'name' => I18n.t('activerecord.copy', name: self.name)
}
+ keys_for_create = attributes.keys - %w{id objectid created_at updated_at}
atts_for_create = attributes
- .slice!(*%w{id objectid created_at updated_at})
+ .slice(*keys_for_create)
.merge(overrides)
+ if opposite
+ atts_for_create[:wayback] = self.opposite_wayback
+ atts_for_create[:name] = I18n.t('routes.opposite', name: self.name)
+ atts_for_create[:published_name] = atts_for_create[:name]
+ atts_for_create[:opposite_route_id] = self.id
+ end
new_route = self.class.create!(atts_for_create)
- duplicate_stop_points(for_route: new_route)
+ duplicate_stop_points(for_route: new_route, opposite: opposite)
new_route
end
- def duplicate_stop_points(for_route:)
- stop_points.each(&duplicate_stop_point(for_route: for_route))
+ def duplicate_stop_points(for_route:, opposite: false)
+ stop_points.each(&duplicate_stop_point(for_route: for_route, opposite: opposite))
end
- def duplicate_stop_point(for_route:)
+ def duplicate_stop_point(for_route:, opposite: false)
-> stop_point do
- stop_point.duplicate(for_route: for_route)
+ stop_point.duplicate(for_route: for_route, opposite: opposite)
end
end
diff --git a/app/models/chouette/stop_point.rb b/app/models/chouette/stop_point.rb
index 42a207774..27407243b 100644
--- a/app/models/chouette/stop_point.rb
+++ b/app/models/chouette/stop_point.rb
@@ -46,11 +46,12 @@ module Chouette
end
end
- def duplicate(for_route:)
+ def duplicate(for_route:, opposite: false)
keys_for_create = attributes.keys - %w{id objectid created_at updated_at}
atts_for_create = attributes
.slice(*keys_for_create)
.merge('route_id' => for_route.id)
+ atts_for_create["position"] = self.route.stop_points.size - atts_for_create["position"] if opposite
self.class.create!(atts_for_create)
end
diff --git a/app/policies/route_policy.rb b/app/policies/route_policy.rb
index 0337a5300..4fcb6be11 100644
--- a/app/policies/route_policy.rb
+++ b/app/policies/route_policy.rb
@@ -20,4 +20,8 @@ class RoutePolicy < ApplicationPolicy
def duplicate?
create?
end
+
+ def create_opposite?
+ create?
+ end
end
diff --git a/config/locales/routes.fr.yml b/config/locales/routes.fr.yml
index ddf706794..9539a2ee3 100644
--- a/config/locales/routes.fr.yml
+++ b/config/locales/routes.fr.yml
@@ -16,6 +16,7 @@ fr:
add_stop_point: "Ajouter un arrêt"
new_stop_point: "Créer un arrêt pour l'ajouter"
opposite_route_timetable: "Horaires retour"
+ opposite: "%{name} (retour)"
new:
title: "Ajouter un itinéraire"
edit:
@@ -56,6 +57,9 @@ fr:
stop_area_name: "Nom de l'arrêt"
for_boarding: "Montée"
for_alighting: "Descente"
+ create_opposite:
+ title: "Créer retour"
+ success: "itinéraire créé avec succès"
duplicate:
title: "Dupliquer l'itinéraire"
success: "itinéraire dupliqué avec succès"
diff --git a/spec/controllers/routes_controller_spec.rb b/spec/controllers/routes_controller_spec.rb
index e4dc6bc23..aae9b6916 100644
--- a/spec/controllers/routes_controller_spec.rb
+++ b/spec/controllers/routes_controller_spec.rb
@@ -83,6 +83,24 @@ RSpec.describe RoutesController, type: :controller do
expect(Chouette::Route.last.name).to eq(I18n.t('activerecord.copy', name: route.name))
expect(Chouette::Route.last.published_name).to eq(route.published_name)
+ expect(Chouette::Route.last.stop_area_ids).to eq route.stop_area_ids
+ end
+
+ context "when opposite = true" do
+ it "creates a new route on the opposite way " do
+ expect do
+ post :duplicate,
+ referential_id: route.line.line_referential_id,
+ line_id: route.line_id,
+ id: route.id,
+ opposite: TRUE
+ end.to change { Chouette::Route.count }.by(1)
+
+ expect(Chouette::Route.last.name).to eq(I18n.t('routes.opposite', name: route.name))
+ expect(Chouette::Route.last.published_name).to eq(Chouette::Route.last.name)
+ expect(Chouette::Route.last.opposite_route).to eq(route)
+ expect(Chouette::Route.last.stop_area_ids).to eq route.stop_area_ids.reverse
+ end
end
end
end