From 02743e42cc49c331a28f8d89cb47ee1a0c787621 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89tienne=20Charignon?= Date: Wed, 13 Nov 2024 17:46:18 +0100 Subject: [PATCH] =?UTF-8?q?Supprime=20les=20espaces=20=C3=A9ventuellement?= =?UTF-8?q?=20saisis=20dans=20le=20SIRET?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models/structure.rb | 7 +++++++ spec/models/structure_spec.rb | 11 +++++++++++ 2 files changed, 18 insertions(+) diff --git a/app/models/structure.rb b/app/models/structure.rb index c0e24875e..dd15e626b 100644 --- a/app/models/structure.rb +++ b/app/models/structure.rb @@ -24,6 +24,7 @@ class Structure < ApplicationRecord end end + before_validation :retire_espaces_siret after_validation :geocode, if: ->(s) { s.code_postal.present? and s.code_postal_changed? } after_create :programme_email_relance @@ -90,4 +91,10 @@ def verifie_siret_ou_siren errors.add(:siret, :invalid) end + + def retire_espaces_siret + return if siret.blank? + + self.siret = siret.gsub(/[[:space:]]/, '') + end end diff --git a/spec/models/structure_spec.rb b/spec/models/structure_spec.rb index 080f74b1a..d9bbbb05f 100644 --- a/spec/models/structure_spec.rb +++ b/spec/models/structure_spec.rb @@ -128,6 +128,17 @@ def mock_geo_api(departement, code_region, region) it { expect(structure.errors[:siret]).to be_blank } end + context 'quand il est un siret valide avec des espaces' do + let(:structure) { build :structure, siret: '1234567890 1234' } + + before { structure.valid? } + + it do + expect(structure.errors[:siret]).to be_blank + expect(structure.siret).to eq('12345678901234') + end + end + context 'quand il est un siret valide (14 chiffres)' do let(:structure) { build :structure, siret: '12345678901234' }