1
0

Fixed importing Foods with QuantityValue

Fixed double flash when not followed by request
Added Food#destroy error reporting
Simplified prepare_meals with no ingredients
Renamed scope on item with subitems: subitems -> with_subitems
This commit is contained in:
cryptogopher
2020-05-20 23:33:34 +02:00
parent fa9c329a81
commit a416e1ce9b
13 changed files with 29 additions and 17 deletions

View File

@@ -16,12 +16,11 @@ class Food < ActiveRecord::Base
belongs_to :ref_unit, class_name: 'Unit', required: true
belongs_to :source, required: false
has_many :ingredients, dependent: :restrict_with_error
has_many :nutrients, foreign_key: 'registry_id', inverse_of: :food, dependent: :destroy,
validate: true
has_many :nutrients, as: :registry, inverse_of: :food, dependent: :destroy, validate: true
DOMAIN = :diet
alias_attribute :subitems, :nutrients
scope :subitems, -> { includes(nutrients: [:quantity, :unit]) }
scope :with_subitems, -> { includes(nutrients: [:quantity, :unit]) }
validates :nutrients, presence: true
accepts_nested_attributes_for :nutrients, allow_destroy: true,

View File

@@ -50,6 +50,8 @@ class Formula < ActiveRecord::Base
def lastBefore(timepoints)
self.map{ BigDecimal(2000) }
last_timepoint = timepoints.max
@quantity.quantity_values.includes(:registry)
end
end

View File

@@ -6,7 +6,7 @@ class Ingredient < ActiveRecord::Base
DOMAIN = :diet
alias_attribute :subitems, :nutrients
scope :subitems, -> { includes(nutrients: [:quantity, :unit]) }
scope :with_subitems, -> { includes(nutrients: [:quantity, :unit]) }
validates :ready_ratio, numericality: {greater_than_or_equal_to: 0.0}
validates :amount, numericality: {greater_than_or_equal_to: 0.0}

View File

@@ -3,12 +3,12 @@ class Measurement < ActiveRecord::Base
class_name: 'MeasurementRoutine'
belongs_to :source, required: false
has_one :project, through: :routine
has_many :readouts, foreign_key: 'registry_id', inverse_of: :measurement,
dependent: :destroy, validate: true
has_many :readouts, as: :registry, inverse_of: :measurement, dependent: :destroy,
validate: true
DOMAIN = :measurement
alias_attribute :subitems, :readouts
scope :subitems, -> { includes(readouts: [:quantity, :unit]) }
scope :with_subitems, -> { includes(readouts: [:quantity, :unit]) }
accepts_nested_attributes_for :routine, allow_destroy: true,
reject_if: proc { |attrs| attrs['name'].blank? }

View File

@@ -1,5 +1,6 @@
class Nutrient < QuantityValue
belongs_to :food, foreign_key: 'registry_id', inverse_of: :nutrients, required: true
belongs_to :food, foreign_key: 'registry_id', foreign_type: 'registry_type',
inverse_of: :nutrients, polymorphic: true, required: true
# Uniqueness NOT validated here, see Value for explanation
#validates :quantity, uniqueness: {scope: :food_id}

View File

@@ -9,6 +9,7 @@ class Quantity < ActiveRecord::Base
belongs_to :project, required: false
has_many :nutrients, dependent: :restrict_with_error
has_many :readouts, dependent: :restrict_with_error
has_many :quantity_values, dependent: :restrict_with_error
has_many :exposures, dependent: :destroy
has_one :formula, inverse_of: :quantity, dependent: :destroy, validate: true

View File

@@ -1,4 +1,6 @@
class QuantityValue < ActiveRecord::Base
# Requirement validation for :registry left to subclasses
belongs_to :registry, polymorphic: true
belongs_to :quantity, required: true
belongs_to :unit, required: true

View File

@@ -1,7 +1,10 @@
class Readout < QuantityValue
belongs_to :measurement, foreign_key: 'registry_id', inverse_of: :readouts, required: true
belongs_to :measurement, foreign_key: 'registry_id', inverse_of: :readouts,
polymorphic: true, required: true
# Uniqueness NOT validated here, see Value for explanation
#validates :quantity, uniqueness: {scope: [:measurement_id, :unit_id]}
validates :value, numericality: true
delegate :completed_at, to: :measurement
end