From 6ddbda34a2ee066771223467f3f0202df2483bd7 Mon Sep 17 00:00:00 2001 From: Colin Darie Date: Tue, 28 Mar 2023 15:38:13 +0200 Subject: [PATCH] fix(avis): don't list question_answer radios on empty question --- app/models/avis.rb | 7 +++++++ app/views/experts/avis/instruction.html.haml | 2 +- app/views/shared/avis/_form.html.haml | 2 +- spec/models/avis_spec.rb | 12 ++++++++++++ .../experts/avis/instruction.html.haml_spec.rb | 13 +++++++++++++ 5 files changed, 34 insertions(+), 2 deletions(-) diff --git a/app/models/avis.rb b/app/models/avis.rb index 8046edd2d..2f99f66f3 100644 --- a/app/models/avis.rb +++ b/app/models/avis.rb @@ -47,6 +47,7 @@ class Avis < ApplicationRecord validates :piece_justificative_file, size: { less_than: FILE_MAX_SIZE } validates :introduction_file, size: { less_than: FILE_MAX_SIZE } before_validation -> { sanitize_email(:email) } + before_validation -> { strip_attribute(:question_label) } default_scope { joins(:dossier) } scope :with_answer, -> { where.not(answer: nil) } @@ -111,4 +112,10 @@ class Avis < ApplicationRecord return false if !remindable_by?(revocator) || answer.present? update!(reminded_at: Time.zone.now) end + + private + + def strip_attribute(attribute) + self[attribute] = self[attribute]&.strip&.presence + end end diff --git a/app/views/experts/avis/instruction.html.haml b/app/views/experts/avis/instruction.html.haml index 148a813d1..af5ae7909 100644 --- a/app/views/experts/avis/instruction.html.haml +++ b/app/views/experts/avis/instruction.html.haml @@ -22,7 +22,7 @@ = render Attachment::DeleteFormComponent.new = form_for @avis, url: expert_avis_path(@avis.procedure, @avis), html: { data: { controller: 'persisted-form', persisted_form_key_value: dom_id(@avis) }, multipart: true } do |f| - - if @avis.question_label + - if @avis.question_label.present? .fr-form-group %fieldset.fr-fieldset.fr-fieldset--inline %legend#radio-inline-legend.fr-fieldset__legend.fr-text--regular diff --git a/app/views/shared/avis/_form.html.haml b/app/views/shared/avis/_form.html.haml index c8d66f1d1..d38788765 100644 --- a/app/views/shared/avis/_form.html.haml +++ b/app/views/shared/avis/_form.html.haml @@ -1,4 +1,4 @@ -%section.ask-avis +%section.ask-avis.fr-mb-4w %h1.tab-title Inviter des personnes à donner leur avis %p.avis-notice Les invités pourront consulter le dossier, donner un avis et contribuer au fil de messagerie. Ils ne pourront pas modifier le dossier. - if @dossier.procedure.experts_require_administrateur_invitation diff --git a/spec/models/avis_spec.rb b/spec/models/avis_spec.rb index 6394c2ba4..3bb4c4da7 100644 --- a/spec/models/avis_spec.rb +++ b/spec/models/avis_spec.rb @@ -149,4 +149,16 @@ RSpec.describe Avis, type: :model do end end end + + describe "question_label cleanup" do + it "nullify empty" do + avis = create(:avis, question_label: " ") + expect(avis.question_label).to be_nil + end + + it "strip" do + avis = create(:avis, question_label: "my question ") + expect(avis.question_label).to eq("my question") + end + end end diff --git a/spec/views/experts/avis/instruction.html.haml_spec.rb b/spec/views/experts/avis/instruction.html.haml_spec.rb index b360f9e82..b1d6bd940 100644 --- a/spec/views/experts/avis/instruction.html.haml_spec.rb +++ b/spec/views/experts/avis/instruction.html.haml_spec.rb @@ -3,6 +3,7 @@ describe 'experts/avis/instruction.html.haml', type: :view do let(:claimant) { create(:instructeur) } let(:procedure) { create(:procedure) } let(:experts_procedure) { create(:experts_procedure, expert: expert, procedure: procedure) } + let(:confidentiel) { false } let(:avis) { create(:avis, confidentiel: confidentiel, claimant: claimant, experts_procedure: experts_procedure) } before do @@ -23,4 +24,16 @@ describe 'experts/avis/instruction.html.haml', type: :view do let(:confidentiel) { false } it { is_expected.to have_text("Cet avis est partagé avec les autres experts") } end + + context 'when the avis has a question' do + let(:avis) { create(:avis, question_label: "is it useful?", claimant: claimant, experts_procedure: experts_procedure) } + + it { is_expected.to have_text(avis.question_label) } + it { is_expected.to have_unchecked_field("oui") } + end + + context 'when the avis has no question' do + let(:avis) { create(:avis, question_label: "", claimant: claimant, experts_procedure: experts_procedure) } + it { is_expected.not_to have_unchecked_field("oui") } + end end