From dc18e7da6fb0a8153b1c4469405dcac25cd9703d Mon Sep 17 00:00:00 2001 From: Xavier J Date: Tue, 10 Nov 2015 10:23:15 +0100 Subject: [PATCH] Create relation between Administrateur, Gestionnaire and Procedure --- app/models/administrateur.rb | 3 +++ app/models/gestionnaire.rb | 2 ++ app/models/procedure.rb | 2 ++ ...59_create_reference_admninistrateur_to_procedure.rb | 5 +++++ ...create_reference_admninistrateur_to_gestionnaire.rb | 5 +++++ db/schema.rb | 10 ++++++---- spec/models/administrateur_spec.rb | 6 ++++++ spec/models/gestionnaire_spec.rb | 5 ++++- spec/models/procedure_spec.rb | 2 ++ 9 files changed, 35 insertions(+), 5 deletions(-) create mode 100644 db/migrate/20151110091159_create_reference_admninistrateur_to_procedure.rb create mode 100644 db/migrate/20151110091451_create_reference_admninistrateur_to_gestionnaire.rb diff --git a/app/models/administrateur.rb b/app/models/administrateur.rb index 4166b6099..3af275c04 100644 --- a/app/models/administrateur.rb +++ b/app/models/administrateur.rb @@ -3,4 +3,7 @@ class Administrateur < ActiveRecord::Base # :confirmable, :lockable, :timeoutable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable + + has_many :gestionnaires + has_many :procedures end diff --git a/app/models/gestionnaire.rb b/app/models/gestionnaire.rb index 4e9feccc1..6b93b0538 100644 --- a/app/models/gestionnaire.rb +++ b/app/models/gestionnaire.rb @@ -3,4 +3,6 @@ class Gestionnaire < ActiveRecord::Base # :confirmable, :lockable, :timeoutable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable + + belongs_to :administrateur end diff --git a/app/models/procedure.rb b/app/models/procedure.rb index 182152038..bb39eccf2 100644 --- a/app/models/procedure.rb +++ b/app/models/procedure.rb @@ -3,6 +3,8 @@ class Procedure < ActiveRecord::Base has_many :types_de_champ has_many :dossiers + belongs_to :administrateur + validates :libelle, presence: true, allow_blank: false, allow_nil: false validates :description, presence: true, allow_blank: false, allow_nil: false end diff --git a/db/migrate/20151110091159_create_reference_admninistrateur_to_procedure.rb b/db/migrate/20151110091159_create_reference_admninistrateur_to_procedure.rb new file mode 100644 index 000000000..55585bd51 --- /dev/null +++ b/db/migrate/20151110091159_create_reference_admninistrateur_to_procedure.rb @@ -0,0 +1,5 @@ +class CreateReferenceAdmninistrateurToProcedure < ActiveRecord::Migration + def change + add_reference :procedures, :administrateur, references: :procedures + end +end diff --git a/db/migrate/20151110091451_create_reference_admninistrateur_to_gestionnaire.rb b/db/migrate/20151110091451_create_reference_admninistrateur_to_gestionnaire.rb new file mode 100644 index 000000000..ecd1237ad --- /dev/null +++ b/db/migrate/20151110091451_create_reference_admninistrateur_to_gestionnaire.rb @@ -0,0 +1,5 @@ +class CreateReferenceAdmninistrateurToGestionnaire < ActiveRecord::Migration + def change + add_reference :gestionnaires, :administrateur, references: :gestionnaires + end +end diff --git a/db/schema.rb b/db/schema.rb index f83ccdbbb..8b1c8c784 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20151105095431) do +ActiveRecord::Schema.define(version: 20151110091451) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -117,6 +117,7 @@ ActiveRecord::Schema.define(version: 20151105095431) do t.inet "last_sign_in_ip" t.datetime "created_at" t.datetime "updated_at" + t.integer "administrateur_id" end add_index "gestionnaires", ["email"], name: "index_gestionnaires_on_email", unique: true, using: :btree @@ -136,10 +137,11 @@ ActiveRecord::Schema.define(version: 20151105095431) do t.string "organisation" t.string "direction" t.string "lien_demarche" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false t.boolean "test" - t.boolean "use_api_carto", default: false + t.boolean "use_api_carto", default: false + t.integer "administrateur_id" end create_table "types_de_champ", force: :cascade do |t| diff --git a/spec/models/administrateur_spec.rb b/spec/models/administrateur_spec.rb index c23f8b0d0..9f5e551f0 100644 --- a/spec/models/administrateur_spec.rb +++ b/spec/models/administrateur_spec.rb @@ -15,4 +15,10 @@ describe Administrateur, type: :model do it { is_expected.to have_db_column(:created_at) } it { is_expected.to have_db_column(:updated_at) } end + + describe 'assocations' do + it { is_expected.to have_many(:gestionnaires) } + it { is_expected.to have_many(:procedures) } + end + end diff --git a/spec/models/gestionnaire_spec.rb b/spec/models/gestionnaire_spec.rb index 6a8a15b2a..2913c3cd3 100644 --- a/spec/models/gestionnaire_spec.rb +++ b/spec/models/gestionnaire_spec.rb @@ -1,7 +1,6 @@ require 'rails_helper' describe Gestionnaire, type: :model do - describe 'database column' do it { is_expected.to have_db_column(:email) } it { is_expected.to have_db_column(:encrypted_password) } @@ -16,4 +15,8 @@ describe Gestionnaire, type: :model do it { is_expected.to have_db_column(:created_at) } it { is_expected.to have_db_column(:updated_at) } end + + describe 'association' do + it { is_expected.to belong_to(:administrateur) } + end end diff --git a/spec/models/procedure_spec.rb b/spec/models/procedure_spec.rb index 6c9a30469..afb8c8c8e 100644 --- a/spec/models/procedure_spec.rb +++ b/spec/models/procedure_spec.rb @@ -5,6 +5,8 @@ describe Procedure do it { is_expected.to have_many(:types_de_piece_justificative) } it { is_expected.to have_many(:types_de_champ) } it { is_expected.to have_many(:dossiers) } + it { is_expected.to belong_to(:administrateur) } + end describe 'attributes' do