parsley/app/models/recipe_ingredient.rb
2016-01-21 11:45:46 -06:00

48 lines
1.0 KiB
Ruby

class RecipeIngredient < ActiveRecord::Base
belongs_to :ingredient
belongs_to :recipe, inverse_of: :recipe_ingredients
validates :sort_order, presence: true
validates :custom_density, density: true, allow_blank: true
def custom_name
if self.ingredient_id.present?
self.ingredient.name
else
super
end
end
def custom_density
if self.ingredient_id.present?
self.ingredient.density
else
super
end
end
def display_name
if quantity.present? && units.present?
"#{quantity} #{units} of #{custom_name}"
elsif quantity.present?
"#{quantity} #{custom_name}"
elsif units.present?
"#{units} #{custom_name}"
else
custom_name
end
end
def scale(factor, auto_unit = false)
if factor.present? && self.quantity.present? && factor != '1'
self.quantity = UnitConversion.convert(self.quantity, factor, nil, nil)
if auto_unit
self.quantity, self.units = UnitConversion.auto_unit(self.quantity, self.units)
end
end
end
end