Service: can update a service

This commit is contained in:
Frederic Merizen 2018-04-19 18:12:48 +02:00 committed by simon lehericey
parent c54c85bad8
commit 365cb9b44d
7 changed files with 70 additions and 4 deletions

View file

@ -0,0 +1,9 @@
@import "constants";
.table-service {
.change {
text-align: center;
width: 200px;
padding-left: $default-padding;
}
}

View file

@ -19,12 +19,31 @@ module NewAdministrateur
end
end
def edit
@service = service
end
def update
@service = service
if @service.update(service_params)
redirect_to services_path, notice: "#{@service.nom} modifié"
else
flash[:alert] = @service.errors.full_messages
render :edit
end
end
private
def service_params
params.require(:service).permit(:nom, :type_organisme)
end
def service
services.find(params[:id])
end
def services
current_administrateur.services
end

View file

@ -0,0 +1,5 @@
.container
%h1 Modifier le service
= render partial: 'form',
locals: { service: @service }

View file

@ -1,12 +1,12 @@
.container
%h1 Liste des Services
%table.table
%table.table.table-service.hoverable
%thead
%tr
%th
nom
%th
%th.change
= link_to('Nouveau service', new_service_path, class: 'button')
%tbody
@ -14,4 +14,5 @@
%tr
%td
= service.nom
%td
%td.change
= link_to('modifier', edit_service_path(service))

View file

@ -287,7 +287,7 @@ Rails.application.routes.draw do
end
end
resources :services, only: [:index, :new, :create]
resources :services, except: [:destroy, :show]
end
apipie

View file

@ -24,4 +24,29 @@ describe NewAdministrateur::ServicesController, type: :controller do
it { expect(response).to render_template(:new) }
end
end
describe '#update' do
let!(:service) { create(:service, administrateur: admin) }
let(:service_params) { { nom: 'nom', type_organisme: 'region' } }
before do
sign_in admin
patch :update, params: { id: service.id, service: service_params }
end
context 'when updating a service' do
it { expect(flash.alert).to be_nil }
it { expect(flash.notice).to eq('nom modifié') }
it { expect(Service.last.nom).to eq('nom') }
it { expect(Service.last.type_organisme).to eq('region') }
it { expect(response).to redirect_to(services_path) }
end
context 'when updating a service with invalid data' do
let(:service_params) { { nom: '', type_organisme: 'region' } }
it { expect(flash.alert).not_to be_nil }
it { expect(response).to render_template(:edit) }
end
end
end

View file

@ -0,0 +1,7 @@
FactoryBot.define do
factory :service do
nom 'service'
type_organisme 'commune'
administrateur { create(:administrateur) }
end
end