parsley/app/models/recipe.rb

206 lines
4.7 KiB
Ruby
Raw Normal View History

2016-10-14 12:19:00 -05:00
class Recipe < ApplicationRecord
2018-09-11 10:38:07 -05:00
include Ingredient
include TokenizedLike
2016-01-12 18:43:00 -06:00
2016-01-13 17:10:43 -06:00
has_many :recipe_ingredients, -> { order :sort_order }, inverse_of: :recipe, dependent: :destroy
belongs_to :user
2016-01-13 17:10:43 -06:00
2018-07-15 17:00:25 -05:00
has_and_belongs_to_many :tags, autosave: true
2016-10-14 12:47:24 -05:00
2016-07-27 22:30:57 -05:00
scope :undeleted, -> { where('deleted <> ? OR deleted IS NULL', true) }
scope :not_log, -> { where('is_log <> ? OR is_log IS NULL', true) }
scope :active, -> { undeleted.not_log }
2016-01-18 19:41:26 -06:00
2016-01-13 17:10:43 -06:00
accepts_nested_attributes_for :recipe_ingredients, allow_destroy: true
validates :name, presence: true
validates :total_time, numericality: true, allow_blank: true
validates :active_time, numericality: true, allow_blank: true
2018-04-17 09:59:38 -05:00
attr_accessor :converted_scale, :converted_system, :converted_unit
2018-07-15 17:00:25 -05:00
def cache_key
[
'recipes',
self.id.to_s,
self.updated_at.to_i.to_s,
converted_scale || '-',
converted_system || '-',
converted_unit || '-'
].join('/')
end
2016-01-20 18:37:28 -06:00
def scale(factor, auto_unit = false)
2018-04-17 09:59:38 -05:00
self.converted_scale = factor
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
2018-07-15 17:00:25 -05:00
self
2016-01-18 19:41:26 -06:00
end
2016-02-27 20:12:41 -06:00
def convert_to_metric
2018-04-17 09:59:38 -05:00
self.converted_system = 'metric'
2016-02-27 20:12:41 -06:00
recipe_ingredients.each do |ri|
ri.to_metric
end
2018-07-15 17:00:25 -05:00
self
2016-02-27 20:12:41 -06:00
end
def convert_to_standard
2018-04-17 09:59:38 -05:00
self.converted_system = 'standard'
2016-02-27 20:12:41 -06:00
recipe_ingredients.each do |ri|
ri.to_standard
end
2018-07-15 17:00:25 -05:00
self
2016-02-27 20:12:41 -06:00
end
def convert_to_mass
2018-04-17 09:59:38 -05:00
self.converted_unit = 'mass'
2016-02-27 20:12:41 -06:00
recipe_ingredients.each do |ri|
ri.to_mass
end
2018-07-15 17:00:25 -05:00
self
2016-02-27 20:12:41 -06:00
end
def convert_to_volume
2018-04-17 09:59:38 -05:00
self.converted_unit = 'volume'
2016-02-27 20:12:41 -06:00
recipe_ingredients.each do |ri|
ri.to_volume
end
2018-07-15 17:00:25 -05:00
self
2016-02-27 20:12:41 -06:00
end
2018-09-11 17:13:22 -05:00
def yields=(val)
@yields_list = nil
super
end
2018-08-27 16:44:45 -05:00
def yields_list
2018-09-11 22:56:26 -05:00
@yields_list ||= self.yields.to_s.split(',').concat(['1 each']).map { |y| y.strip }.select { |y| y.present? }.map do |y|
2018-09-11 17:13:22 -05:00
begin
UnitConversion::parse(y)
rescue UntConversion::UnparseableUnitError
nil
end
end.compact
2018-08-27 16:44:45 -05:00
end
2016-10-20 15:48:33 -05:00
def tag_names
self.tags.map { |t| t.name }
end
def tag_names=(names)
names = Array.wrap(names).map { |n| n.to_s }.select { |n| n.length > 0 }
existing_tags = Tag.by_name(names)
new_tags = names.select { |n| existing_tags.none? { |t| t.is?(n) } }
self.tags = existing_tags
new_tags.each do |n|
self.tags << Tag.new(name: n)
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
2018-09-11 22:56:26 -05:00
def nutrition_errors
nutrition_data.errors
end
2016-09-28 17:08:43 -05:00
def update_rating!
self.rating = Log.for_recipe(self).for_user(self.user_id).where('rating IS NOT NULL').average(:rating)
save(validate: false)
end
2016-07-27 22:30:57 -05:00
# Creates a copy of this recipe suitable for associating to a log
def log_copy(user)
copy = Recipe.new
copy.user = user
copy.is_log = true
copy.name = self.name
copy.description = self.description
copy.source = self.source
copy.yields = self.yields
copy.total_time = self.total_time
copy.active_time = self.active_time
2017-04-14 16:40:38 -05:00
copy.step_text = self.step_text
2016-07-27 22:30:57 -05:00
self.recipe_ingredients.each do |ri|
copy.recipe_ingredients << ri.log_copy
end
copy
end
2018-09-11 10:38:07 -05:00
def density
mas = yields_list.detect { |y| y.mass? }
vol = yields_list.detect { |y| y.volume? }
if mas && vol
"#{mas.value.value / vol.value.value} #{mas.unit.original_unit}/#{vol.unit.original_unit}"
else
nil
end
end
def custom_units
2018-09-11 22:56:26 -05:00
arbitrary = self.yields_list.select { |y| !y.mass? && !y.volume? }
2018-09-11 17:13:22 -05:00
mass = self.yields_list.select { |y| y.mass? }
volume = self.yields_list.select { |y| y.volume? }
primary_unit = mass.first || volume.first
2018-09-11 10:38:07 -05:00
2018-09-11 22:56:26 -05:00
if primary_unit
cus = {}
arbitrary.each do |y|
ratio = 1
if y.value.value != 1
ratio = 1.0 / y.value.value
end
2018-09-11 10:38:07 -05:00
2018-09-11 22:56:26 -05:00
cus[y.unit.unit] = primary_unit.scale(ratio)
end
cus
else
{}
end
2018-09-11 10:38:07 -05:00
end
2018-09-11 22:56:26 -05:00
def nutrition_unit
UnitConversion.parse('1 each')
2018-09-11 10:38:07 -05:00
end
def self.for_criteria(criteria)
2018-07-15 17:00:25 -05:00
query = active.order(criteria.sort_column => criteria.sort_direction)
if criteria.name.present?
2018-04-01 22:32:13 -05:00
query = query.matches_tokens(:name, criteria.name.split(' '))
end
if criteria.tags.present?
tags = Tag.by_name(criteria.tags.split)
query = query.where(id: tags.joins(:recipes).pluck('recipes.id'))
end
2018-07-15 17:00:25 -05:00
query.page(criteria.page).per(criteria.per)
end
2018-09-12 09:43:50 -05:00
def self.search_by_name(query)
tokens = query.to_s.split(' ')
if tokens.empty?
Recipe.none
else
Recipe.matches_tokens(:name, tokens)
end
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