add serving logic

This commit is contained in:
Dan Elbert 2018-09-22 01:56:39 -05:00
parent c4ef70d0e8
commit 521c62d09b
3 changed files with 17 additions and 2 deletions

View File

@ -90,8 +90,12 @@ class Recipe < Ingredient
def yields_list
@yields_list ||= self.yields.to_s.split(',').concat(['1 each']).map { |y| y.strip }.select { |y| y.present? }.map do |y|
begin
UnitConversion::parse(y)
rescue UntConversion::UnparseableUnitError
vu = UnitConversion::parse(y)
if vu.unit.nil?
vu = UnitConversion::ValueUnit.for(vu.value, 'servings')
end
vu
rescue UnitConversion::UnparseableUnitError
nil
end
end.compact

View File

@ -14,6 +14,7 @@ services:
environment:
- RAILS_USE_MEMCACHE=true
- PASSENGER_APP_ENV=production
- RAILS_ENV=production
env_file: /etc/default/parsley
volumes:
- /var/log/parsley/:/home/app/parsley/log

View File

@ -46,6 +46,16 @@ RSpec.describe Recipe, type: :model do
expect(l1.first).not_to equal(l2.first)
end
it 'converts a bare value into a servings unit' do
r = create(:recipe, yields: '3')
l = r.yields_list
expect(l.length).to eq 2
s = l.detect { |y| y.unit.unit == 'servings' }
expect(s).to be_a UnitConversion::ValueUnit
expect(s.raw_value).to eq 3
expect(s.unit.unit).to eq 'servings'
end
end
describe '#custom_units' do