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
|
|
|
|
2016-01-30 20:29:35 -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'
|
|
|
|
self.quantity = UnitConversion.convert(self.quantity, factor, nil, nil)
|
2016-01-20 18:37:28 -06:00
|
|
|
|
|
|
|
if auto_unit
|
|
|
|
self.quantity, self.units = UnitConversion.auto_unit(self.quantity, self.units)
|
|
|
|
end
|
2016-01-18 19:41:26 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-01-12 18:43:00 -06:00
|
|
|
end
|