diff --git a/app/models/anonymisation/structure.rb b/app/models/anonymisation/structure.rb index 335599467..27f5207c9 100644 --- a/app/models/anonymisation/structure.rb +++ b/app/models/anonymisation/structure.rb @@ -4,7 +4,7 @@ module Anonymisation class Structure < Anonymisation::Base def anonymise super do |structure| - structure.code_postal = 'Non communiqué' if structure.code_postal.nil? + structure.code_postal = ::Structure::TYPE_NON_COMMUNIQUE if structure.code_postal.nil? structure.nom = FFaker::Company.name end end diff --git a/app/models/structure.rb b/app/models/structure.rb index 42a04f9cc..71930a1bc 100644 --- a/app/models/structure.rb +++ b/app/models/structure.rb @@ -6,10 +6,15 @@ class Structure < ApplicationRecord organisme_formation orientation_scolaire cap_emploi e2c SMA autre ].freeze + TYPE_NON_COMMUNIQUE = 'non_communique' + validates :nom, :code_postal, :type_structure, presence: true - validates :type_structure, inclusion: { in: (TYPES_STRUCTURES + ['non_communique']) } + validates :type_structure, inclusion: { in: (TYPES_STRUCTURES + [TYPE_NON_COMMUNIQUE]) } + validates :code_postal, numericality: { only_integer: true }, length: { is: 5 }, + unless: proc { |s| s.code_postal == TYPE_NON_COMMUNIQUE } - auto_strip_attributes :nom, :code_postal, squish: true + auto_strip_attributes :nom, squish: true + auto_strip_attributes :code_postal, delete_whitespaces: true geocoded_by :code_postal, state: :region, params: { countrycodes: 'fr' } do |obj, resultats| if (resultat = resultats.first) diff --git a/spec/integrations/anonymisation/structure_spec.rb b/spec/integrations/anonymisation/structure_spec.rb index 583bfe4f6..872385c37 100644 --- a/spec/integrations/anonymisation/structure_spec.rb +++ b/spec/integrations/anonymisation/structure_spec.rb @@ -34,7 +34,7 @@ Anonymisation::Structure.new(structure).anonymise structure.reload - expect(structure.code_postal).to eql('Non communiqué') + expect(structure.code_postal).to eql('non_communique') end end end diff --git a/spec/models/structure_spec.rb b/spec/models/structure_spec.rb index 687bb1206..b28ece351 100644 --- a/spec/models/structure_spec.rb +++ b/spec/models/structure_spec.rb @@ -6,6 +6,9 @@ it { should validate_presence_of(:nom) } it { should validate_presence_of(:code_postal) } it { should validate_presence_of(:type_structure) } + it { should validate_numericality_of(:code_postal) } + it { should validate_length_of(:code_postal).is_equal_to(5) } + it do types_structures = %w[ mission_locale pole_emploi SIAE service_insertion_collectivite CRIA @@ -19,6 +22,16 @@ .display_name).to eql('eva - 75012') end + it "Le code postal ne peut pas comporter d'espaces" do + structure = described_class.create(nom: 'eva', code_postal: '75 01 2 ') + expect(structure.code_postal).to eq('75012') + end + + it 'Accepte les codes postaux commençant par 0' do + structure = described_class.create(nom: 'eva', code_postal: '01000') + expect(structure.code_postal).to eq('01000') + end + describe 'géolocalisation à la validation' do let(:structure) { Structure.new code_postal: '75012' } before do