diff --git a/app/assets/stylesheets/new_design/table-service.scss b/app/assets/stylesheets/new_design/table-service.scss new file mode 100644 index 000000000..bf7e9d67f --- /dev/null +++ b/app/assets/stylesheets/new_design/table-service.scss @@ -0,0 +1,9 @@ +@import "constants"; + +.table-service { + .change { + text-align: center; + width: 200px; + padding-left: $default-padding; + } +} diff --git a/app/controllers/new_administrateur/services_controller.rb b/app/controllers/new_administrateur/services_controller.rb index c0ff1366a..01f9484a7 100644 --- a/app/controllers/new_administrateur/services_controller.rb +++ b/app/controllers/new_administrateur/services_controller.rb @@ -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 diff --git a/app/views/new_administrateur/services/edit.html.haml b/app/views/new_administrateur/services/edit.html.haml new file mode 100644 index 000000000..bcb573318 --- /dev/null +++ b/app/views/new_administrateur/services/edit.html.haml @@ -0,0 +1,5 @@ +.container + %h1 Modifier le service + + = render partial: 'form', + locals: { service: @service } diff --git a/app/views/new_administrateur/services/index.html.haml b/app/views/new_administrateur/services/index.html.haml index 5a3140184..b02598129 100644 --- a/app/views/new_administrateur/services/index.html.haml +++ b/app/views/new_administrateur/services/index.html.haml @@ -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)) diff --git a/config/routes.rb b/config/routes.rb index 27eedef16..d8e1276df 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -287,7 +287,7 @@ Rails.application.routes.draw do end end - resources :services, only: [:index, :new, :create] + resources :services, except: [:destroy, :show] end apipie diff --git a/spec/controllers/new_administrateur/services_controller_spec.rb b/spec/controllers/new_administrateur/services_controller_spec.rb index 91a74a67d..d87aeb2ab 100644 --- a/spec/controllers/new_administrateur/services_controller_spec.rb +++ b/spec/controllers/new_administrateur/services_controller_spec.rb @@ -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 diff --git a/spec/factories/service.rb b/spec/factories/service.rb new file mode 100644 index 000000000..bffd5dc44 --- /dev/null +++ b/spec/factories/service.rb @@ -0,0 +1,7 @@ +FactoryBot.define do + factory :service do + nom 'service' + type_organisme 'commune' + administrateur { create(:administrateur) } + end +end