Service: create model

This commit is contained in:
simon lehericey 2018-04-17 16:11:49 +02:00
parent c62cddc389
commit fc17b68dc1
9 changed files with 107 additions and 0 deletions

View file

@ -9,6 +9,7 @@ class Administrateur < ApplicationRecord
has_many :procedures
has_many :administrateurs_procedures
has_many :admin_procedures, through: :administrateurs_procedures, source: :procedure
has_many :services
before_validation -> { sanitize_email(:email) }
before_save :ensure_api_token

View file

@ -11,6 +11,7 @@ class Procedure < ApplicationRecord
belongs_to :administrateur
belongs_to :parent_procedure, class_name: 'Procedure'
belongs_to :service
has_many :assign_to, dependent: :destroy
has_many :administrateurs_procedures

20
app/models/service.rb Normal file
View file

@ -0,0 +1,20 @@
class Service < ApplicationRecord
has_many :procedures
belongs_to :administrateur
enum type_organisme: {
administration_centrale: 'administration_centrale',
association: 'association',
commune: 'commune',
departement: 'departement',
etablissement_enseignement: 'etablissement_enseignement',
prefecture: 'prefecture',
region: 'region',
autre: 'autre'
}
validates :nom, presence: { message: 'doit être renseigné' }, allow_nil: false
validates :nom, uniqueness: { scope: :administrateur, message: 'existe déjà' }
validates :type_organisme, presence: { message: 'doit être renseigné' }, allow_nil: false
validates :administrateur, presence: { message: 'doit être renseigné' }, allow_nil: false
end

View file

@ -0,0 +1,10 @@
class CreateServices < ActiveRecord::Migration[5.2]
def change
create_table :services do |t|
t.string :type_organisme, null: false
t.string :nom, null: false
t.timestamps
end
end
end

View file

@ -0,0 +1,5 @@
class AddServiceToProcedures < ActiveRecord::Migration[5.2]
def change
add_reference :procedures, :service, foreign_key: true
end
end

View file

@ -0,0 +1,5 @@
class AddAdministrateurToServices < ActiveRecord::Migration[5.2]
def change
add_reference :services, :administrateur, foreign_key: true
end
end

View file

@ -0,0 +1,5 @@
class AddUniqueIndexToServices < ActiveRecord::Migration[5.2]
def change
add_index :services, [:administrateur_id, :nom], unique: true
end
end

View file

@ -466,8 +466,10 @@ ActiveRecord::Schema.define(version: 2018_05_15_135415) do
t.bigint "parent_procedure_id"
t.datetime "test_started_at"
t.string "aasm_state", default: "brouillon"
t.bigint "service_id"
t.index ["hidden_at"], name: "index_procedures_on_hidden_at"
t.index ["parent_procedure_id"], name: "index_procedures_on_parent_procedure_id"
t.index ["service_id"], name: "index_procedures_on_service_id"
end
create_table "quartier_prioritaires", id: :serial, force: :cascade do |t|
@ -511,6 +513,16 @@ ActiveRecord::Schema.define(version: 2018_05_15_135415) do
t.index ["entreprise_id"], name: "index_rna_informations_on_entreprise_id"
end
create_table "services", force: :cascade do |t|
t.string "type_organisme", null: false
t.string "nom", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.bigint "administrateur_id"
t.index ["administrateur_id", "nom"], name: "index_services_on_administrateur_id_and_nom", unique: true
t.index ["administrateur_id"], name: "index_services_on_administrateur_id"
end
create_table "types_de_champ", id: :serial, force: :cascade do |t|
t.string "libelle"
t.string "type_champ"
@ -576,8 +588,10 @@ ActiveRecord::Schema.define(version: 2018_05_15_135415) do
add_foreign_key "procedure_paths", "administrateurs"
add_foreign_key "procedure_paths", "procedures"
add_foreign_key "procedure_presentations", "assign_tos"
add_foreign_key "procedures", "services"
add_foreign_key "received_mails", "procedures"
add_foreign_key "refused_mails", "procedures"
add_foreign_key "services", "administrateurs"
add_foreign_key "without_continuation_mails", "procedures"
create_view "searches", sql_definition: <<-SQL

View file

@ -0,0 +1,46 @@
describe Service, type: :model do
describe 'validation' do
let(:administrateur) { create(:administrateur) }
let(:params) do
{
nom: 'service des jardins',
type_organisme: 'commune',
administrateur_id: administrateur.id
}
end
it { expect(Service.new(params).valid?).to be_truthy }
context 'when a first service exists' do
before { Service.create(params) }
context 'checks uniqueness of administrateur, name couple' do
it { expect(Service.create(params).valid?).to be_falsey }
end
end
context 'of type_organisme' do
it 'should be set' do
expect(Service.new(params.except(:type_organisme)).valid?).to be_falsey
end
end
context 'of nom' do
it 'should be set' do
expect(Service.new(params.except(:nom)).valid?).to be_falsey
end
end
context 'of administrateur' do
it 'should be set' do
expect(Service.new(params.except(:administrateur_id)).valid?).to be_falsey
end
end
context 'of type_organisme' do
it 'should belong to the enum' do
expect{ Service.new(params.merge(type_organisme: 'choucroute')) }.to raise_error(ArgumentError)
end
end
end
end