diff options
| -rw-r--r-- | app/models/workbench.rb | 12 | ||||
| -rw-r--r-- | db/migrate/20170925123159_add_output_to_workbenches.rb | 5 | ||||
| -rw-r--r-- | db/migrate/20170925154017_create_referential_suite_for_each_existing_workbench.rb | 12 | ||||
| -rw-r--r-- | db/schema.rb | 3 | ||||
| -rw-r--r-- | spec/factories/referential_suites.rb | 3 | ||||
| -rw-r--r-- | spec/factories/workbenches.rb | 1 | ||||
| -rw-r--r-- | spec/models/workbench_spec.rb | 24 | ||||
| -rw-r--r-- | spec/support/referential.rb | 17 |
8 files changed, 74 insertions, 3 deletions
diff --git a/app/models/workbench.rb b/app/models/workbench.rb index 30692e625..fe4e6cce1 100644 --- a/app/models/workbench.rb +++ b/app/models/workbench.rb @@ -2,6 +2,7 @@ class Workbench < ActiveRecord::Base belongs_to :organisation belongs_to :line_referential belongs_to :stop_area_referential + belongs_to :output, class_name: 'ReferentialSuite' has_many :lines, -> (workbench) { Stif::MyWorkbenchScopes.new(workbench).line_scope(self) }, through: :line_referential has_many :networks, through: :line_referential @@ -13,10 +14,13 @@ class Workbench < ActiveRecord::Base validates :name, presence: true validates :organisation, presence: true + validates :output, presence: true has_many :referentials has_many :referential_metadatas, through: :referentials, source: :metadatas + before_validation :initialize_output + def all_referentials if line_ids.empty? @@ -26,4 +30,12 @@ class Workbench < ActiveRecord::Base end end + private + + def initialize_output + # Don't reset `output` if it's already initialised + return if !output.nil? + + self.output = ReferentialSuite.create + end end diff --git a/db/migrate/20170925123159_add_output_to_workbenches.rb b/db/migrate/20170925123159_add_output_to_workbenches.rb new file mode 100644 index 000000000..d6aea96a0 --- /dev/null +++ b/db/migrate/20170925123159_add_output_to_workbenches.rb @@ -0,0 +1,5 @@ +class AddOutputToWorkbenches < ActiveRecord::Migration + def change + add_column :workbenches, :output_id, :bigint, index: true + end +end diff --git a/db/migrate/20170925154017_create_referential_suite_for_each_existing_workbench.rb b/db/migrate/20170925154017_create_referential_suite_for_each_existing_workbench.rb new file mode 100644 index 000000000..530850a5a --- /dev/null +++ b/db/migrate/20170925154017_create_referential_suite_for_each_existing_workbench.rb @@ -0,0 +1,12 @@ +class CreateReferentialSuiteForEachExistingWorkbench < ActiveRecord::Migration + def up + Workbench.where(output: nil).each do |workbench| + workbench.output = ReferentialSuite.create + workbench.save + end + end + + def down + raise ActiveRecord::IrreversibleMigration + end +end diff --git a/db/schema.rb b/db/schema.rb index 89f002aee..c95280051 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: 20170922165315) do +ActiveRecord::Schema.define(version: 20170925154017) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -969,6 +969,7 @@ ActiveRecord::Schema.define(version: 20170922165315) do t.datetime "updated_at" t.integer "line_referential_id", limit: 8 t.integer "stop_area_referential_id", limit: 8 + t.integer "output_id", limit: 8 end add_index "workbenches", ["line_referential_id"], name: "index_workbenches_on_line_referential_id", using: :btree diff --git a/spec/factories/referential_suites.rb b/spec/factories/referential_suites.rb new file mode 100644 index 000000000..bb17b085c --- /dev/null +++ b/spec/factories/referential_suites.rb @@ -0,0 +1,3 @@ +FactoryGirl.define do + factory :referential_suite +end diff --git a/spec/factories/workbenches.rb b/spec/factories/workbenches.rb index d55141513..57bef2203 100644 --- a/spec/factories/workbenches.rb +++ b/spec/factories/workbenches.rb @@ -5,5 +5,6 @@ FactoryGirl.define do association :organisation association :line_referential association :stop_area_referential + association :output, factory: :referential_suite end end diff --git a/spec/models/workbench_spec.rb b/spec/models/workbench_spec.rb index 84149ddb0..037537b60 100644 --- a/spec/models/workbench_spec.rb +++ b/spec/models/workbench_spec.rb @@ -11,6 +11,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(:output).class_name('ReferentialSuite') } it { should have_many(:lines).through(:line_referential) } it { should have_many(:networks).through(:line_referential) } @@ -19,6 +20,15 @@ RSpec.describe Workbench, :type => :model do it { should have_many(:stop_areas).through(:stop_area_referential) } + it do + # This callback interferes with the validation test + Workbench.skip_callback(:validation, :before, :initialize_output) + + should validate_presence_of(:output) + + Workbench.set_callback(:validation, :before, :initialize_output) + end + context '.lines' do let!(:ids) { ['STIF:CODIFLIGNE:Line:C00840', 'STIF:CODIFLIGNE:Line:C00086'] } let!(:organisation) { create :organisation, sso_attributes: { functional_scope: ids.to_json } } @@ -33,4 +43,18 @@ RSpec.describe Workbench, :type => :model do expect(lines.map(&:objectid)).to include(*ids) end end + + describe ".create" do + it "must automatically create a ReferentialSuite when being created" do + workbench = Workbench.create + expect(workbench.output).to be_an_instance_of(ReferentialSuite) + end + + it "must not overwrite a given ReferentialSuite" do + referential_suite = create(:referential_suite) + workbench = create(:workbench, output: referential_suite) + + expect(workbench.output).to eq(referential_suite) + end + end end diff --git a/spec/support/referential.rb b/spec/support/referential.rb index 3b74cb639..6f60bd86b 100644 --- a/spec/support/referential.rb +++ b/spec/support/referential.rb @@ -52,8 +52,21 @@ RSpec.configure do |config| referential.add_member organisation, owner: true end - workbench = Workbench.create!(:name => "Gestion de l'offre", organisation: organisation, line_referential: line_referential, stop_area_referential: stop_area_referential) - referential = Referential.create! prefix: "first", name: "first", slug: "first", organisation: organisation, workbench: workbench + workbench = FactoryGirl.create( + :workbench, + name: "Gestion de l'offre", + organisation: organisation, + line_referential: line_referential, + stop_area_referential: stop_area_referential + ) + referential = FactoryGirl.create( + :referential, + prefix: "first", + name: "first", + slug: "first", + organisation: organisation, + workbench: workbench + ) end config.before(:each) do |
