Service: can create a service

This commit is contained in:
simon lehericey 2018-04-19 10:44:14 +02:00
parent bda0ca8188
commit c54c85bad8
7 changed files with 80 additions and 1 deletions

View file

@ -4,8 +4,27 @@ module NewAdministrateur
@services = services.ordered
end
def new
end
def create
new_service = Service.new(service_params)
new_service.administrateur = current_administrateur
if new_service.save
redirect_to services_path, notice: "#{new_service.nom} créé"
else
flash[:alert] = new_service.errors.full_messages
render :new
end
end
private
def service_params
params.require(:service).permit(:nom, :type_organisme)
end
def services
current_administrateur.services
end

View file

@ -0,0 +1,15 @@
= form_for service, html: { class: 'form' } do |f|
= f.label :nom do
Nom
%span.mandatory *
= f.text_field :nom, placeholder: 'service jeunesse et prévention, direction des affaires maritimes', required: true
= f.label :type_organisme do
Type dorganisme
%span.mandatory *
= f.select :type_organisme, Service.type_organismes.keys.map { |key| [ I18n.t("type_organisme.#{key}"), key] }
.send-wrapper
= f.submit "Valider", class: 'button send'

View file

@ -6,9 +6,12 @@
%tr
%th
nom
%th
= link_to('Nouveau service', new_service_path, class: 'button')
%tbody
- @services.each do |service|
%tr
%td
= service.nom
%td

View file

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

View file

@ -0,0 +1,10 @@
fr:
type_organisme:
administration_centrale: 'administration centrale'
association: 'association'
commune: 'commune'
departement: 'département'
etablissement_enseignement: 'établissement denseignement'
prefecture: 'préfecture'
region: 'région'
autre: 'autre'

View file

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

View file

@ -0,0 +1,27 @@
describe NewAdministrateur::ServicesController, type: :controller do
let(:admin) { create(:administrateur) }
describe '#create' do
before do
sign_in admin
post :create, params: params
end
context 'when submitting a new service' do
let(:params) { { service: { nom: 'super service', type_organisme: 'region' } } }
it { expect(flash.alert).to be_nil }
it { expect(flash.notice).to eq('super service créé') }
it { expect(Service.last.nom).to eq('super service') }
it { expect(Service.last.type_organisme).to eq('region') }
it { expect(response).to redirect_to(services_path) }
end
context 'when submitting an invalid service' do
let(:params) { { service: { nom: 'super service' } } }
it { expect(flash.alert).not_to be_nil }
it { expect(response).to render_template(:new) }
end
end
end