parsley/app/models/recipe.rb

76 lines
1.7 KiB
Ruby
Raw Normal View History

2016-01-12 18:43:00 -06:00
class Recipe < ActiveRecord::Base
2016-01-13 17:10:43 -06:00
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
2016-01-13 17:10:43 -06:00
2016-01-18 19:41:26 -06:00
scope :active, -> { where('deleted <> ? OR deleted IS NULL', true) }
2016-01-13 17:10:43 -06:00
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
2016-01-19 17:26:23 -06:00
NAME_CUTOFF = 35
def display_name
if self.name.length >= NAME_CUTOFF
self.name[0...NAME_CUTOFF] + '...'
else
self.name
end
end
2016-01-20 18:37:28 -06:00
def scale(factor, auto_unit = false)
2016-01-18 19:41:26 -06:00
recipe_ingredients.each do |ri|
2016-01-20 18:37:28 -06:00
ri.scale(factor, auto_unit)
2016-01-18 19:41:26 -06:00
end
end
2016-02-27 20:12:41 -06:00
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
2016-02-14 19:29:34 -06:00
def nutrition_data(recalculate = false)
if recalculate || @nutrition_data.nil?
@nutrition_data = calculate_nutrition_data
end
@nutrition_data
end
2016-01-12 18:43:00 -06:00
2016-04-03 18:03:51 -05:00
def parsed_yield
if @parsed_yield.nil? && self.yields.present?
@parsed_yield = YieldParser.parse(self.yields)
end
@parsed_yield
end
2016-02-14 19:29:34 -06:00
private
def calculate_nutrition_data
NutritionData.new(recipe_ingredients)
end
2016-01-12 18:43:00 -06:00
end