Service: can create a service
This commit is contained in:
parent
bda0ca8188
commit
c54c85bad8
7 changed files with 80 additions and 1 deletions
|
@ -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
|
||||
|
|
15
app/views/new_administrateur/services/_form.html.haml
Normal file
15
app/views/new_administrateur/services/_form.html.haml
Normal 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 d’organisme
|
||||
%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'
|
|
@ -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
|
||||
|
|
5
app/views/new_administrateur/services/new.html.haml
Normal file
5
app/views/new_administrateur/services/new.html.haml
Normal file
|
@ -0,0 +1,5 @@
|
|||
.container
|
||||
%h1 Nouveau Service
|
||||
|
||||
= render partial: 'form',
|
||||
locals: { service: Service.new }
|
10
config/locales/models/service/fr.yml
Normal file
10
config/locales/models/service/fr.yml
Normal file
|
@ -0,0 +1,10 @@
|
|||
fr:
|
||||
type_organisme:
|
||||
administration_centrale: 'administration centrale'
|
||||
association: 'association'
|
||||
commune: 'commune'
|
||||
departement: 'département'
|
||||
etablissement_enseignement: 'établissement d’enseignement'
|
||||
prefecture: 'préfecture'
|
||||
region: 'région'
|
||||
autre: 'autre'
|
|
@ -287,7 +287,7 @@ Rails.application.routes.draw do
|
|||
end
|
||||
end
|
||||
|
||||
resources :services, only: [:index]
|
||||
resources :services, only: [:index, :new, :create]
|
||||
end
|
||||
|
||||
apipie
|
||||
|
|
|
@ -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
|
Loading…
Reference in a new issue