Fixed yield parser; fixed N+1 query issue

This commit is contained in:
Dan Elbert 2016-07-05 16:48:59 -05:00
parent 16bc8b562b
commit c71e356195
3 changed files with 12 additions and 6 deletions

View File

@ -98,7 +98,7 @@ class RecipesController < ApplicationController
private
# Use callbacks to share common setup or constraints between actions.
def set_recipe
@recipe = Recipe.includes(recipe_ingredients: :ingredient).find(params[:id])
@recipe = Recipe.includes(recipe_ingredients: {ingredient: :ingredient_units }).find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.

View File

@ -4,15 +4,19 @@ class YieldParser
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
when match.nil?
when vu.nil?
nil
when match[2]
Result.new(match[1].to_f, match[2].strip.singularize)
when vu.unit.nil?
Result.new(vu.raw_value.to_f, 'each')
else
Result.new(match[1].to_f, 'each')
Result.new(vu.raw_value.to_f, vu.unit.to_s.singularize)
end
end

View File

@ -7,6 +7,8 @@ RSpec.describe YieldParser do
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('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