From 53a48f963d1f56164d1666a4da6e5a130fb6e50c Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Mon, 22 Jul 2024 17:01:32 +0200 Subject: [PATCH] tdc: add libelle_as_filename --- app/models/type_de_champ.rb | 6 ++++++ spec/models/type_de_champ_spec.rb | 8 ++++++++ 2 files changed, 14 insertions(+) diff --git a/app/models/type_de_champ.rb b/app/models/type_de_champ.rb index bcf24c0f1..4033b0f09 100644 --- a/app/models/type_de_champ.rb +++ b/app/models/type_de_champ.rb @@ -676,6 +676,12 @@ class TypeDeChamp < ApplicationRecord end end + def libelle_as_filename + libelle.gsub(/[[:space:]]+/, ' ') + .truncate(30, omission: '', separator: ' ') + .parameterize + end + class << self def champ_value(type_champ, champ) dynamic_type_class = type_champ_to_class_name(type_champ).constantize diff --git a/spec/models/type_de_champ_spec.rb b/spec/models/type_de_champ_spec.rb index 95cd2bb44..8cacac706 100644 --- a/spec/models/type_de_champ_spec.rb +++ b/spec/models/type_de_champ_spec.rb @@ -302,4 +302,12 @@ describe TypeDeChamp do it { expect(create(:type_de_champ, :header_section, libelle: " 2.3 Test").libelle).to eq("2.3 Test") } it { expect(create(:type_de_champ, libelle: " fix me ").libelle).to eq("fix me") } end + + describe '#safe_filename' do + subject { build(:type_de_champ, libelle:).libelle_as_filename } + + let(:libelle) { " #/🐉 1 très intéressant Bilan " } + + it { is_expected.to eq("1-tres-interessant-bilan") } + end end