Service: add service to procedure

This commit is contained in:
simon lehericey 2018-04-20 12:03:02 +02:00
parent 5d7455f436
commit af7be58132
5 changed files with 76 additions and 2 deletions

View file

@ -0,0 +1,7 @@
@import "constants";
#services-index {
h1 {
margin-top: 2 * $default-padding;
}
}

View file

@ -2,6 +2,7 @@ module NewAdministrateur
class ServicesController < AdministrateurController
def index
@services = services.ordered
@procedure = procedure
end
def new
@ -36,6 +37,16 @@ module NewAdministrateur
end
end
def add_to_procedure
procedure = current_administrateur.procedures.find(procedure_params[:id])
service = services.find(procedure_params[:service_id])
procedure.update(service: service)
redirect_to admin_procedure_path(procedure.id),
notice: "service affecté : #{procedure.service.nom}"
end
private
def service_params
@ -49,5 +60,13 @@ module NewAdministrateur
def services
current_administrateur.services
end
def procedure_params
params.require(:procedure).permit(:id, :service_id)
end
def procedure
current_administrateur.procedures.find(params[:procedure_id])
end
end
end

View file

@ -1,4 +1,16 @@
.container
#services-index.container
%h1 Choix du service pour la procédure
= form_for @procedure, url: { controller: "new_administrateur/services", action: :add_to_procedure } , html: { class: 'form' } do |f|
= f.label :service_id, "La procédure #{@procedure.libelle} est affectée au service"
= f.select :service_id,
current_administrateur.services.map { |s| [ s.nom, s.id ] },
{ prompt: 'choisir un service', selected: @procedure.service&.id },
required: true
= f.hidden_field :id
= f.submit 'valider', class: 'button primary'
%h1 Liste des Services
%table.table.table-service.hoverable

View file

@ -287,7 +287,11 @@ Rails.application.routes.draw do
end
end
resources :services, except: [:destroy, :show]
resources :services, except: [:destroy, :show] do
collection do
patch 'add_to_procedure'
end
end
end
apipie

View file

@ -62,4 +62,36 @@ describe NewAdministrateur::ServicesController, type: :controller do
it { expect(response).to render_template(:edit) }
end
end
describe '#add_to_procedure' do
let!(:procedure) { create(:procedure, administrateur: admin) }
let!(:service) { create(:service, administrateur: admin) }
def post_add_to_procedure
sign_in admin
params = {
procedure: {
id: procedure.id,
service_id: service.id
}
}
patch :add_to_procedure, params: params
procedure.reload
end
context 'when adding a service to a procedure' do
before { post_add_to_procedure }
it { expect(flash.alert).to be_nil }
it { expect(flash.notice).to eq("service affecté : #{service.nom}") }
it { expect(procedure.service_id).to eq(service.id) }
it { expect(response).to redirect_to(admin_procedure_path(procedure.id)) }
end
context 'when stealing a service to add it to a procedure' do
let!(:service) { create(:service) }
it { expect { post_add_to_procedure }.to raise_error(ActiveRecord::RecordNotFound) }
end
end
end