From 47014118c816dff6b30f97c3d10bd133b3f26311 Mon Sep 17 00:00:00 2001 From: Dan Elbert Date: Tue, 11 Sep 2018 17:13:22 -0500 Subject: [PATCH] work --- app/models/recipe.rb | 31 ++++++++++++++++++++----------- app/models/recipe_ingredient.rb | 8 +++++++- spec/models/recipe_spec.rb | 23 +++++++++++++++++++++++ 3 files changed, 50 insertions(+), 12 deletions(-) diff --git a/app/models/recipe.rb b/app/models/recipe.rb index eea7eb6..ae5db7f 100644 --- a/app/models/recipe.rb +++ b/app/models/recipe.rb @@ -70,18 +70,19 @@ class Recipe < ApplicationRecord self end + def yields=(val) + @yields_list = nil + super + end + def yields_list - if self.yields.present? - self.yields.split(',').map { |y| y.strip }.select { |y| y.present? }.map do |y| - begin - UnitConversion::parse(y) - rescue UntConversion::UnparseableUnitError - nil - end - end.compact - else - [] - end + @yields_list ||= self.yields.to_s.split(',').concat(['1 recipe']).map { |y| y.strip }.select { |y| y.present? }.map do |y| + begin + UnitConversion::parse(y) + rescue UntConversion::UnparseableUnitError + nil + end + end.compact end def tag_names @@ -141,6 +142,14 @@ class Recipe < ApplicationRecord end def custom_units + arbitrary = self.yields_list.select { |y| !y.mass? && !y.volume } + mass = self.yields_list.select { |y| y.mass? } + volume = self.yields_list.select { |y| y.volume? } + + primary_unit = mass.first || volume.first + + Hash[arbitrary.map { |y| [y.unit.unit, primary_unit] }] + Hash[yields_list.select { |y| !y.mass? && !y.volume? && y.unit }.map { [y.unit.unit, y] }] end diff --git a/app/models/recipe_ingredient.rb b/app/models/recipe_ingredient.rb index e991016..9a6e8f7 100644 --- a/app/models/recipe_ingredient.rb +++ b/app/models/recipe_ingredient.rb @@ -127,7 +127,7 @@ class RecipeIngredient < ApplicationRecord unit = self.units.present? ? self.units.downcase : '' pair = self.ingredient.custom_units.detect do |u, e| if unit.empty? - ['each', 'ech', 'item', 'per'].include?(u.downcase) + ['each', 'ech', 'item', 'per', 'recipe'].include?(u.downcase) else [u.downcase, u.downcase.singularize, u.downcase.pluralize].any? { |uv| [unit, unit.singularize, unit.pluralize].include?(uv) } end @@ -149,6 +149,12 @@ class RecipeIngredient < ApplicationRecord gram_unit.raw_value end + # Based on current quantity and units, return the value with with to multiply each nutrient to get the total amount + # supplied by this ingredient + def calculate_nutrition_ratio + + end + def as_value_unit custom_unit = self.get_custom_unit_equivalent diff --git a/spec/models/recipe_spec.rb b/spec/models/recipe_spec.rb index 558fc0e..e2db818 100644 --- a/spec/models/recipe_spec.rb +++ b/spec/models/recipe_spec.rb @@ -1,6 +1,29 @@ require 'rails_helper' RSpec.describe Recipe, type: :model do + describe '#yields_list' do + it 'always has "1 recipe" as a yield' do + r = create(:recipe, yields: '') + l = r.yields_list + expect(l.length).to eq 1 + expect(l.first).to be_a UnitConversion::ValueUnit + expect(l.first.value.value).to eq 1 + expect(l.first.unit.unit).to eq 'recipe' + end + + it 'caches the list and resets it when yield is changed' do + r = create(:recipe, yields: '3 cups') + l1 = r.yields_list + l2 = r.yields_list + expect(l1.first).to equal(l2.first) + + r.yields = '4 cups' + l2 = r.yields_list + + expect(l1.first).not_to equal(l2.first) + end + end + describe '#update_rating!' do it 'should set rating to nil with no ratings' do