parsley/app/models/recipe_ingredient.rb

126 lines
3.2 KiB
Ruby
Raw Normal View History

2016-01-12 18:43:00 -06:00
class RecipeIngredient < ActiveRecord::Base
belongs_to :ingredient
2016-01-13 17:10:43 -06:00
belongs_to :recipe, inverse_of: :recipe_ingredients
validates :sort_order, presence: true
2016-01-12 18:43:00 -06:00
def name
2016-01-14 18:56:45 -06:00
if self.ingredient_id.present?
self.ingredient.name
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
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'
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
value_unit = value_unit.auto_unit
2016-01-20 18:37:28 -06:00
end
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?
if ingredient && ingredient.density
density = UnitConversion.parse(ingredient.density)
if density.density?
value_unit = UnitConversion.parse(self.quantity, self.units)
value_unit = value_unit.to_volume(density)
self.quantity = value_unit.pretty_value
self.units = value_unit.unit.to_s
end
end
end
def to_mass
2016-07-05 16:31:36 -05:00
value_unit = as_value_unit
density = self.ingredient.density? ? UnitConversion.parse(ingredient.density) : nil
2016-02-27 20:12:41 -06:00
2016-07-05 16:31:36 -05:00
case
when value_unit.nil?
when value_unit.mass?
self.quantity = value_unit.pretty_value
self.units = value_unit.unit.to_s
when value_unit.volume? && density && density.density?
value_unit = value_unit.to_mass(density)
2016-02-27 20:12:41 -06:00
self.quantity = value_unit.pretty_value
self.units = value_unit.unit.to_s
end
end
2016-07-05 16:31:36 -05:00
def custom_unit?
self.ingredient && self.ingredient.custom_unit_weight(self.units).present?
end
2016-02-14 19:29:34 -06:00
def can_convert_to_grams?
2016-07-05 16:31:36 -05:00
vu = as_value_unit
vu.present? && (vu.mass? || (vu.volume? && self.ingredient && self.ingredient.density.present?))
2016-02-14 19:29:34 -06:00
end
def to_grams
2016-07-05 16:31:36 -05:00
value_unit = as_value_unit
2016-02-14 19:29:34 -06:00
gram_unit = value_unit.convert('g', self.ingredient ? self.ingredient.density : nil)
gram_unit.raw_value
end
2016-07-05 16:31:36 -05:00
def as_value_unit
case
when self.quantity.blank?
nil
when self.custom_unit?
self.ingredient.custom_unit_weight(self.units).scale(self.quantity)
when self.units.present?
UnitConversion.parse(self.quantity, self.units)
else
nil
end
end
2016-07-27 22:30:57 -05:00
def log_copy
copy = RecipeIngredient.new
copy.ingredient = self.ingredient
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