From 87f435d2d36062c4ec9adcc571e6a1d4c0c2b14a Mon Sep 17 00:00:00 2001 From: Martin Date: Tue, 29 Aug 2023 09:22:58 +0200 Subject: [PATCH] amelioration(procedure.chorus): ajoute le form component pour le 'crud' du ChorusConfiguration --- .../procedure/chorus_form_component.rb | 7 ++ .../chorus_form_component.html.haml | 15 ++++ .../administrateurs/chorus_controller.rb | 20 ++++++ app/models/chorus_configuration.rb | 24 +++++++ .../concerns/procedure_chorus_concern.rb | 4 ++ .../administrateurs/chorus/edit.html.haml | 2 + config/routes.rb | 2 +- .../administrateurs/chorus_controller_spec.rb | 69 +++++++++++++++++++ 8 files changed, 142 insertions(+), 1 deletion(-) create mode 100644 app/components/procedure/chorus_form_component.rb create mode 100644 app/components/procedure/chorus_form_component/chorus_form_component.html.haml create mode 100644 app/models/chorus_configuration.rb diff --git a/app/components/procedure/chorus_form_component.rb b/app/components/procedure/chorus_form_component.rb new file mode 100644 index 000000000..219b4e066 --- /dev/null +++ b/app/components/procedure/chorus_form_component.rb @@ -0,0 +1,7 @@ +class Procedure::ChorusFormComponent < ApplicationComponent + attr_reader :procedure + + def initialize(procedure:) + @procedure = procedure + end +end diff --git a/app/components/procedure/chorus_form_component/chorus_form_component.html.haml b/app/components/procedure/chorus_form_component/chorus_form_component.html.haml new file mode 100644 index 000000000..6f48026ba --- /dev/null +++ b/app/components/procedure/chorus_form_component/chorus_form_component.html.haml @@ -0,0 +1,15 @@ += form_for([procedure, procedure.chorus_configuration],url: admin_procedure_chorus_path(procedure), method: :put) do |f| + + .fr-select-group + = f.label :centre_de_coup, class: 'fr-label' + = f.select :centre_de_coup, options_for_select(ChorusConfiguration.centre_de_coup_options, procedure.chorus_configuration.centre_de_coup), {}, class: 'fr-select' + + .fr-select-group + = f.label :domaine_fonctionnel, class: 'fr-label' + = f.select :domaine_fonctionnel, options_for_select(ChorusConfiguration.domaine_fonctionnel_options, procedure.chorus_configuration.domaine_fonctionnel), {}, class: 'fr-select' + + .fr-select-group + = f.label :referentiel_de_programmation, class: 'fr-label' + = f.select :referentiel_de_programmation, options_for_select(ChorusConfiguration.referentiel_de_programmation_options, procedure.chorus_configuration.referentiel_de_programmation), {}, class: 'fr-select' + + = f.submit "Enregister", class: 'fr-btn' diff --git a/app/controllers/administrateurs/chorus_controller.rb b/app/controllers/administrateurs/chorus_controller.rb index ebf5b682f..e6b850392 100644 --- a/app/controllers/administrateurs/chorus_controller.rb +++ b/app/controllers/administrateurs/chorus_controller.rb @@ -4,5 +4,25 @@ module Administrateurs def edit end + + def update + @configuration = @procedure.chorus_configuration + @configuration.assign_attributes(configurations_params) + if @configuration.valid? + @procedure.update!(chorus: @configuration.attributes) + + flash.notice = "La configuration Chorus a été mise à jour et prend immédiatement effet pour les nouveaux dossiers." + redirect_to admin_procedure_path(@procedure) + else + flash.now.alert = "Des erreurs empêchent la validation du connecteur chorus. Corrigez les erreurs" + render :edit + end + end + + private + + def configurations_params + params.require(:chorus_configuration).permit(:centre_de_coup, :domaine_fonctionnel, :referentiel_de_programmation) + end end end diff --git a/app/models/chorus_configuration.rb b/app/models/chorus_configuration.rb new file mode 100644 index 000000000..cec9eb9a1 --- /dev/null +++ b/app/models/chorus_configuration.rb @@ -0,0 +1,24 @@ +class ChorusConfiguration + include ActiveModel::Model + include ActiveModel::Attributes + + attribute :centre_de_coup, default: nil + attribute :domaine_fonctionnel, default: nil + attribute :referentiel_de_programmation, default: nil + + validates :centre_de_coup, inclusion: { in: Proc.new { ChorusConfiguration.centre_de_coup_options } } + validates :domaine_fonctionnel, inclusion: { in: Proc.new { ChorusConfiguration.domaine_fonctionnel_options } } + validates :referentiel_de_programmation, inclusion: { in: Proc.new { ChorusConfiguration.referentiel_de_programmation_options } } + + def self.centre_de_coup_options + [1, 2, 3].map(&:to_s) + end + + def self.domaine_fonctionnel_options + [4, 5, 6].map(&:to_s) + end + + def self.referentiel_de_programmation_options + [7, 8, 9].map(&:to_s) + end +end diff --git a/app/models/concerns/procedure_chorus_concern.rb b/app/models/concerns/procedure_chorus_concern.rb index 5de495d1e..fe71152f5 100644 --- a/app/models/concerns/procedure_chorus_concern.rb +++ b/app/models/concerns/procedure_chorus_concern.rb @@ -2,6 +2,10 @@ module ProcedureChorusConcern extend ActiveSupport::Concern included do + def chorus_configuration + @chorus_configuration ||= ChorusConfiguration.new(chorus) + end + def chorusable? feature_enabled?(:chorus) end diff --git a/app/views/administrateurs/chorus/edit.html.haml b/app/views/administrateurs/chorus/edit.html.haml index 6d6e2327a..8f6037e99 100644 --- a/app/views/administrateurs/chorus/edit.html.haml +++ b/app/views/administrateurs/chorus/edit.html.haml @@ -7,3 +7,5 @@ .container %h1.mb-2 Cadre budgétaire + + = render Procedure::ChorusFormComponent.new(procedure: @procedure) diff --git a/config/routes.rb b/config/routes.rb index 9c2e0f460..21158ba68 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -601,7 +601,7 @@ Rails.application.routes.draw do resource :attestation_template, only: [:show, :edit, :update, :create] do get 'preview', on: :member end - resource :chorus, only: [:edit] + resource :chorus, only: [:edit, :update] resource :dossier_submitted_message, only: [:edit, :update, :create] # ADDED TO ACCESS IT FROM THE IFRAME get 'attestation_template/preview' => 'attestation_templates#preview' diff --git a/spec/controllers/administrateurs/chorus_controller_spec.rb b/spec/controllers/administrateurs/chorus_controller_spec.rb index 0e0005626..89b6d7278 100644 --- a/spec/controllers/administrateurs/chorus_controller_spec.rb +++ b/spec/controllers/administrateurs/chorus_controller_spec.rb @@ -17,6 +17,75 @@ describe Administrateurs::ChorusController, type: :controller do context 'signed as admin' do before { sign_in(admin.user) } it { is_expected.to have_http_status(200) } + + context 'rendered' do + render_views + + it { is_expected.to have_http_status(200) } + end + end + end + + describe 'update' do + let(:user) { create(:user) } + let(:admin) { create(:administrateur, user: create(:user)) } + let(:procedure) { create(:procedure, administrateurs: [admin]) } + let(:chorus_configuration_params) { {} } + subject do + put :update, + params: { + procedure_id: procedure.id, + chorus_configuration: chorus_configuration_params + } + end + + context 'not signed in' do + it { is_expected.to redirect_to(new_user_session_path) } + end + + context 'signed in but not admin of procedure' do + before { sign_in(user) } + it { is_expected.to redirect_to(new_user_session_path) } + end + + context 'signed as admin' do + before { sign_in(admin.user) } + + context "valid payload" do + let(:chorus_configuration_params) do + { + centre_de_coup: ChorusConfiguration.centre_de_coup_options.first, + domaine_fonctionnel: ChorusConfiguration.domaine_fonctionnel_options.first, + referentiel_de_programmation: ChorusConfiguration.referentiel_de_programmation_options.first + } + end + + it { is_expected.to redirect_to(admin_procedure_path(procedure)) } + it 'updates params' do + subject + expect(flash[:notice]).to eq("La configuration Chorus a été mise à jour et prend immédiatement effet pour les nouveaux dossiers.") + procedure.reload + expect(procedure.chorus_configuration.centre_de_coup).to eq(ChorusConfiguration.centre_de_coup_options.first) + expect(procedure.chorus_configuration.domaine_fonctionnel).to eq(ChorusConfiguration.domaine_fonctionnel_options.first) + expect(procedure.chorus_configuration.referentiel_de_programmation).to eq(ChorusConfiguration.referentiel_de_programmation_options.first) + end + end + + context "invalid payload" do + let(:chorus_configuration_params) do + { + centre_de_coup: 0 + } + end + + it { is_expected.to have_http_status(200) } + it 'updates params' do + subject + expect(flash[:notice]).to eq("Des erreurs empêchent la validation du connecteur chorus. Corrigez les erreurs") + procedure.reload + expect(procedure.chorus_configuration.centre_de_coup).to eq(nil) + end + end end end end