work
This commit is contained in:
parent
56fe5aae35
commit
47014118c8
@ -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
|
||||
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user