diff options
| -rw-r--r-- | app/controllers/routes_controller.rb | 9 | ||||
| -rw-r--r-- | app/decorators/route_decorator.rb | 12 | ||||
| -rw-r--r-- | app/models/chouette/route.rb | 7 | ||||
| -rw-r--r-- | app/policies/route_policy.rb | 4 | ||||
| -rw-r--r-- | app/views/routes/duplicate.html.slim | 8 | ||||
| -rw-r--r-- | config/locales/routes.en.yml | 2 | ||||
| -rw-r--r-- | config/locales/routes.fr.yml | 2 | ||||
| -rw-r--r-- | config/routes.rb | 3 | ||||
| -rw-r--r-- | spec/controllers/routes_controller_spec.rb | 27 | ||||
| -rw-r--r-- | spec/models/chouette/route/route_duplication_spec.rb | 13 | ||||
| -rw-r--r-- | spec/policies/route_policy_spec.rb | 4 | ||||
| -rw-r--r-- | spec/routing/routes_routing_spec.rb | 8 |
12 files changed, 38 insertions, 61 deletions
diff --git a/app/controllers/routes_controller.rb b/app/controllers/routes_controller.rb index e20ae9d14..04f63c112 100644 --- a/app/controllers/routes_controller.rb +++ b/app/controllers/routes_controller.rb @@ -70,13 +70,8 @@ class RoutesController < ChouetteController end def duplicate - @route = Chouette::Route.find(params[:id]) - @line = @route.line - build_breadcrumb(:edit) - end - - def post_duplicate - @route = Chouette::Route.find(params[:id]) + route = Chouette::Route.find(params[:id]).duplicate + redirect_to edit_referential_line_route_path(@referential, route.line, route) end protected diff --git a/app/decorators/route_decorator.rb b/app/decorators/route_decorator.rb index 484c3db04..46cb6cd5f 100644 --- a/app/decorators/route_decorator.rb +++ b/app/decorators/route_decorator.rb @@ -58,6 +58,18 @@ class RouteDecorator < Draper::Decorator ) end + if h.policy(object).duplicate? + links << Link.new( + content: h.t('routes.duplicate.title'), + href: h.duplicate_referential_line_route_path( + context[:referential], + context[:line], + object + ), + method: :post + ) + end + links end end diff --git a/app/models/chouette/route.rb b/app/models/chouette/route.rb index 049ee2881..cab877dc7 100644 --- a/app/models/chouette/route.rb +++ b/app/models/chouette/route.rb @@ -76,11 +76,10 @@ class Chouette::Route < Chouette::TridentActiveRecord after_commit :journey_patterns_control_route_sections - def duplicate name: nil, published_name: nil + def duplicate overrides = { - 'name' => name || self.name, - 'published_name' => published_name || self.published_name, - 'object_version' => object_version - 1 + 'object_version' => object_version - 1, + 'opposite_route_id' => nil } keys_for_create = attributes.keys - %w{id objectid created_at updated_at} atts_for_create = attributes diff --git a/app/policies/route_policy.rb b/app/policies/route_policy.rb index 786b0acf4..7e9fe251a 100644 --- a/app/policies/route_policy.rb +++ b/app/policies/route_policy.rb @@ -16,4 +16,8 @@ class RoutePolicy < ApplicationPolicy def update? !archived? && organisation_match? && user.has_permission?('routes.update') end + + def duplicate? + create? + end end diff --git a/app/views/routes/duplicate.html.slim b/app/views/routes/duplicate.html.slim deleted file mode 100644 index 2c894cc47..000000000 --- a/app/views/routes/duplicate.html.slim +++ /dev/null @@ -1,8 +0,0 @@ -= pageheader 'map-marker', - t('routes.duplicate.title', route: @route.name) - -.page_content - .container-fluid - .row - .col-lg-8.col-lg-offset-2.col-md-8.col-md-offset-2.col-sm-10.col-sm-offset-1 - == render 'form' diff --git a/config/locales/routes.en.yml b/config/locales/routes.en.yml index be704fe15..e94adf490 100644 --- a/config/locales/routes.en.yml +++ b/config/locales/routes.en.yml @@ -33,7 +33,7 @@ en: for_boarding: "Boarding" for_alighting: "Alighting" duplicate: - title: "Duplicate route %{route}" + title: "Duplicate route" route: no_journey_pattern: "No Journey pattern" wayback: diff --git a/config/locales/routes.fr.yml b/config/locales/routes.fr.yml index 986e9ba9e..a494e60ec 100644 --- a/config/locales/routes.fr.yml +++ b/config/locales/routes.fr.yml @@ -33,7 +33,7 @@ fr: for_boarding: "Montée" for_alighting: "Descente" duplicate: - title: "Dupliquer l'itinéraire %{route}" + title: "Dupliquer l'itinéraire" route: no_journey_pattern: "Pas de mission" wayback: diff --git a/config/routes.rb b/config/routes.rb index e73a2a494..f6cf5e672 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -110,8 +110,7 @@ ChouetteIhm::Application.routes.draw do member do get 'edit_boarding_alighting' put 'save_boarding_alighting' - get 'duplicate' - post 'duplicate', to: 'routes#post_duplicate' + post 'duplicate', to: 'routes#duplicate' end resource :journey_patterns_collection, :only => [:show, :update] resources :journey_patterns do diff --git a/spec/controllers/routes_controller_spec.rb b/spec/controllers/routes_controller_spec.rb index 3862d8173..336f20945 100644 --- a/spec/controllers/routes_controller_spec.rb +++ b/spec/controllers/routes_controller_spec.rb @@ -1,3 +1,5 @@ +Route = Chouette::Route + RSpec.describe RoutesController, type: :controller do login_user @@ -75,31 +77,20 @@ RSpec.describe RoutesController, type: :controller do end end - describe "GET /duplicate" do - it "returns success" do - get :duplicate, - referential_id: route.line.line_referential_id, - line_id: route.line_id, - id: route.id - - expect(response).to be_success - end - end describe "POST /duplicate" do + let!( :route_prime ){ route } + it "creates a new route" do expect do post :duplicate, referential_id: route.line.line_referential_id, line_id: route.line_id, - id: route.id, - params: { - name: '102 Route', - published_name: '102 route' - } - end.to change { Chouette::Route.count }.by(1) - - expect(Chouette::Route.last.name).to eq('102 Route') + id: route.id + end.to change { Route.count }.by(1) + + expect(Route.last.name).to eq(route.name) + expect(Route.last.published_name).to eq(route.published_name) end end end diff --git a/spec/models/chouette/route/route_duplication_spec.rb b/spec/models/chouette/route/route_duplication_spec.rb index 74e944181..6645b909f 100644 --- a/spec/models/chouette/route/route_duplication_spec.rb +++ b/spec/models/chouette/route/route_duplication_spec.rb @@ -13,19 +13,6 @@ RSpec.describe Route do route.duplicate expect( values_for_create(Route.last, except: %w{objectid}) ).to eq( values_for_create( route, except: %w{objectid} ) ) end - it 'but some can be redefined optionally', :wip do - excluded_attributes = %w{objectid checksum checksum_source} - expected_attributes = values_for_create( - route, - name: 'new name', - published_name: 'new published name', - except: excluded_attributes ) - - route.duplicate name: 'new name', published_name: 'new published name' - expect( - values_for_create(Route.last, except: excluded_attributes) - ).to eq( expected_attributes ) - end it 'and others cannot' do expect{ route.duplicate name: 'YAN', line_id: 42 }.to raise_error(ArgumentError) end diff --git a/spec/policies/route_policy_spec.rb b/spec/policies/route_policy_spec.rb index 243d85acb..d7edceaef 100644 --- a/spec/policies/route_policy_spec.rb +++ b/spec/policies/route_policy_spec.rb @@ -6,6 +6,10 @@ RSpec.describe RoutePolicy, type: :policy do it_behaves_like 'permitted policy and same organisation', 'routes.create', archived: true end + permissions :duplicate? do + it_behaves_like 'permitted policy and same organisation', 'routes.create', archived: true + end + permissions :destroy? do it_behaves_like 'permitted policy and same organisation', 'routes.destroy', archived: true end diff --git a/spec/routing/routes_routing_spec.rb b/spec/routing/routes_routing_spec.rb index 4635603d4..311de9f39 100644 --- a/spec/routing/routes_routing_spec.rb +++ b/spec/routing/routes_routing_spec.rb @@ -3,16 +3,10 @@ RSpec.describe "routes for Routes", type: :routing do let( :controller ){ {controller: 'routes', referential_id: ':referential_id', line_id: ':line_id', id: ':id'} } - it 'with method get to #duplicate' do - expect( - get: '/referentials/:referential_id/lines/:line_id/routes/:id/duplicate' - ).to route_to controller.merge(action: 'duplicate') - end - it 'with method post to #post_duplicate' do expect( post: '/referentials/:referential_id/lines/:line_id/routes/:id/duplicate' - ).to route_to controller.merge(action: 'post_duplicate') + ).to route_to controller.merge(action: 'duplicate') end end end |
