From 9501ebeb7b43323263e2ecded2b4568f4b07b994 Mon Sep 17 00:00:00 2001 From: Adrien Di Pasquale Date: Wed, 20 Nov 2024 09:20:53 +0100 Subject: [PATCH] introduction form object pour les proches + ajout de la validation --- app/controllers/users/relatives_controller.rb | 51 +++++++++++-------- app/form_models/relative_user_form.rb | 34 +++++++++++++ .../users/relatives/_form_fields.html.slim | 6 ++- app/views/users/relatives/edit.html.slim | 6 +-- app/views/users/relatives/new.html.slim | 4 +- .../users/relatives_controller_spec.rb | 17 +------ 6 files changed, 76 insertions(+), 42 deletions(-) create mode 100644 app/form_models/relative_user_form.rb diff --git a/app/controllers/users/relatives_controller.rb b/app/controllers/users/relatives_controller.rb index 036a8145c7..79f5a58dd0 100644 --- a/app/controllers/users/relatives_controller.rb +++ b/app/controllers/users/relatives_controller.rb @@ -4,35 +4,40 @@ class Users::RelativesController < UserAuthController respond_to :html def new - @user = current_user.relatives.new - authorize(@user, policy_class: User::UserPolicy) - respond_modal_with @user + user = current_user.relatives.new + authorize(user, policy_class: User::UserPolicy) + @form = RelativeUserForm.new(user:, **form_params) + respond_modal_with @form end def create - @user = User.new(user_params) - @user.created_through = "user_relative_creation" - @user.responsible_id = current_user.id - @user.organisation_ids = current_user.organisation_ids - authorize(@user, policy_class: User::UserPolicy) + user = User.new( + created_through: "user_relative_creation", + responsible_id: current_user.id, + organisation_ids: current_user.organisation_ids + ) + authorize(user, policy_class: User::UserPolicy) return_location = request.referer - if @user.save - flash[:success] = "#{@user.full_name} a été ajouté comme proche." - return_location = add_query_string_params_to_url(request.referer, created_user_id: @user.id) + @form = RelativeUserForm.new(user:) + if @form.submit(**form_params) + flash[:success] = "#{@form.user.full_name} a été ajouté comme proche." + return_location = add_query_string_params_to_url(request.referer, created_user_id: @form.user.id) end - respond_modal_with @user, location: return_location + respond_modal_with @form, location: return_location end def edit - @user = User.find(params[:id]) - authorize(@user, policy_class: User::UserPolicy) + user = User.find(params.require(:id)) + authorize(user, policy_class: User::UserPolicy) + @form = RelativeUserForm.new(user:, **form_params) end def update - @user = User.find(params[:id]) - authorize(@user, policy_class: User::UserPolicy) - if @user.update(user_params) - flash[:success] = "Les informations de votre proche #{@user.full_name} ont été mises à jour." + user = User.find(params.require(:id)) + authorize(user, policy_class: User::UserPolicy) + @form = RelativeUserForm.new(user:) + if @form.submit(**form_params) + flash[:success] = "Les informations de votre proche #{@form.user.full_name} ont été mises à jour." redirect_to users_informations_path else render :edit @@ -48,7 +53,13 @@ def destroy private - def user_params - params.require(:user).permit(:first_name, :last_name, :birth_date, :ants_pre_demande_number) + def form_params + { + ants_pre_demande_number_required: params[:ants_pre_demande_number_required]&.to_b, + first_name: params.dig(:user, :first_name), + last_name: params.dig(:user, :last_name), + birth_date: params.dig(:user, :birth_date), + ants_pre_demande_number: params.dig(:user, :ants_pre_demande_number), + }.compact end end diff --git a/app/form_models/relative_user_form.rb b/app/form_models/relative_user_form.rb new file mode 100644 index 0000000000..54664a2a30 --- /dev/null +++ b/app/form_models/relative_user_form.rb @@ -0,0 +1,34 @@ +class RelativeUserForm + include ActiveModel::Model + + attr_reader :user, :ants_pre_demande_number_required + + DELEGATED_ATTRIBUTES = %i[first_name last_name birth_date ants_pre_demande_number].freeze + + delegate(*DELEGATED_ATTRIBUTES, to: :user) + delegate :persisted?, to: :user # pour que form_for utilise la bonne route PUT ou POST + + validate :validate_user + validates_with AntsPreDemandeNumberValidation, if: :ants_pre_demande_number_required + + def initialize(user:, **params) + @user = user + assign_attributes_from_params(params) + end + + def submit(**params) + assign_attributes_from_params(params) + valid? && user.save + end + + private + + def assign_attributes_from_params(params) + @ants_pre_demande_number_required = params[:ants_pre_demande_number_required] + @user.assign_attributes(params.slice(*DELEGATED_ATTRIBUTES)) + end + + def validate_user + errors.merge!(user) if user.invalid? + end +end diff --git a/app/views/users/relatives/_form_fields.html.slim b/app/views/users/relatives/_form_fields.html.slim index f728b3dda9..1d2438521d 100644 --- a/app/views/users/relatives/_form_fields.html.slim +++ b/app/views/users/relatives/_form_fields.html.slim @@ -1,7 +1,9 @@ .form-row .col-md-6= f.input :first_name .col-md-6= f.input :last_name -- if params[:ants_pre_demande_number_required].to_b - = f.input :ants_pre_demande_number, required: true, hint: t("simple_form.hints.user.ants_pre_demande_number_html"), input_html: {style: "text-transform: uppercase;"} +- if f.object.ants_pre_demande_number_required + = f.input :ants_pre_demande_number, label: "Numéro de pré-demande ANTS", required: true, hint: t("simple_form.hints.user.ants_pre_demande_number_html"), input_html: {style: "text-transform: uppercase;"} + = hidden_field_tag :ants_pre_demande_number_required, "true" + / on veut un nom niveau racine, et pas imbriqué sous user[..] - if current_domain != Domain::RDV_MAIRIE = f.input :birth_date, as: :string, input_html: { type: "date" } diff --git a/app/views/users/relatives/edit.html.slim b/app/views/users/relatives/edit.html.slim index 552f0a02a9..cb6a3e26fe 100644 --- a/app/views/users/relatives/edit.html.slim +++ b/app/views/users/relatives/edit.html.slim @@ -2,11 +2,11 @@ .card .card-body - = simple_form_for @user, url: relative_path(@user) do |f| - = render "model_errors", model: @user + = simple_form_for @form, url: relative_path(@form.user), as: :user do |f| + = render "model_errors", model: @form = render "form_fields", f: f .d-flex.justify-content-between - = link_to "Supprimer", relative_path(@user), method: :delete, class: "btn btn-outline-danger", data: { confirm: "Confirmez-vous la suppression de ce proche ?"} + = link_to "Supprimer", relative_path(@form.user), method: :delete, class: "btn btn-outline-danger", data: { confirm: "Confirmez-vous la suppression de ce proche ?"} .d-flex.justify-content-end = link_to "Annuler", users_informations_path, class: "btn btn-link" diff --git a/app/views/users/relatives/new.html.slim b/app/views/users/relatives/new.html.slim index 501b5c9382..fa934ad97a 100644 --- a/app/views/users/relatives/new.html.slim +++ b/app/views/users/relatives/new.html.slim @@ -1,8 +1,8 @@ - content_for(:title) do h1.rdv-color-white Ajouter un proche -= simple_form_for @user, url: relatives_path, remote: true, data: { modal: true } do |f| - = render "model_errors", model: @user += simple_form_for @form, url: relatives_path, remote: true, data: { modal: true }, as: :user do |f| + = render "model_errors", model: @form = render "form_fields", f: f .row .col.rdv-text-align-right diff --git a/spec/controllers/users/relatives_controller_spec.rb b/spec/controllers/users/relatives_controller_spec.rb index e5251e88fc..2cca9dc8a6 100644 --- a/spec/controllers/users/relatives_controller_spec.rb +++ b/spec/controllers/users/relatives_controller_spec.rb @@ -17,11 +17,6 @@ it "returns a success response" do expect(response).to be_successful end - - it "assigns relative" do - expect(response.body).to include("Modifier un proche") - expect(assigns(:user)).to eq(relative) - end end describe "GET #new" do @@ -32,11 +27,6 @@ it "returns a success response" do expect(response).to be_successful end - - it "assigns a new user" do - expect(response.body).to include("Ajouter un proche") - expect(assigns(:user)).to be_a_new(User) - end end describe "POST #create" do @@ -51,11 +41,8 @@ it "creates a new User" do expect { subject }.to change(User, :count).by(1) - end - - it "set organisation_ids for relative" do - subject - expect(assigns(:user).organisation_ids).to eq(user.organisation_ids) + created_user = User.last + expect(created_user.organisation_ids).to eq(user.organisation_ids) end it "redirects to user informations with the newly created user id as a param" do