work
This commit is contained in:
parent
56fe5aae35
commit
47014118c8
@ -70,18 +70,19 @@ class Recipe < ApplicationRecord
|
|||||||
self
|
self
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def yields=(val)
|
||||||
|
@yields_list = nil
|
||||||
|
super
|
||||||
|
end
|
||||||
|
|
||||||
def yields_list
|
def yields_list
|
||||||
if self.yields.present?
|
@yields_list ||= self.yields.to_s.split(',').concat(['1 recipe']).map { |y| y.strip }.select { |y| y.present? }.map do |y|
|
||||||
self.yields.split(',').map { |y| y.strip }.select { |y| y.present? }.map do |y|
|
|
||||||
begin
|
begin
|
||||||
UnitConversion::parse(y)
|
UnitConversion::parse(y)
|
||||||
rescue UntConversion::UnparseableUnitError
|
rescue UntConversion::UnparseableUnitError
|
||||||
nil
|
nil
|
||||||
end
|
end
|
||||||
end.compact
|
end.compact
|
||||||
else
|
|
||||||
[]
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def tag_names
|
def tag_names
|
||||||
@ -141,6 +142,14 @@ class Recipe < ApplicationRecord
|
|||||||
end
|
end
|
||||||
|
|
||||||
def custom_units
|
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] }]
|
Hash[yields_list.select { |y| !y.mass? && !y.volume? && y.unit }.map { [y.unit.unit, y] }]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -127,7 +127,7 @@ class RecipeIngredient < ApplicationRecord
|
|||||||
unit = self.units.present? ? self.units.downcase : ''
|
unit = self.units.present? ? self.units.downcase : ''
|
||||||
pair = self.ingredient.custom_units.detect do |u, e|
|
pair = self.ingredient.custom_units.detect do |u, e|
|
||||||
if unit.empty?
|
if unit.empty?
|
||||||
['each', 'ech', 'item', 'per'].include?(u.downcase)
|
['each', 'ech', 'item', 'per', 'recipe'].include?(u.downcase)
|
||||||
else
|
else
|
||||||
[u.downcase, u.downcase.singularize, u.downcase.pluralize].any? { |uv| [unit, unit.singularize, unit.pluralize].include?(uv) }
|
[u.downcase, u.downcase.singularize, u.downcase.pluralize].any? { |uv| [unit, unit.singularize, unit.pluralize].include?(uv) }
|
||||||
end
|
end
|
||||||
@ -149,6 +149,12 @@ class RecipeIngredient < ApplicationRecord
|
|||||||
gram_unit.raw_value
|
gram_unit.raw_value
|
||||||
end
|
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
|
def as_value_unit
|
||||||
|
|
||||||
custom_unit = self.get_custom_unit_equivalent
|
custom_unit = self.get_custom_unit_equivalent
|
||||||
|
@ -1,6 +1,29 @@
|
|||||||
require 'rails_helper'
|
require 'rails_helper'
|
||||||
|
|
||||||
RSpec.describe Recipe, type: :model do
|
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
|
describe '#update_rating!' do
|
||||||
|
|
||||||
it 'should set rating to nil with no ratings' do
|
it 'should set rating to nil with no ratings' do
|
||||||
|
Loading…
Reference in New Issue
Block a user