Skip to content

Commit

Permalink
introduction form object pour les proches + ajout de la validation
Browse files Browse the repository at this point in the history
  • Loading branch information
adipasquale committed Jan 13, 2025
1 parent 7a72d04 commit 9501ebe
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 42 deletions.
51 changes: 31 additions & 20 deletions app/controllers/users/relatives_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
34 changes: 34 additions & 0 deletions app/form_models/relative_user_form.rb
Original file line number Diff line number Diff line change
@@ -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
6 changes: 4 additions & 2 deletions app/views/users/relatives/_form_fields.html.slim
Original file line number Diff line number Diff line change
@@ -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" }
6 changes: 3 additions & 3 deletions app/views/users/relatives/edit.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions app/views/users/relatives/new.html.slim
Original file line number Diff line number Diff line change
@@ -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
Expand Down
17 changes: 2 additions & 15 deletions spec/controllers/users/relatives_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 9501ebe

Please sign in to comment.