Skip to content

Commit

Permalink
Ajoute un champ Siret aux structures
Browse files Browse the repository at this point in the history
  • Loading branch information
etienneCharignon committed Jul 11, 2024
1 parent 234a571 commit b0e94fd
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 3 deletions.
2 changes: 1 addition & 1 deletion app/admin/structures_administratives.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
ActiveAdmin.register StructureAdministrative do
menu parent: 'Terrain', if: proc { current_compte.anlci? }

permit_params :nom, :parent_id
permit_params :nom, :parent_id, :siret

filter :nom
filter :created_at
Expand Down
2 changes: 1 addition & 1 deletion app/admin/structures_locales.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
menu parent: 'Terrain', if: proc { current_compte.anlci? }
actions :all

permit_params :nom, :type_structure, :code_postal, :parent_id
permit_params :nom, :type_structure, :code_postal, :parent_id, :siret

filter :nom
filter :type_structure,
Expand Down
11 changes: 11 additions & 0 deletions app/models/structure.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class Structure < ApplicationRecord
case_sensitive: false,
scope: :code_postal
}
validates :siret, numericality: { only_integer: true, allow_blank: true }
validate :verifie_siret_ou_siren

auto_strip_attributes :nom, squish: true

Expand Down Expand Up @@ -79,4 +81,13 @@ def programme_email_relance
.set(wait: 7.days)
.perform_later(id)
end

private

def verifie_siret_ou_siren
return if siret.blank?
return if siret.size == 14 || siret.size == 9

errors.add(:siret, :invalid)
end
end
1 change: 1 addition & 0 deletions app/views/admin/structures_locales/_form.html.arb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ div class: 'panel nouvelle_structure' do
f.input :nom
f.input :type_structure, as: :select, collection: collection_types_structures
f.input :code_postal
f.input :siret
if can?(:manage, Compte)
f.input :parent_id, as: :select, collection: StructureAdministrative.all
end
Expand Down
1 change: 1 addition & 0 deletions app/views/nouvelles_structures/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
<%= structure.input :nom, placeholder: t('.placeholders.structure.nom') %>
<%= structure.input :type_structure, as: :select, collection: collection_types_structures %>
<%= structure.input :code_postal, placeholder: t('.placeholders.structure.code_postal') %>
<%= structure.input :siret %>
<% end %>
</div>
<% end %>
Expand Down
3 changes: 3 additions & 0 deletions config/locales/models/structure.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ fr:
attributes:
nom:
taken: Cette structure existe
siret:
invalid: Le numéro est invalide
models:
structure_administrative:
one: Structure administrative
Expand All @@ -19,6 +21,7 @@ fr:
updated_at: Mise à jour le
nom: Nom de structure
code_postal: Code Postal
siret: Siret ou Siren
region: Région
date_derniere_evaluation: Dernière évaluation
structure_referente: Structure référente
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20240704141851_ajoute_colonne_siret_a_structure.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AjouteColonneSiretAStructure < ActiveRecord::Migration[7.0]
def change
add_column :structures, :siret, :string
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.0].define(version: 2024_06_24_120736) do
ActiveRecord::Schema[7.0].define(version: 2024_07_04_141851) do
# These are extensions that must be enabled in order to support this database
enable_extension "pgcrypto"
enable_extension "plpgsql"
Expand Down Expand Up @@ -368,6 +368,7 @@
t.string "type"
t.datetime "deleted_at"
t.string "ancestry", null: false, collation: "C"
t.string "siret"
t.index ["ancestry"], name: "index_structures_on_ancestry"
t.index ["deleted_at"], name: "index_structures_on_deleted_at"
t.index ["latitude", "longitude"], name: "index_structures_on_latitude_and_longitude"
Expand Down
Binary file modified docs/erd.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
38 changes: 38 additions & 0 deletions spec/models/structure_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
describe Structure, type: :model do
it { is_expected.to validate_presence_of(:nom) }
it { is_expected.to validate_uniqueness_of(:nom).scoped_to(:code_postal).case_insensitive }
it { is_expected.to validate_numericality_of(:siret) }

describe 'Ancestry primary key format' do
it { expect('uuid invalide').not_to match(Ancestry.default_primary_key_format) }
Expand Down Expand Up @@ -118,4 +119,41 @@ def mock_geo_api(departement, code_region, region)
.to have_enqueued_job(RelanceStructureSansCampagneJob).exactly(1)
end
end

describe 'validation du champ siret' do
context 'quand il est vide' do
let(:structure) { build :structure }

before { structure.valid? }

it { expect(structure.errors[:siret]).to be_blank }
end

context 'quand il est un siret valide (14 chiffres)' do
let(:structure) { build :structure, siret: '12345678901234' }

before { structure.valid? }

it { expect(structure.errors[:siret]).to be_blank }
end

context 'quand il est un siren valide (9 chiffres)' do
let(:structure) { build :structure, siret: '123456789' }

before { structure.valid? }

it { expect(structure.errors[:siret]).to be_blank }
end

context 'quand il est invalide' do
let(:structure) { build :structure, siret: '12345678' }

before { structure.valid? }

it do
expect(structure.errors[:siret])
.to include(I18n.t('activerecord.errors.models.structure.attributes.siret.invalid'))
end
end
end
end

0 comments on commit b0e94fd

Please sign in to comment.