66 lines
1.5 KiB
Ruby
66 lines
1.5 KiB
Ruby
class Recipe < ActiveRecord::Base
|
|
|
|
has_many :recipe_ingredients, -> { order :sort_order }, inverse_of: :recipe, dependent: :destroy
|
|
has_many :recipe_steps, -> { order :sort_order }, inverse_of: :recipe, dependent: :destroy
|
|
belongs_to :user
|
|
|
|
scope :active, -> { where('deleted <> ? OR deleted IS NULL', true) }
|
|
|
|
accepts_nested_attributes_for :recipe_ingredients, allow_destroy: true
|
|
accepts_nested_attributes_for :recipe_steps, allow_destroy: true
|
|
|
|
validates :name, presence: true
|
|
validates :total_time, numericality: true, allow_blank: true
|
|
validates :active_time, numericality: true, allow_blank: true
|
|
|
|
def scale(factor, auto_unit = false)
|
|
recipe_ingredients.each do |ri|
|
|
ri.scale(factor, auto_unit)
|
|
end
|
|
end
|
|
|
|
def convert_to_metric
|
|
recipe_ingredients.each do |ri|
|
|
ri.to_metric
|
|
end
|
|
end
|
|
|
|
def convert_to_standard
|
|
recipe_ingredients.each do |ri|
|
|
ri.to_standard
|
|
end
|
|
end
|
|
|
|
def convert_to_mass
|
|
recipe_ingredients.each do |ri|
|
|
ri.to_mass
|
|
end
|
|
end
|
|
|
|
def convert_to_volume
|
|
recipe_ingredients.each do |ri|
|
|
ri.to_volume
|
|
end
|
|
end
|
|
|
|
def nutrition_data(recalculate = false)
|
|
if recalculate || @nutrition_data.nil?
|
|
@nutrition_data = calculate_nutrition_data
|
|
end
|
|
@nutrition_data
|
|
end
|
|
|
|
def parsed_yield
|
|
if @parsed_yield.nil? && self.yields.present?
|
|
@parsed_yield = YieldParser.parse(self.yields)
|
|
end
|
|
@parsed_yield
|
|
end
|
|
|
|
private
|
|
|
|
def calculate_nutrition_data
|
|
NutritionData.new(recipe_ingredients)
|
|
end
|
|
end
|