From f769fff9300596c6f3a5c22fd6edb70f710c0daf Mon Sep 17 00:00:00 2001 From: cryptogopher Date: Tue, 3 Dec 2019 00:30:30 +0100 Subject: [PATCH] Updated comments, added before_destroy where applicable --- app/controllers/ingredients_controller.rb | 1 - app/controllers/measurements_controller.rb | 1 - app/controllers/sources_controller.rb | 1 - app/controllers/units_controller.rb | 1 - app/models/ingredient.rb | 6 ++++++ app/models/measurement.rb | 3 ++- app/models/quantity.rb | 6 ++++++ app/models/source.rb | 6 ++++++ app/models/unit.rb | 6 ++++++ 9 files changed, 26 insertions(+), 5 deletions(-) diff --git a/app/controllers/ingredients_controller.rb b/app/controllers/ingredients_controller.rb index 86c5f13..64735da 100644 --- a/app/controllers/ingredients_controller.rb +++ b/app/controllers/ingredients_controller.rb @@ -43,7 +43,6 @@ class IngredientsController < ApplicationController end def destroy - # FIXME: don't destroy if any meal depend on ingredient if @ingredient.destroy flash[:notice] = 'Deleted ingredient' end diff --git a/app/controllers/measurements_controller.rb b/app/controllers/measurements_controller.rb index 27ab643..84453b2 100644 --- a/app/controllers/measurements_controller.rb +++ b/app/controllers/measurements_controller.rb @@ -39,7 +39,6 @@ class MeasurementsController < ApplicationController end def destroy - # FIXME: don't destroy if there are any readout values if @measurement.destroy flash[:notice] = 'Deleted measurement' end diff --git a/app/controllers/sources_controller.rb b/app/controllers/sources_controller.rb index be04304..1cff136 100644 --- a/app/controllers/sources_controller.rb +++ b/app/controllers/sources_controller.rb @@ -22,7 +22,6 @@ class SourcesController < ApplicationController end def destroy - # FIXME: do not destroy if anything depends on it if @source.destroy flash[:notice] = 'Deleted source' end diff --git a/app/controllers/units_controller.rb b/app/controllers/units_controller.rb index 11ef164..158997d 100644 --- a/app/controllers/units_controller.rb +++ b/app/controllers/units_controller.rb @@ -22,7 +22,6 @@ class UnitsController < ApplicationController end def destroy - # FIXME: do not destroy if anything depends on it if @unit.destroy flash[:notice] = 'Deleted unit' end diff --git a/app/models/ingredient.rb b/app/models/ingredient.rb index f22ce0f..0e90b92 100644 --- a/app/models/ingredient.rb +++ b/app/models/ingredient.rb @@ -6,6 +6,12 @@ class Ingredient < ActiveRecord::Base meat: 1 } + # Has to go before any 'dependent:' association + before_destroy do + # FIXME: disallow destruction if any object depends on this quantity + nil + end + belongs_to :project, required: true belongs_to :ref_unit, class_name: 'Unit', required: true belongs_to :source, required: false diff --git a/app/models/measurement.rb b/app/models/measurement.rb index 21d27ac..ab08170 100644 --- a/app/models/measurement.rb +++ b/app/models/measurement.rb @@ -7,7 +7,8 @@ class Measurement < ActiveRecord::Base accepts_nested_attributes_for :readouts, allow_destroy: true, reject_if: proc { |attrs| attrs['quantity_id'].blank? && attrs['value'].blank? } - # Readout (quantity_id, unit_id) pair uniqueness check for nested attributes + # Readout quantity_id + unit_id uniqueness validation. Cannot be effectively + # checked on Readout model level. validate do quantities = self.readouts.map { |r| [r.quantity_id, r.unit_id] } if quantities.length != quantities.uniq.length diff --git a/app/models/quantity.rb b/app/models/quantity.rb index cfdec64..17ceb53 100644 --- a/app/models/quantity.rb +++ b/app/models/quantity.rb @@ -7,6 +7,12 @@ class Quantity < ActiveRecord::Base exercise: 2 } + # Has to go before any 'dependent:' association + before_destroy do + # FIXME: disallow destruction if any object depends on this quantity + nil + end + acts_as_nested_set dependent: :destroy, scope: :project belongs_to :project, required: false diff --git a/app/models/source.rb b/app/models/source.rb index 01160d9..daa45ee 100644 --- a/app/models/source.rb +++ b/app/models/source.rb @@ -2,4 +2,10 @@ class Source < ActiveRecord::Base belongs_to :project, required: false validates :name, presence: true, uniqueness: {scope: :project_id} + + # Has to go before any 'dependent:' association + before_destroy do + # FIXME: disallow destruction if any object depends on this quantity + nil + end end diff --git a/app/models/unit.rb b/app/models/unit.rb index 7b44d77..93a39e5 100644 --- a/app/models/unit.rb +++ b/app/models/unit.rb @@ -2,4 +2,10 @@ class Unit < ActiveRecord::Base belongs_to :project, required: false validates :shortname, presence: true, uniqueness: {scope: :project_id} + + # Has to go before any 'dependent:' association + before_destroy do + # FIXME: disallow destruction if any object depends on this quantity + nil + end end