From 972cbb129747feb15c3c6c5ab5eabea2d7c045e1 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Mon, 25 Sep 2017 15:18:15 +0200 Subject: Workbench: Add `output` association The `output` attribute should be a `ReferentialSuite`. Refs #3520 --- app/models/workbench.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'app/models/workbench.rb') diff --git a/app/models/workbench.rb b/app/models/workbench.rb index 30692e625..234cb043f 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 -- cgit v1.2.3 From 72806a4961e4240cff955c66fff1840e40301b89 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Mon, 25 Sep 2017 16:17:04 +0200 Subject: Workbench: Validate presence of `output` Ensure all workbenches always have an associated `ReferentialSuite` by validating presence of the `output` attribute. In order to pass the 'should have a valid factory' test, we need to define a `ReferentialSuite` factory and set up a default association on `output` in the `Workbench` factory. We also need to add set `output` when creating a `Workbench` in `spec/support/referential.rb`, otherwise we get a validation error that breaks the tests. Clean up the formatting of that call. In order to pass a `ReferentialSuite`, I just create one above, but I'm not sure if I should be using a `find_or_create` mechanism like the other associated objects do above. Refs #3520 --- app/models/workbench.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'app/models/workbench.rb') diff --git a/app/models/workbench.rb b/app/models/workbench.rb index 234cb043f..b4852b9d5 100644 --- a/app/models/workbench.rb +++ b/app/models/workbench.rb @@ -14,6 +14,7 @@ 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 -- cgit v1.2.3 From cfd4b8a553af99fd4571172183fa11e59f7e6e35 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Mon, 25 Sep 2017 17:30:23 +0200 Subject: 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 --- app/models/workbench.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'app/models/workbench.rb') 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 -- cgit v1.2.3