add contact information model

This commit is contained in:
Christophe Robillard 2023-08-11 10:49:16 +02:00
parent 202e310d03
commit 46dec40543
5 changed files with 104 additions and 1 deletions

View file

@ -0,0 +1,11 @@
class ContactInformation < ApplicationRecord
belongs_to :groupe_instructeur
validates :nom, presence: { message: 'doit être renseigné' }, allow_nil: false
validates :nom, uniqueness: { scope: :groupe_instructeur, message: 'existe déjà' }
validates :email, format: { with: Devise.email_regexp, message: "n'est pas valide" }, presence: { message: 'doit être renseigné' }, allow_nil: false
validates :telephone, phone: { possible: true, allow_blank: false }
validates :horaires, presence: { message: 'doivent être renseignés' }, allow_nil: false
validates :adresse, presence: { message: 'doit être renseignée' }, allow_nil: false
validates :groupe_instructeur, presence: { message: 'doit être renseigné' }, allow_nil: false
end

View file

@ -13,6 +13,7 @@ class GroupeInstructeur < ApplicationRecord
has_and_belongs_to_many :bulk_messages, dependent: :destroy
has_one :defaut_procedure, -> { with_discarded }, class_name: 'Procedure', foreign_key: :defaut_groupe_instructeur_id, dependent: :nullify, inverse_of: :defaut_groupe_instructeur
has_one :contact_information
validates :label, presence: true, allow_nil: false
validates :label, uniqueness: { scope: :procedure }

View file

@ -0,0 +1,15 @@
class CreateContactInformations < ActiveRecord::Migration[7.0]
def change
create_table :contact_informations do |t|
t.belongs_to :groupe_instructeur, null: false, foreign_key: true
t.text :adresse, null: false
t.string :email, null: false
t.text :horaires, null: false
t.string :nom, null: false
t.string :telephone, null: false
t.timestamps
end
add_index :contact_informations, [:groupe_instructeur_id, :nom], unique: true, name: 'index_contact_informations_on_gi_and_nom'
end
end

View file

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.0].define(version: 2023_08_02_161011) do
ActiveRecord::Schema[7.0].define(version: 2023_08_09_151357) do
# These are extensions that must be enabled in order to support this database
enable_extension "pgcrypto"
enable_extension "plpgsql"
@ -612,6 +612,19 @@ ActiveRecord::Schema[7.0].define(version: 2023_08_02_161011) do
t.index ["source"], name: "index_geo_areas_on_source"
end
create_table "contact_informations", force: :cascade do |t|
t.text "adresse", null: false
t.datetime "created_at", null: false
t.string "email", null: false
t.bigint "groupe_instructeur_id", null: false
t.text "horaires", null: false
t.string "nom", null: false
t.string "telephone", null: false
t.datetime "updated_at", null: false
t.index ["groupe_instructeur_id", "nom"], name: "index_contact_informations_on_gi_and_nom", unique: true
t.index ["groupe_instructeur_id"], name: "index_contact_informations_on_groupe_instructeur_id"
end
create_table "groupe_instructeurs", force: :cascade do |t|
t.boolean "closed", default: false
t.datetime "created_at", precision: 6, null: false
@ -1044,6 +1057,7 @@ ActiveRecord::Schema[7.0].define(version: 2023_08_02_161011) do
add_foreign_key "commentaires", "dossiers"
add_foreign_key "commentaires", "experts"
add_foreign_key "commentaires", "instructeurs"
add_foreign_key "contact_informations", "groupe_instructeurs"
add_foreign_key "dossier_assignments", "dossiers"
add_foreign_key "dossier_batch_operations", "batch_operations"
add_foreign_key "dossier_batch_operations", "dossiers"

View file

@ -0,0 +1,62 @@
describe ContactInformation, type: :model do
describe 'validation' do
let(:gi) { create(:groupe_instructeur) }
let(:params) do
{
nom: 'service des jardins',
email: 'super@email.com',
telephone: '012345678',
horaires: 'du lundi au vendredi',
adresse: '12 rue des schtroumpfs',
groupe_instructeur_id: gi.id
}
end
subject { ContactInformation.new(params) }
it { expect(subject).to be_valid }
it 'should forbid invalid phone numbers' do
invalid_phone_numbers = ["1", "Néant", "01 60 50 40 30 20"]
invalid_phone_numbers.each do |tel|
subject.telephone = tel
expect(subject).not_to be_valid
end
end
it 'should not accept no phone numbers' do
subject.telephone = nil
expect(subject).not_to be_valid
end
it 'should accept valid phone numbers' do
valid_phone_numbers = ["3646", "273115", "0160376983", "01 60 50 40 30 ", "+33160504030"]
valid_phone_numbers.each do |tel|
subject.telephone = tel
expect(subject).to be_valid
end
end
context 'when a contact information already exists' do
before { ContactInformation.create(params) }
context 'checks uniqueness of administrateur, name couple' do
it { expect(ContactInformation.create(params)).not_to be_valid }
end
end
context 'of nom' do
it 'should be set' do
expect(ContactInformation.new(params.except(:nom))).not_to be_valid
end
end
context 'of groupe instructeur' do
it 'should be set' do
expect(ContactInformation.new(params.except(:groupe_instructeur_id))).not_to be_valid
end
end
end
end