diff options
| author | Robert | 2018-01-09 15:02:29 +0100 |
|---|---|---|
| committer | Alban Peignier | 2018-01-10 17:43:03 +0100 |
| commit | a9078add9036d97ede702c14b064c216365dc5b1 (patch) | |
| tree | 9ac51b45a59b241be5b4dfe9707e24918640fc29 | |
| parent | de93869cd6f9e842d8c0b968a3ff40a1ff521774 (diff) | |
| download | chouette-core-a9078add9036d97ede702c14b064c216365dc5b1.tar.bz2 | |
Fixes: #5499; Added validations and associations
| -rw-r--r-- | app/models/workbench.rb | 1 | ||||
| -rw-r--r-- | app/models/workgroup.rb | 12 | ||||
| -rw-r--r-- | config/initializers/stif.rb | 15 | ||||
| -rw-r--r-- | db/migrate/20180108132310_create_workgroups.rb | 4 | ||||
| -rw-r--r-- | db/migrate/20180109133022_add_workgroup_id_to_workbenches.rb | 6 | ||||
| -rw-r--r-- | db/schema.rb | 7 | ||||
| -rw-r--r-- | spec/factories/workbenches.rb | 1 | ||||
| -rw-r--r-- | spec/factories/workgroups.rb | 6 | ||||
| -rw-r--r-- | spec/models/workbench_spec.rb | 1 | ||||
| -rw-r--r-- | spec/models/workgroup_spec.rb | 28 |
10 files changed, 63 insertions, 18 deletions
diff --git a/app/models/workbench.rb b/app/models/workbench.rb index 3190246ae..f49f4e7cf 100644 --- a/app/models/workbench.rb +++ b/app/models/workbench.rb @@ -4,6 +4,7 @@ class Workbench < ActiveRecord::Base belongs_to :line_referential belongs_to :stop_area_referential belongs_to :output, class_name: 'ReferentialSuite' + belongs_to :workgroup has_many :lines, -> (workbench) { Stif::MyWorkbenchScopes.new(workbench).line_scope(self) }, through: :line_referential has_many :networks, through: :line_referential diff --git a/app/models/workgroup.rb b/app/models/workgroup.rb index 725add5a3..fb8fb3386 100644 --- a/app/models/workgroup.rb +++ b/app/models/workgroup.rb @@ -1,5 +1,15 @@ class Workgroup < ActiveRecord::Base - belongs_to :stop_area_referential belongs_to :line_referential + belongs_to :stop_area_referential + + has_many :workbenches + + validates_uniqueness_of :name + + validates_presence_of :line_referential_id + validates_presence_of :stop_area_referential_id + def organisations + workbenches.map(&:organisation) + end end diff --git a/config/initializers/stif.rb b/config/initializers/stif.rb index 60db50083..428869be6 100644 --- a/config/initializers/stif.rb +++ b/config/initializers/stif.rb @@ -6,13 +6,20 @@ Rails.application.config.to_prepare do line_referential.organisations << organisation stop_area_referential.organisations << organisation - organisation.workbenches.find_or_create_by(name: "Gestion de l'offre") do |workbench| - workbench.line_referential = line_referential - workbench.stop_area_referential = stop_area_referential - workbench.objectid_format = Workbench.objectid_format.stif_netex + workgroup = Workgroup.find_or_create_by(name: "Gestion de l'offre théorique IDFm") do |wkgrp| + wkgrp.line_referential = line_referential + wkgrp.stop_area_referential = stop_area_referential + end + + workbench = organisation.workbenches.find_or_create_by(name: "Gestion de l'offre") do |wkbnch| + wkbnch.line_referential = line_referential + wkbnch.stop_area_referential = stop_area_referential + wkbnch.objectid_format = Workbench.objectid_format.stif_netex Rails.logger.debug "Create Workbench for #{organisation.name}" end + + workbench.update( workgroup: workgroup ) end end unless Rails.env.test? diff --git a/db/migrate/20180108132310_create_workgroups.rb b/db/migrate/20180108132310_create_workgroups.rb index 4e441a58d..717f05856 100644 --- a/db/migrate/20180108132310_create_workgroups.rb +++ b/db/migrate/20180108132310_create_workgroups.rb @@ -2,8 +2,8 @@ class CreateWorkgroups < ActiveRecord::Migration def change create_table :workgroups do |t| t.string :name - t.integer :line_referential_id, index: true, limit: 8 - t.integer :stop_area_referential_id, index: true, limit: 8 + t.integer :line_referential_id, limit: 8 + t.integer :stop_area_referential_id, limit: 8 t.timestamps null: false end diff --git a/db/migrate/20180109133022_add_workgroup_id_to_workbenches.rb b/db/migrate/20180109133022_add_workgroup_id_to_workbenches.rb new file mode 100644 index 000000000..8736f7fbb --- /dev/null +++ b/db/migrate/20180109133022_add_workgroup_id_to_workbenches.rb @@ -0,0 +1,6 @@ +class AddWorkgroupIdToWorkbenches < ActiveRecord::Migration + def change + add_column :workbenches, :workgroup_id, :integer, limit: 8 + add_index :workbenches, :workgroup_id + end +end diff --git a/db/schema.rb b/db/schema.rb index cfc4d4464..19af8690b 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: 20180108132310) do +ActiveRecord::Schema.define(version: 20180109133022) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -985,11 +985,13 @@ ActiveRecord::Schema.define(version: 20180108132310) do t.integer "stop_area_referential_id", limit: 8 t.integer "output_id", limit: 8 t.string "objectid_format" + t.integer "workgroup_id", limit: 8 end add_index "workbenches", ["line_referential_id"], name: "index_workbenches_on_line_referential_id", using: :btree add_index "workbenches", ["organisation_id"], name: "index_workbenches_on_organisation_id", using: :btree add_index "workbenches", ["stop_area_referential_id"], name: "index_workbenches_on_stop_area_referential_id", using: :btree + add_index "workbenches", ["workgroup_id"], name: "index_workbenches_on_workgroup_id", using: :btree create_table "workgroups", id: :bigserial, force: :cascade do |t| t.string "name" @@ -999,9 +1001,6 @@ ActiveRecord::Schema.define(version: 20180108132310) do t.datetime "updated_at", null: false end - add_index "workgroups", ["line_referential_id"], name: "index_workgroups_on_line_referential_id", using: :btree - add_index "workgroups", ["stop_area_referential_id"], name: "index_workgroups_on_stop_area_referential_id", using: :btree - add_foreign_key "access_links", "access_points", name: "aclk_acpt_fkey" add_foreign_key "api_keys", "organisations" add_foreign_key "compliance_check_blocks", "compliance_check_sets" diff --git a/spec/factories/workbenches.rb b/spec/factories/workbenches.rb index 0f26559d8..98fdd6ad9 100644 --- a/spec/factories/workbenches.rb +++ b/spec/factories/workbenches.rb @@ -7,5 +7,6 @@ FactoryGirl.define do association :line_referential association :stop_area_referential association :output, factory: :referential_suite + association :workgroup end end diff --git a/spec/factories/workgroups.rb b/spec/factories/workgroups.rb index d9647ae1a..792deddf8 100644 --- a/spec/factories/workgroups.rb +++ b/spec/factories/workgroups.rb @@ -1,7 +1,7 @@ FactoryGirl.define do factory :workgroup do - name "MyString" - stop_area_referential nil - line_referential nil + sequence(:name) { |n| "Workgroup ##{n}" } + association :line_referential + association :stop_area_referential end end diff --git a/spec/models/workbench_spec.rb b/spec/models/workbench_spec.rb index caff00ae4..2f1fe39da 100644 --- a/spec/models/workbench_spec.rb +++ b/spec/models/workbench_spec.rb @@ -12,6 +12,7 @@ RSpec.describe Workbench, :type => :model do it { should belong_to(:organisation) } it { should belong_to(:line_referential) } it { should belong_to(:stop_area_referential) } + it { should belong_to(:workgroup) } it { should belong_to(:output).class_name('ReferentialSuite') } it { should have_many(:lines).through(:line_referential) } diff --git a/spec/models/workgroup_spec.rb b/spec/models/workgroup_spec.rb index 7d7b3cf87..ac8d3fc98 100644 --- a/spec/models/workgroup_spec.rb +++ b/spec/models/workgroup_spec.rb @@ -1,10 +1,30 @@ require 'rails_helper' RSpec.describe Workgroup, type: :model do - let( :workgroup ){ build_stubbed :workgroup } + context "associations" do + let( :workgroup ){ build_stubbed :workgroup, line_referential_id: 53, stop_area_referential_id: 42 } - it 'is not valid without a stop_area_referential' do - workgroup.line_referential_id = 42 - expect( workgroup ).not_to be_valid + it{ should have_many(:workbenches) } + it{ should validate_uniqueness_of(:name) } + + it 'is not valid without a stop_area_referential' do + workgroup.stop_area_referential_id = nil + expect( workgroup ).not_to be_valid + end + it 'is not valid without a line_referential' do + workgroup.line_referential_id = nil + expect( workgroup ).not_to be_valid + end + it 'is valid with both assoications' do + expect( workgroup ).to be_valid + end + end + + context "find organisations" do + let( :workgroup ){ create :workgroup } + let!( :workbench1 ){ create :workbench, workgroup: workgroup } + let!( :workbench2 ){ create :workbench, workgroup: workgroup } + + it{ expect( Set.new(workgroup.organisations) ).to eq(Set.new([ workbench1.organisation, workbench2.organisation ])) } end end |
