aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTeddy Wing2017-09-25 17:30:23 +0200
committerTeddy Wing2017-09-25 17:30:23 +0200
commitcfd4b8a553af99fd4571172183fa11e59f7e6e35 (patch)
treea375f4d41f18c7ae610a21497b14206de66b4f49
parent72806a4961e4240cff955c66fff1840e40301b89 (diff)
downloadchouette-core-cfd4b8a553af99fd4571172183fa11e59f7e6e35.tar.bz2
Workbench: Ensure `output` attr is initialised when creating workbench
Since all `Workbench`es must have an associated `ReferentialSuite` via `output`, if none is provided when creating a new `Workbench`, create one automatically. This is done in a `before_validation` callback (eck, AR callbacks). In order for our prior 'should validate_presence_of' test to continue to work, we have to work around the new callback, otherwise Shoulda can't set `output` to `nil` to run its expectation and the test fails. Refs #3520
-rw-r--r--app/models/workbench.rb10
-rw-r--r--spec/models/workbench_spec.rb23
2 files changed, 32 insertions, 1 deletions
diff --git a/app/models/workbench.rb b/app/models/workbench.rb
index b4852b9d5..fe4e6cce1 100644
--- a/app/models/workbench.rb
+++ b/app/models/workbench.rb
@@ -19,6 +19,8 @@ class Workbench < ActiveRecord::Base
has_many :referentials
has_many :referential_metadatas, through: :referentials, source: :metadatas
+ before_validation :initialize_output
+
def all_referentials
if line_ids.empty?
@@ -28,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/spec/models/workbench_spec.rb b/spec/models/workbench_spec.rb
index f83342f75..653b896dc 100644
--- a/spec/models/workbench_spec.rb
+++ b/spec/models/workbench_spec.rb
@@ -20,7 +20,14 @@ RSpec.describe Workbench, :type => :model do
it { should have_many(:stop_areas).through(:stop_area_referential) }
- it { should validate_presence_of(:output) }
+ 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'] }
@@ -36,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 = ReferentialSuite.create
+ workbench = Workbench.create(output: referential_suite)
+
+ expect(workbench.output).to eq(referential_suite)
+ end
+ end
end