1
0

Updated comments, added before_destroy where applicable

This commit is contained in:
cryptogopher 2019-12-03 00:30:30 +01:00
parent 8d368d6aa5
commit f769fff930
9 changed files with 26 additions and 5 deletions

View File

@ -43,7 +43,6 @@ class IngredientsController < ApplicationController
end end
def destroy def destroy
# FIXME: don't destroy if any meal depend on ingredient
if @ingredient.destroy if @ingredient.destroy
flash[:notice] = 'Deleted ingredient' flash[:notice] = 'Deleted ingredient'
end end

View File

@ -39,7 +39,6 @@ class MeasurementsController < ApplicationController
end end
def destroy def destroy
# FIXME: don't destroy if there are any readout values
if @measurement.destroy if @measurement.destroy
flash[:notice] = 'Deleted measurement' flash[:notice] = 'Deleted measurement'
end end

View File

@ -22,7 +22,6 @@ class SourcesController < ApplicationController
end end
def destroy def destroy
# FIXME: do not destroy if anything depends on it
if @source.destroy if @source.destroy
flash[:notice] = 'Deleted source' flash[:notice] = 'Deleted source'
end end

View File

@ -22,7 +22,6 @@ class UnitsController < ApplicationController
end end
def destroy def destroy
# FIXME: do not destroy if anything depends on it
if @unit.destroy if @unit.destroy
flash[:notice] = 'Deleted unit' flash[:notice] = 'Deleted unit'
end end

View File

@ -6,6 +6,12 @@ class Ingredient < ActiveRecord::Base
meat: 1 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 :project, required: true
belongs_to :ref_unit, class_name: 'Unit', required: true belongs_to :ref_unit, class_name: 'Unit', required: true
belongs_to :source, required: false belongs_to :source, required: false

View File

@ -7,7 +7,8 @@ class Measurement < ActiveRecord::Base
accepts_nested_attributes_for :readouts, allow_destroy: true, reject_if: proc { |attrs| accepts_nested_attributes_for :readouts, allow_destroy: true, reject_if: proc { |attrs|
attrs['quantity_id'].blank? && attrs['value'].blank? 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 validate do
quantities = self.readouts.map { |r| [r.quantity_id, r.unit_id] } quantities = self.readouts.map { |r| [r.quantity_id, r.unit_id] }
if quantities.length != quantities.uniq.length if quantities.length != quantities.uniq.length

View File

@ -7,6 +7,12 @@ class Quantity < ActiveRecord::Base
exercise: 2 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 acts_as_nested_set dependent: :destroy, scope: :project
belongs_to :project, required: false belongs_to :project, required: false

View File

@ -2,4 +2,10 @@ class Source < ActiveRecord::Base
belongs_to :project, required: false belongs_to :project, required: false
validates :name, presence: true, uniqueness: {scope: :project_id} 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 end

View File

@ -2,4 +2,10 @@ class Unit < ActiveRecord::Base
belongs_to :project, required: false belongs_to :project, required: false
validates :shortname, presence: true, uniqueness: {scope: :project_id} 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 end