2016-10-14 12:19:00 -05:00
|
|
|
class RecipeIngredient < ApplicationRecord
|
2016-01-12 18:43:00 -06:00
|
|
|
|
2018-09-11 10:38:07 -05:00
|
|
|
belongs_to :food, optional: true
|
2018-08-27 16:44:45 -05:00
|
|
|
belongs_to :recipe_as_ingredient, class_name: 'Recipe', optional: true
|
2018-04-02 11:21:35 -05:00
|
|
|
belongs_to :recipe, inverse_of: :recipe_ingredients, touch: true
|
2016-01-13 17:10:43 -06:00
|
|
|
|
|
|
|
validates :sort_order, presence: true
|
2016-01-12 18:43:00 -06:00
|
|
|
|
2016-01-30 20:29:35 -06:00
|
|
|
def name
|
2018-09-11 10:38:07 -05:00
|
|
|
if self.ingredient.present?
|
|
|
|
self.ingredient.name
|
2016-01-14 18:56:45 -06:00
|
|
|
else
|
|
|
|
super
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-01-21 11:45:46 -06:00
|
|
|
def display_name
|
2016-01-30 21:51:32 -06:00
|
|
|
str = [quantity, units, name].delete_if { |i| i.blank? }.join(' ')
|
|
|
|
str << ", #{preparation}" if preparation.present?
|
|
|
|
str
|
2016-01-21 11:45:46 -06:00
|
|
|
end
|
|
|
|
|
2018-09-11 10:38:07 -05:00
|
|
|
def ingredient_id
|
2018-08-27 16:44:45 -05:00
|
|
|
case
|
|
|
|
when recipe_as_ingredient_id
|
|
|
|
"R#{recipe_as_ingredient_id}"
|
2018-09-11 10:38:07 -05:00
|
|
|
when food_id
|
|
|
|
"F#{food_id}"
|
2018-08-27 16:44:45 -05:00
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-09-11 10:38:07 -05:00
|
|
|
def ingredient_id=(val)
|
|
|
|
return val if self.ingredient_id == val
|
|
|
|
|
|
|
|
@ingredient = nil
|
2018-08-27 16:44:45 -05:00
|
|
|
case val
|
|
|
|
when -> (v) { v.blank? }
|
|
|
|
self.recipe_as_ingredient_id = nil
|
2018-09-11 10:38:07 -05:00
|
|
|
self.food_id = nil
|
2018-08-27 16:44:45 -05:00
|
|
|
when /^R(\d+)$/
|
2018-09-11 10:38:07 -05:00
|
|
|
self.food_id = nil
|
2018-08-27 16:44:45 -05:00
|
|
|
self.recipe_as_ingredient_id = $1.to_i
|
2018-09-11 10:38:07 -05:00
|
|
|
when /^F(\d+)$/
|
2018-08-27 16:44:45 -05:00
|
|
|
self.recipe_as_ingredient_id = nil
|
2018-09-11 10:38:07 -05:00
|
|
|
self.food_id = $1.to_i
|
2018-08-27 16:44:45 -05:00
|
|
|
else
|
2018-09-11 10:38:07 -05:00
|
|
|
raise "Invalid ingredient_id: #{val}"
|
2018-08-27 16:44:45 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-09-11 10:38:07 -05:00
|
|
|
def ingredient
|
|
|
|
@ingredient ||= case
|
2018-08-27 16:44:45 -05:00
|
|
|
when self.recipe_as_ingredient_id
|
2018-09-11 10:38:07 -05:00
|
|
|
self.recipe_as_ingredient
|
|
|
|
when self.food_id
|
|
|
|
self.food
|
2018-08-27 16:44:45 -05:00
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-01-20 18:37:28 -06:00
|
|
|
def scale(factor, auto_unit = false)
|
2016-01-18 19:41:26 -06:00
|
|
|
if factor.present? && self.quantity.present? && factor != '1'
|
2016-02-02 15:48:20 -06:00
|
|
|
|
|
|
|
value_unit = UnitConversion.parse(self.quantity, self.units)
|
|
|
|
value_unit = value_unit.scale(factor)
|
2016-01-20 18:37:28 -06:00
|
|
|
|
|
|
|
if auto_unit
|
2016-02-02 15:48:20 -06:00
|
|
|
value_unit = value_unit.auto_unit
|
2016-01-20 18:37:28 -06:00
|
|
|
end
|
2016-02-02 15:48:20 -06:00
|
|
|
|
|
|
|
self.quantity = value_unit.pretty_value
|
|
|
|
self.units = value_unit.unit.to_s
|
2016-01-18 19:41:26 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-02-27 20:12:41 -06:00
|
|
|
def to_metric
|
|
|
|
return unless self.quantity.present?
|
|
|
|
value_unit = UnitConversion.parse(self.quantity, self.units)
|
|
|
|
value_unit = value_unit.to_metric
|
|
|
|
|
|
|
|
self.quantity = value_unit.pretty_value
|
|
|
|
self.units = value_unit.unit.to_s
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_standard
|
|
|
|
return unless self.quantity.present?
|
|
|
|
value_unit = UnitConversion.parse(self.quantity, self.units)
|
|
|
|
value_unit = value_unit.to_standard
|
|
|
|
|
|
|
|
self.quantity = value_unit.pretty_value
|
|
|
|
self.units = value_unit.unit.to_s
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_volume
|
|
|
|
return unless self.quantity.present?
|
2018-09-11 10:38:07 -05:00
|
|
|
if ingredient && ingredient.density?
|
2016-02-27 20:12:41 -06:00
|
|
|
density = UnitConversion.parse(ingredient.density)
|
|
|
|
if density.density?
|
|
|
|
value_unit = UnitConversion.parse(self.quantity, self.units)
|
2018-09-11 22:56:26 -05:00
|
|
|
value_unit = value_unit.to_volume(density).auto_unit
|
2016-02-27 20:12:41 -06:00
|
|
|
|
|
|
|
self.quantity = value_unit.pretty_value
|
|
|
|
self.units = value_unit.unit.to_s
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_mass
|
2018-09-11 10:38:07 -05:00
|
|
|
return unless self.quantity.present?
|
|
|
|
if ingredient && ingredient.density?
|
2018-09-11 22:56:26 -05:00
|
|
|
UnitConversion::with_custom_units(self.ingredient.custom_units) do
|
|
|
|
density = UnitConversion.parse(ingredient.density)
|
|
|
|
if density.density?
|
|
|
|
value_unit = UnitConversion.parse(self.quantity, self.units)
|
|
|
|
value_unit = value_unit.to_mass(density).auto_unit
|
|
|
|
|
|
|
|
self.quantity = value_unit.pretty_value
|
|
|
|
self.units = value_unit.unit.to_s
|
2018-09-11 10:38:07 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-07-05 16:31:36 -05:00
|
|
|
end
|
|
|
|
|
2018-09-11 17:13:22 -05:00
|
|
|
# Based on current quantity and units, return the value with with to multiply each nutrient to get the total amount
|
|
|
|
# supplied by this ingredient
|
|
|
|
def calculate_nutrition_ratio
|
2018-09-11 22:56:26 -05:00
|
|
|
if self.ingredient.blank? || self.quantity.blank?
|
|
|
|
return nil
|
|
|
|
end
|
2018-09-11 17:13:22 -05:00
|
|
|
|
2018-09-11 22:56:26 -05:00
|
|
|
UnitConversion::with_custom_units(self.ingredient.custom_units) do
|
|
|
|
unit_is_each = self.units.blank? || %w(each ech item items per recipe recipes).include?(self.units.downcase)
|
|
|
|
unit = UnitConversion::parse(self.quantity, unit_is_each ? 'each' : self.units)
|
|
|
|
nutrition_unit = self.ingredient.nutrition_unit
|
2018-09-11 10:38:07 -05:00
|
|
|
|
2018-09-11 22:56:26 -05:00
|
|
|
converted_unit = unit.convert(nutrition_unit.unit.unit, self.ingredient.density)
|
|
|
|
converted_unit.scale(1.0 / nutrition_unit.value.value).raw_value
|
2016-07-05 16:31:36 -05:00
|
|
|
end
|
2018-09-11 22:56:26 -05:00
|
|
|
rescue UnitConversion::UnparseableUnitError
|
|
|
|
nil
|
2016-07-05 16:31:36 -05:00
|
|
|
end
|
|
|
|
|
2016-07-27 22:30:57 -05:00
|
|
|
def log_copy
|
|
|
|
copy = RecipeIngredient.new
|
2018-09-11 10:38:07 -05:00
|
|
|
copy.food = self.food
|
2018-08-27 16:44:45 -05:00
|
|
|
copy.recipe_as_ingredient = self.recipe_as_ingredient
|
2016-07-27 22:30:57 -05:00
|
|
|
copy.name = self.name
|
|
|
|
copy.sort_order = self.sort_order
|
|
|
|
copy.quantity = self.quantity
|
|
|
|
copy.units = self.units
|
|
|
|
copy.preparation = self.preparation
|
|
|
|
|
|
|
|
copy
|
|
|
|
end
|
|
|
|
|
2016-01-12 18:43:00 -06:00
|
|
|
end
|