1
0
This repository has been archived on 2023-12-07. You can view files and clone it, but cannot push or open issues or pull requests.
cryptogopher a416e1ce9b 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
2020-05-20 23:33:34 +02:00

58 lines
1.7 KiB
Ruby

class Food < ActiveRecord::Base
enum group: {
other: 0,
dish: 1,
recipe: 2,
meat: 3
}
# 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
has_many :ingredients, dependent: :restrict_with_error
has_many :nutrients, as: :registry, inverse_of: :food, dependent: :destroy, validate: true
DOMAIN = :diet
alias_attribute :subitems, :nutrients
scope :with_subitems, -> { includes(nutrients: [:quantity, :unit]) }
validates :nutrients, presence: true
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? }
.map { |n| n.quantity_id }
if quantities.length != quantities.uniq.length
errors.add(:nutrients, :duplicated_quantity)
end
end
validates :name, presence: true, uniqueness: {scope: :project_id}
validates :ref_amount, numericality: {greater_than: 0}
validates :group, inclusion: {in: groups.keys}
scope :visible, -> { where(hidden: false) }
scope :hidden, -> { where(hidden: true) }
after_initialize do
if new_record?
self.ref_amount ||= 100
units = self.project.units
self.ref_unit ||= units.find_by(shortname: 'g') || units.first
self.group ||= :other
self.hidden = false if self.hidden.nil?
end
end
def toggle_hidden!
self.toggle!(:hidden)
end
end