aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlban Peignier2018-01-10 17:45:23 +0100
committerGitHub2018-01-10 17:45:23 +0100
commitdf77b06a42277b3c36627a2bfaa84ddf501d9f5f (patch)
tree7f3ba79ebb0a9e831749b69cdf03e4b4ec7c6e56
parente9f9757ea558f90ed125c0284b9cb98539764b75 (diff)
parent0bc79220fbce288102ea470fa57f865ee8fdfb47 (diff)
downloadchouette-core-df77b06a42277b3c36627a2bfaa84ddf501d9f5f.tar.bz2
Merge pull request #215 from af83/5499-workgroup-model
Provide Workgroup model. Refs #5499
-rw-r--r--app/models/workbench.rb1
-rw-r--r--app/models/workgroup.rb12
-rw-r--r--config/initializers/apartment.rb1
-rw-r--r--config/initializers/stif.rb15
-rw-r--r--db/migrate/20180108132310_create_workgroups.rb11
-rw-r--r--db/migrate/20180109133022_add_workgroup_id_to_workbenches.rb6
-rw-r--r--db/schema.rb12
-rw-r--r--db/seeds/stif.seeds.rb7
-rw-r--r--spec/factories/workbenches.rb1
-rw-r--r--spec/factories/workgroups.rb7
-rw-r--r--spec/models/workbench_spec.rb1
-rw-r--r--spec/models/workgroup_spec.rb30
12 files changed, 99 insertions, 5 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
new file mode 100644
index 000000000..bc2831bbd
--- /dev/null
+++ b/app/models/workgroup.rb
@@ -0,0 +1,12 @@
+class Workgroup < ActiveRecord::Base
+ belongs_to :line_referential
+ belongs_to :stop_area_referential
+
+ has_many :workbenches
+ has_many :organisations, through: :workbenches
+
+ validates_uniqueness_of :name
+
+ validates_presence_of :line_referential_id
+ validates_presence_of :stop_area_referential_id
+end
diff --git a/config/initializers/apartment.rb b/config/initializers/apartment.rb
index 8becd23c2..2031d9918 100644
--- a/config/initializers/apartment.rb
+++ b/config/initializers/apartment.rb
@@ -39,6 +39,7 @@ Apartment.configure do |config|
'Chouette::Network',
'ReferentialCloning',
'Workbench',
+ 'Workgroup',
'CleanUp',
'CleanUpResult',
'Calendar',
diff --git a/config/initializers/stif.rb b/config/initializers/stif.rb
index 60db50083..a73e4931b 100644
--- a/config/initializers/stif.rb
+++ b/config/initializers/stif.rb
@@ -1,3 +1,4 @@
+# coding: utf-8
Rails.application.config.to_prepare do
Organisation.after_create do |organisation|
line_referential = LineReferential.find_by(name: "CodifLigne")
@@ -6,10 +7,16 @@ 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 |w|
+ w.line_referential = line_referential
+ w.stop_area_referential = stop_area_referential
+ end
+
+ workbench = organisation.workbenches.find_or_create_by(name: "Gestion de l'offre") do |w|
+ w.line_referential = line_referential
+ w.stop_area_referential = stop_area_referential
+ w.objectid_format = Workbench.objectid_format.stif_netex
+ w.workgroup = workgroup
Rails.logger.debug "Create Workbench for #{organisation.name}"
end
diff --git a/db/migrate/20180108132310_create_workgroups.rb b/db/migrate/20180108132310_create_workgroups.rb
new file mode 100644
index 000000000..717f05856
--- /dev/null
+++ b/db/migrate/20180108132310_create_workgroups.rb
@@ -0,0 +1,11 @@
+class CreateWorkgroups < ActiveRecord::Migration
+ def change
+ create_table :workgroups do |t|
+ t.string :name
+ t.integer :line_referential_id, limit: 8
+ t.integer :stop_area_referential_id, limit: 8
+
+ t.timestamps null: false
+ end
+ end
+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 df8243cfd..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: 20180103084612) 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,21 @@ ActiveRecord::Schema.define(version: 20180103084612) 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"
+ t.integer "line_referential_id", limit: 8
+ t.integer "stop_area_referential_id", limit: 8
+ t.datetime "created_at", null: false
+ t.datetime "updated_at", null: false
+ end
add_foreign_key "access_links", "access_points", name: "aclk_acpt_fkey"
add_foreign_key "api_keys", "organisations"
diff --git a/db/seeds/stif.seeds.rb b/db/seeds/stif.seeds.rb
index 464601557..c87bb7970 100644
--- a/db/seeds/stif.seeds.rb
+++ b/db/seeds/stif.seeds.rb
@@ -5,6 +5,13 @@
stop_area_referential = StopAreaReferential.find_or_create_by!(name: "Reflex", objectid_format: "stif_netex")
line_referential = LineReferential.find_or_create_by!(name: "CodifLigne", objectid_format: "stif_netex")
+workgroup = Workgroup.find_or_create_by!(name: "Gestion de l'offre théorique IDFm") do |w|
+ w.line_referential = line_referential
+ w.stop_area_referential = stop_area_referential
+end
+
+Workbench.update_all workgroup_id: workgroup
+
# Organisations
stif = Organisation.find_or_create_by!(code: "STIF") do |org|
org.name = 'STIF'
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
new file mode 100644
index 000000000..792deddf8
--- /dev/null
+++ b/spec/factories/workgroups.rb
@@ -0,0 +1,7 @@
+FactoryGirl.define do
+ factory :workgroup do
+ 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
new file mode 100644
index 000000000..ac8d3fc98
--- /dev/null
+++ b/spec/models/workgroup_spec.rb
@@ -0,0 +1,30 @@
+require 'rails_helper'
+
+RSpec.describe Workgroup, type: :model do
+ context "associations" do
+ let( :workgroup ){ build_stubbed :workgroup, line_referential_id: 53, stop_area_referential_id: 42 }
+
+ 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