diff --git a/app/models/administrateur.rb b/app/models/administrateur.rb index 22ad5db10..fbf62a854 100644 --- a/app/models/administrateur.rb +++ b/app/models/administrateur.rb @@ -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 diff --git a/app/models/procedure.rb b/app/models/procedure.rb index 218081808..2744623dd 100644 --- a/app/models/procedure.rb +++ b/app/models/procedure.rb @@ -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 diff --git a/app/models/service.rb b/app/models/service.rb new file mode 100644 index 000000000..f0f2c9111 --- /dev/null +++ b/app/models/service.rb @@ -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 diff --git a/db/migrate/20180416120759_create_services.rb b/db/migrate/20180416120759_create_services.rb new file mode 100644 index 000000000..2af2eca7f --- /dev/null +++ b/db/migrate/20180416120759_create_services.rb @@ -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 diff --git a/db/migrate/20180416122152_add_service_to_procedures.rb b/db/migrate/20180416122152_add_service_to_procedures.rb new file mode 100644 index 000000000..79c3e919d --- /dev/null +++ b/db/migrate/20180416122152_add_service_to_procedures.rb @@ -0,0 +1,5 @@ +class AddServiceToProcedures < ActiveRecord::Migration[5.2] + def change + add_reference :procedures, :service, foreign_key: true + end +end diff --git a/db/migrate/20180419093104_add_administrateur_to_services.rb b/db/migrate/20180419093104_add_administrateur_to_services.rb new file mode 100644 index 000000000..a60ed5356 --- /dev/null +++ b/db/migrate/20180419093104_add_administrateur_to_services.rb @@ -0,0 +1,5 @@ +class AddAdministrateurToServices < ActiveRecord::Migration[5.2] + def change + add_reference :services, :administrateur, foreign_key: true + end +end diff --git a/db/migrate/20180423102102_add_unique_index_to_services.rb b/db/migrate/20180423102102_add_unique_index_to_services.rb new file mode 100644 index 000000000..50feedb51 --- /dev/null +++ b/db/migrate/20180423102102_add_unique_index_to_services.rb @@ -0,0 +1,5 @@ +class AddUniqueIndexToServices < ActiveRecord::Migration[5.2] + def change + add_index :services, [:administrateur_id, :nom], unique: true + end +end diff --git a/db/schema.rb b/db/schema.rb index 9594d9709..57f898afe 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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 diff --git a/spec/models/service_spec.rb b/spec/models/service_spec.rb new file mode 100644 index 000000000..64802d55e --- /dev/null +++ b/spec/models/service_spec.rb @@ -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