Skip to content

Commit

Permalink
Merge pull request #11210 from demarches-simplifiees/fix-10960
Browse files Browse the repository at this point in the history
ETQ Administrateur, je veux que le libellé par défaut d'un nouveau champ soit adapté au type de champ choisi
  • Loading branch information
mmagn authored Jan 17, 2025
2 parents c5852ac + f78eaba commit 7462bc4
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def champ_libelle
if annotations?
"Nouvelle annotation"
else
"Nouveau champ"
nil
end
end
end
22 changes: 18 additions & 4 deletions app/models/type_de_champ.rb
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,6 @@ def self.dump(options)

serialize :condition, LogicSerializer

after_initialize :set_dynamic_type
after_create :populate_stable_id

attr_reader :dynamic_type

scope :public_only, -> { where(private: false) }
Expand Down Expand Up @@ -209,11 +206,15 @@ def self.dump(options)
allow_blank: true
}

after_initialize :set_dynamic_type
after_create :populate_stable_id

before_validation :check_mandatory
before_validation :set_default_libelle, if: -> { type_champ_changed? }
before_validation :normalize_libelle
before_validation :set_drop_down_list_options, if: -> { type_champ_changed? }

before_save :remove_attachment, if: -> { type_champ_changed? }
before_validation :set_drop_down_list_options, if: -> { type_champ_changed? }

def valid?(context = nil)
super
Expand All @@ -235,6 +236,19 @@ def type_champ=(value)
set_dynamic_type
end

def set_default_libelle
libelle_was_default = libelle == default_libelle(type_champ_was)
self.libelle = default_libelle(type_champ) if libelle.blank? || libelle_was_default
end

def default_libelle(type_champ)
return if type_champ.blank?

I18n.t(type_champ,
scope: [:activerecord, :attributes, :type_de_champ, :default_libelle],
default: I18n.t(type_champ, scope: [:activerecord, :attributes, :type_de_champ, :type_champs]))
end

def params_for_champ
{
private: private?,
Expand Down
18 changes: 14 additions & 4 deletions config/locales/models/type_de_champ/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,23 @@ en:
cnaf: 'Data from Caisse nationale des allocations familiales'
dgfip: 'Data from Direction générale des Finances publiques'
pole_emploi: 'Pôle emploi status'
mesri: "Data from Ministère de l’Enseignement Supérieur, de la Recherche et de l’Innovation"
epci: "EPCI"
cojo: "Accreditation Paris 2024"
mesri: 'Data from Ministère de l’Enseignement Supérieur, de la Recherche et de l’Innovation'
epci: 'EPCI'
cojo: 'Accreditation Paris 2024'
expression_reguliere: 'Regular expression'
referentiel: 'External data to configure (advanced)'
default_libelle:
annuaire_education: Educational institution
dossier_link: File number submitted on %{app_name}
epci: Public establishments for intercommunal cooperation (EPCI)
iban: IBAN
siret: SIRET number
titre_identite: Identity document
phone: Phone number
rna: RNA number
rnf: RNF number
errors:
type_de_champ:
attributes:
header_section_level:
gap_error: "An header section with %{level} is missing."
gap_error: 'An header section with %{level} is missing.'
13 changes: 13 additions & 0 deletions config/locales/models/type_de_champ/fr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,19 @@ fr:
cojo: "Accréditation Paris 2024"
expression_reguliere: 'Expression régulière'
referentiel: 'Référentiel à configurer (avancé)'
default_libelle:
annuaire_education: Établissement
communes: Commune
departements: Département
dossier_link: Numéro de dossier déposé sur %{app_name}
epci: Établissements publics de coopération intercommunale (EPCI)
iban: IBAN
regions: Région
siret: Numéro SIRET
titre_identite: Pièce d'identité
phone: Numéro de téléphone
rna: Numéro RNA
rnf: Numéro RNF
errors:
type_de_champ:
attributes:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ def morpheds

context "validate" do
let(:type_champ) { TypeDeChamp.type_champs.fetch(:text) }
let(:params) { default_params.deep_merge(type_de_champ: { libelle: '' }) }
let(:params) { default_params.deep_merge(type_de_champ: { type_champ: nil }) }

it do
is_expected.to have_http_status(:ok)
expect(assigns(:coordinate)).to be_nil
expect(flash.alert).to eq(["Le champ « Libelle » doit être rempli"])
expect(flash.alert).to eq(["Le champ « Type champ » doit être rempli"])
end
end
end
Expand Down
7 changes: 0 additions & 7 deletions spec/models/procedure_revision_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,6 @@
end
end

context 'when a libelle is missing' do
let(:text_params) { { type_champ: :text, libelle: 'text', after_stable_id: procedure.draft_revision.types_de_champ_private.last.id } }
let(:tdc_params) { text_params.except(:libelle) }

it { expect(subject.errors.full_messages).to eq(["Le champ « Libelle » doit être rempli"]) }
end

context 'when a parent is incorrect' do
let(:text_params) { { type_champ: :text, libelle: 'text', after_stable_id: procedure.draft_revision.types_de_champ_private.last.id } }
let(:tdc_params) { text_params.merge(parent_id: 123456789) }
Expand Down
19 changes: 19 additions & 0 deletions spec/models/type_de_champ_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,25 @@ def never_valid
it { expect(create(:type_de_champ, libelle: " fix me ").libelle).to eq("fix me") }
end

describe '#set_default_libelle' do
let(:type_de_champ) { create(:type_de_champ, type_champ: :header_section, libelle: libelle) }
let(:libelle) { nil }

it { expect(type_de_champ.libelle).to eq("Titre de section") }

context "when the type champ is changed" do
before { type_de_champ.update(type_champ: :dossier_link) }

it { expect(type_de_champ.libelle).to eq("Numéro de dossier déposé sur %{app_name}") }

context "when the libelle is customized" do
let(:libelle) { "Customized libelle" }

it { expect(type_de_champ.libelle).to eq("Customized libelle") }
end
end
end

describe '#safe_filename' do
subject { build(:type_de_champ, libelle:).libelle_as_filename }

Expand Down

0 comments on commit 7462bc4

Please sign in to comment.