Fixed yield parser; fixed N+1 query issue
This commit is contained in:
parent
16bc8b562b
commit
c71e356195
@ -98,7 +98,7 @@ class RecipesController < ApplicationController
|
|||||||
private
|
private
|
||||||
# Use callbacks to share common setup or constraints between actions.
|
# Use callbacks to share common setup or constraints between actions.
|
||||||
def set_recipe
|
def set_recipe
|
||||||
@recipe = Recipe.includes(recipe_ingredients: :ingredient).find(params[:id])
|
@recipe = Recipe.includes(recipe_ingredients: {ingredient: :ingredient_units }).find(params[:id])
|
||||||
end
|
end
|
||||||
|
|
||||||
# Never trust parameters from the scary internet, only allow the white list through.
|
# Never trust parameters from the scary internet, only allow the white list through.
|
||||||
|
@ -4,15 +4,19 @@ class YieldParser
|
|||||||
|
|
||||||
def self.parse(yield_string)
|
def self.parse(yield_string)
|
||||||
|
|
||||||
match = /(\d+(?:\.\d*)?)\s*(\w[\w\s]*)?/.match(yield_string)
|
begin
|
||||||
|
vu = UnitConversion::parse(yield_string)
|
||||||
|
rescue UnparseableUnitError
|
||||||
|
vu = nil
|
||||||
|
end
|
||||||
|
|
||||||
case
|
case
|
||||||
when match.nil?
|
when vu.nil?
|
||||||
nil
|
nil
|
||||||
when match[2]
|
when vu.unit.nil?
|
||||||
Result.new(match[1].to_f, match[2].strip.singularize)
|
Result.new(vu.raw_value.to_f, 'each')
|
||||||
else
|
else
|
||||||
Result.new(match[1].to_f, 'each')
|
Result.new(vu.raw_value.to_f, vu.unit.to_s.singularize)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -7,6 +7,8 @@ RSpec.describe YieldParser do
|
|||||||
expect(YieldParser.parse('4 servings')).to eq YieldParser::Result.new(4.0, 'serving')
|
expect(YieldParser.parse('4 servings')).to eq YieldParser::Result.new(4.0, 'serving')
|
||||||
expect(YieldParser.parse('3 pancakes')).to eq YieldParser::Result.new(3.0, 'pancake')
|
expect(YieldParser.parse('3 pancakes')).to eq YieldParser::Result.new(3.0, 'pancake')
|
||||||
expect(YieldParser.parse('13.5 large croutons')).to eq YieldParser::Result.new(13.5, 'large crouton')
|
expect(YieldParser.parse('13.5 large croutons')).to eq YieldParser::Result.new(13.5, 'large crouton')
|
||||||
|
|
||||||
|
expect(YieldParser.parse('1 1/2 pints')).to eq YieldParser::Result.new(1.5, 'pint')
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
Loading…
Reference in New Issue
Block a user