Removed ITEM_TYPES in favor of uniformly named scopes/attributes
This commit is contained in:
@@ -16,12 +16,16 @@ 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
|
||||
|
||||
DOMAIN = :diet
|
||||
alias_attribute :subitems, :nutrients
|
||||
scope :subitems, -> { includes(nutrients: [:quantity, :unit]) }
|
||||
|
||||
has_many :nutrients, inverse_of: :food, dependent: :destroy, validate: true
|
||||
validates :nutrients, presence: true
|
||||
accepts_nested_attributes_for :nutrients, allow_destroy: true, reject_if: proc { |attrs|
|
||||
attrs['quantity_id'].blank? && attrs['amount'].blank?
|
||||
}
|
||||
accepts_nested_attributes_for :nutrients, allow_destroy: true,
|
||||
reject_if: proc { |attrs| attrs['quantity_id'].blank? && attrs['amount'].blank? }
|
||||
# Nutrient quantity_id uniqueness check for nested attributes
|
||||
validate do
|
||||
quantities = self.nutrients.reject { |n| n.marked_for_destruction? }
|
||||
|
||||
@@ -2,6 +2,11 @@ class Ingredient < ActiveRecord::Base
|
||||
belongs_to :composition, inverse_of: :ingredients, polymorphic: true, required: true
|
||||
belongs_to :food, required: true
|
||||
belongs_to :part_of, required: false
|
||||
has_many :nutrients, through: :food, source: :nutrients
|
||||
|
||||
DOMAIN = :diet
|
||||
alias_attribute :subitems, :nutrients
|
||||
scope :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}
|
||||
|
||||
@@ -1,19 +1,22 @@
|
||||
class Measurement < ActiveRecord::Base
|
||||
belongs_to :routine, required: true, inverse_of: :measurements,
|
||||
class_name: 'MeasurementRoutine'
|
||||
accepts_nested_attributes_for :routine, allow_destroy: true, reject_if: proc { |attrs|
|
||||
attrs['name'].blank?
|
||||
}
|
||||
after_destroy { self.routine.destroy if self.routine.measurements.empty? }
|
||||
has_one :project, through: :routine
|
||||
|
||||
belongs_to :source, required: false
|
||||
has_one :project, through: :routine
|
||||
has_many :readouts, foreign_key: 'registry_id', inverse_of: :measurement,
|
||||
dependent: :destroy, validate: true
|
||||
|
||||
DOMAIN = :measurement
|
||||
alias_attribute :subitems, :readouts
|
||||
scope :subitems, -> { includes(readouts: [:quantity, :unit]) }
|
||||
|
||||
accepts_nested_attributes_for :routine, allow_destroy: true,
|
||||
reject_if: proc { |attrs| attrs['name'].blank? }
|
||||
after_destroy { self.routine.destroy if self.routine.measurements.empty? }
|
||||
|
||||
has_many :readouts, inverse_of: :measurement, dependent: :destroy, validate: true
|
||||
validates :readouts, presence: true
|
||||
accepts_nested_attributes_for :readouts, allow_destroy: true, reject_if: proc { |attrs|
|
||||
attrs['quantity_id'].blank? && attrs['value'].blank?
|
||||
}
|
||||
accepts_nested_attributes_for :readouts, allow_destroy: true,
|
||||
reject_if: proc { |attrs| attrs['quantity_id'].blank? && attrs['value'].blank? }
|
||||
# Readout quantity_id + unit_id uniqueness validation. Cannot be effectively
|
||||
# checked on Readout model level.
|
||||
validate do
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
class Nutrient < QuantityValue
|
||||
belongs_to :food, inverse_of: :nutrients, required: true
|
||||
belongs_to :food, foreign_key: 'registry_id', inverse_of: :nutrients, required: true
|
||||
|
||||
validates :value, numericality: {greater_than_or_equal_to: 0.0}
|
||||
# Uniqueness NOT validated here, see Value for explanation
|
||||
#validates :quantity, uniqueness: {scope: :food_id}
|
||||
validates :value, numericality: {greater_than_or_equal_to: 0.0}
|
||||
|
||||
alias_attribute :amount, :value
|
||||
delegate :ref_amount, to: :food
|
||||
end
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
class Readout < QuantityValue
|
||||
belongs_to :measurement, inverse_of: :readouts, required: true
|
||||
belongs_to :measurement, foreign_key: 'registry_id', inverse_of: :readouts, required: true
|
||||
|
||||
validates :value, numericality: true
|
||||
# Uniqueness NOT validated here, see Value for explanation
|
||||
#validates :quantity, uniqueness: {scope: [:measurement_id, :unit_id]}
|
||||
validates :value, numericality: true
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user