diff --git a/app/controllers/recipes_controller.rb b/app/controllers/recipes_controller.rb
index af6fe81..1dbd94e 100644
--- a/app/controllers/recipes_controller.rb
+++ b/app/controllers/recipes_controller.rb
@@ -98,7 +98,7 @@ class RecipesController < ApplicationController
private
# Use callbacks to share common setup or constraints between actions.
def set_recipe
- @recipe = Recipe.find(params[:id])
+ @recipe = Recipe.includes(recipe_ingredients: :ingredient).find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
diff --git a/app/decorators/nutrition_data_decorator.rb b/app/decorators/nutrition_data_decorator.rb
index d43a935..f813470 100644
--- a/app/decorators/nutrition_data_decorator.rb
+++ b/app/decorators/nutrition_data_decorator.rb
@@ -1,6 +1,6 @@
class NutritionDataDecorator < BaseDecorator
- [:protein, :lipids, :carbohydrates, :kcal, :fiber, :sugar].each do |m|
+ NutritionData::NUTRIENTS.each do |m|
def format_number(n)
'%.1f' % n
diff --git a/app/helpers/recipes_helper.rb b/app/helpers/recipes_helper.rb
index bfc051e..c9ec10e 100644
--- a/app/helpers/recipes_helper.rb
+++ b/app/helpers/recipes_helper.rb
@@ -22,4 +22,14 @@ module RecipesHelper
end
}.compact.reverse.join(' ')
end
+
+ def nutrient_row(recipe, nutrients, heading, nutrient_name)
+ content_tag('tr') do
+ [
+ content_tag('td', heading),
+ recipe.parsed_yield ? content_tag('td', nutrients.send("#{nutrient_name}_per".to_sym, recipe.parsed_yield.number)) : nil,
+ content_tag('td', nutrients.send("#{nutrient_name}".to_sym))
+ ].compact.join("\n".html_safe).html_safe
+ end
+ end
end
diff --git a/app/models/ingredient.rb b/app/models/ingredient.rb
index 661fa93..ffd5f85 100644
--- a/app/models/ingredient.rb
+++ b/app/models/ingredient.rb
@@ -40,15 +40,38 @@ class Ingredient < ActiveRecord::Base
def set_usda_food(food)
return unless food
- self.ndbn = food.ndbn
- self.water = food.water
- self.protein = food.protein
+ copy_fields = [
+ :water,
+ :ash,
+ :protein,
+ :kcal,
+ :fiber,
+ :sugar,
+ :carbohydrates,
+ :calcium,
+ :iron,
+ :magnesium,
+ :phosphorus,
+ :potassium,
+ :sodium,
+ :zinc,
+ :copper,
+ :manganese,
+ :vit_c,
+ :vit_b6,
+ :vit_b12,
+ :vit_a,
+ :vit_e,
+ :vit_d,
+ :vit_k,
+ :cholesterol
+ ]
+
+ copy_fields.each do |f|
+ self.send("#{f}=".to_sym, food.send(f.to_sym))
+ end
+
self.lipids = food.lipid
- self.carbohydrates = food.carbohydrates
- self.ash = food.ash
- self.kcal = food.kcal
- self.fiber = food.fiber
- self.sugar = food.sugar
self.density = food.density_best_guess
end
diff --git a/app/models/nutrition_data.rb b/app/models/nutrition_data.rb
index 5017bd8..2e55106 100644
--- a/app/models/nutrition_data.rb
+++ b/app/models/nutrition_data.rb
@@ -1,15 +1,40 @@
class NutritionData
- attr_reader :protein, :lipids, :carbohydrates, :kcal, :fiber, :sugar, :errors
+ NUTRIENTS = [
+ :protein,
+ :lipids,
+ :kcal,
+ :fiber,
+ :sugar,
+ :carbohydrates,
+ :calcium,
+ :iron,
+ :magnesium,
+ :phosphorus,
+ :potassium,
+ :sodium,
+ :zinc,
+ :copper,
+ :manganese,
+ :vit_c,
+ :vit_b6,
+ :vit_b12,
+ :vit_a,
+ :vit_e,
+ :vit_d,
+ :vit_k,
+ :cholesterol
+ ]
+
+ attr_reader :errors
+ attr_reader *NUTRIENTS
def initialize(recipe_ingredients)
@errors = []
- @protein = 0.0
- @lipids = 0.0
- @kcal = 0.0
- @fiber = 0.0
- @sugar = 0.0
- @carbohydrates = 0.0
+
+ NUTRIENTS.each do |n|
+ self.instance_variable_set("@#{n}".to_sym, 0.0)
+ end
valid_ingredients = []
@@ -23,12 +48,10 @@ class NutritionData
end
end
- keys = [:protein, :lipids, :kcal, :fiber, :sugar, :carbohydrates]
-
valid_ingredients.each do |i|
grams = i.to_grams
- keys.each do |k|
+ NUTRIENTS.each do |k|
value = i.ingredient.send(k)
if value.present?
value = value.to_f
@@ -41,7 +64,7 @@ class NutritionData
end
end
- keys.each do |k|
+ NUTRIENTS.each do |k|
v = self.instance_variable_get("@#{k}".to_sym)
self.instance_variable_set("@#{k}".to_sym, v.round(2))
end
diff --git a/app/views/ingredients/_form.html.erb b/app/views/ingredients/_form.html.erb
index 2061cee..a028562 100644
--- a/app/views/ingredients/_form.html.erb
+++ b/app/views/ingredients/_form.html.erb
@@ -42,39 +42,59 @@