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

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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