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 @@
> -
- <%= f.label :water, "Grams of Water", class: 'control-label' %> - <%= f.text_field :water, class: 'form-control' %> -
+
+
+
+ <%= f.label :water, "Grams of Water", class: 'control-label' %> + <%= f.text_field :water, class: 'form-control' %> +
-
- <%= f.label :protein, "Grams of Protein", class: 'control-label' %> - <%= f.text_field :protein, class: 'form-control' %> -
+
+ <%= f.label :protein, "Grams of Protein", class: 'control-label' %> + <%= f.text_field :protein, class: 'form-control' %> +
-
- <%= f.label :lipids, "Grams of Fat", class: 'control-label' %> - <%= f.text_field :lipids, class: 'form-control' %> -
+
+ <%= f.label :lipids, "Grams of Fat", class: 'control-label' %> + <%= f.text_field :lipids, class: 'form-control' %> +
-
- <%= f.label :carbohydrates, "Grams of Carbohydrates", class: 'control-label' %> - <%= f.text_field :carbohydrates, class: 'form-control' %> -
+
+ <%= f.label :carbohydrates, "Grams of Carbohydrates", class: 'control-label' %> + <%= f.text_field :carbohydrates, class: 'form-control' %> +
-
- <%= f.label :kcal, "Calories", class: 'control-label' %> - <%= f.text_field :kcal, class: 'form-control' %> -
+
+ <%= f.label :kcal, "Calories", class: 'control-label' %> + <%= f.text_field :kcal, class: 'form-control' %> +
-
- <%= f.label :fiber, "Grams of Fiber", class: 'control-label' %> - <%= f.text_field :fiber, class: 'form-control' %> -
+
+ <%= f.label :fiber, "Grams of Fiber", class: 'control-label' %> + <%= f.text_field :fiber, class: 'form-control' %> +
-
- <%= f.label :sugar, "Grams of Sugar", class: 'control-label' %> - <%= f.text_field :sugar, class: 'form-control' %> +
+ <%= f.label :sugar, "Grams of Sugar", class: 'control-label' %> + <%= f.text_field :sugar, class: 'form-control' %> +
+
+
+
+ <%= f.label :calcium, "Miligrams of Calcium", class: 'control-label' %> + <%= f.text_field :calcium, class: 'form-control' %> +
+ +
+ <%= f.label :sodium, "Miligrams of Sodium", class: 'control-label' %> + <%= f.text_field :sodium, class: 'form-control' %> +
+ +
+ <%= f.label :vit_k, "Micrograms of Vitamin K", class: 'control-label' %> + <%= f.text_field :vit_k, class: 'form-control' %> +
+
diff --git a/app/views/recipes/show.html.erb b/app/views/recipes/show.html.erb index 5811135..6120783 100644 --- a/app/views/recipes/show.html.erb +++ b/app/views/recipes/show.html.erb @@ -99,48 +99,16 @@ Total - - Calories - <% if @recipe.parsed_yield %> - <%= nutrition_data.kcal_per(@recipe.parsed_yield.number) %> - <% end %> - <%= nutrition_data.kcal %> - - - Grams Protein - <% if @recipe.parsed_yield %> - <%= nutrition_data.protein_per(@recipe.parsed_yield.number) %> - <% end %> - <%= nutrition_data.protein %> - - - Grams Fat - <% if @recipe.parsed_yield %> - <%= nutrition_data.lipids_per(@recipe.parsed_yield.number) %> - <% end %> - <%= nutrition_data.lipids %> - - - Grams Carbohydrates - <% if @recipe.parsed_yield %> - <%= nutrition_data.carbohydrates_per(@recipe.parsed_yield.number) %> - <% end %> - <%= nutrition_data.carbohydrates %> - - - Grams Sugar - <% if @recipe.parsed_yield %> - <%= nutrition_data.sugar_per(@recipe.parsed_yield.number) %> - <% end %> - <%= nutrition_data.sugar %> - - - Grams Fiber - <% if @recipe.parsed_yield %> - <%= nutrition_data.fiber_per(@recipe.parsed_yield.number) %> - <% end %> - <%= nutrition_data.fiber %> - + + <%= nutrient_row(@recipe, nutrition_data, 'Calories', :kcal) %> + <%= nutrient_row(@recipe, nutrition_data, 'g Protein', :protein) %> + <%= nutrient_row(@recipe, nutrition_data, 'g Fat', :lipids) %> + <%= nutrient_row(@recipe, nutrition_data, 'g Carbohydrates', :carbohydrates) %> + <%= nutrient_row(@recipe, nutrition_data, 'mg Sodium', :sodium) %> + <%= nutrient_row(@recipe, nutrition_data, 'g Sugar', :sugar) %> + <%= nutrient_row(@recipe, nutrition_data, 'g Fiber', :fiber) %> + <%= nutrient_row(@recipe, nutrition_data, 'µg Vitamin K', :vit_k) %> + <% end %> diff --git a/db/migrate/20160622171944_add_nutrients_to_usda_foods.rb b/db/migrate/20160622171944_add_nutrients_to_usda_foods.rb new file mode 100644 index 0000000..812513d --- /dev/null +++ b/db/migrate/20160622171944_add_nutrients_to_usda_foods.rb @@ -0,0 +1,24 @@ +class AddNutrientsToUsdaFoods < ActiveRecord::Migration + def change + change_table 'usda_foods' do |t| + t.integer :calcium + t.decimal :iron, precision: 10, scale: 2 + t.integer :magnesium + t.integer :phosphorus + t.integer :potassium + t.integer :sodium + t.decimal :zinc, precision: 10, scale: 2 + t.decimal :copper, precision: 10, scale: 3 + t.decimal :manganese, precision: 10, scale: 3 + t.decimal :vit_c, precision: 10, scale: 1 + t.decimal :vit_b6, precision: 10, scale: 3 + t.decimal :vit_b12, precision: 10, scale: 2 + t.integer :vit_a + t.decimal :vit_e, precision: 10, scale: 2 + t.decimal :vit_d, precision: 10, scale: 1 + t.decimal :vit_k, precision: 10, scale: 1 + t.decimal :cholesterol, precision: 10, scale: 3 + + end + end +end diff --git a/db/migrate/20160622180838_add_nutrients_to_ingredients.rb b/db/migrate/20160622180838_add_nutrients_to_ingredients.rb new file mode 100644 index 0000000..79595f2 --- /dev/null +++ b/db/migrate/20160622180838_add_nutrients_to_ingredients.rb @@ -0,0 +1,24 @@ +class AddNutrientsToIngredients < ActiveRecord::Migration + def change + change_table 'ingredients' do |t| + t.integer :calcium + t.decimal :iron, precision: 10, scale: 2 + t.integer :magnesium + t.integer :phosphorus + t.integer :potassium + t.integer :sodium + t.decimal :zinc, precision: 10, scale: 2 + t.decimal :copper, precision: 10, scale: 3 + t.decimal :manganese, precision: 10, scale: 3 + t.decimal :vit_c, precision: 10, scale: 1 + t.decimal :vit_b6, precision: 10, scale: 3 + t.decimal :vit_b12, precision: 10, scale: 2 + t.integer :vit_a + t.decimal :vit_e, precision: 10, scale: 2 + t.decimal :vit_d, precision: 10, scale: 1 + t.decimal :vit_k, precision: 10, scale: 1 + t.decimal :cholesterol, precision: 10, scale: 3 + + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 675f142..d309b08 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20160403205734) do +ActiveRecord::Schema.define(version: 20160622180838) do create_table "ingredients", force: :cascade do |t| t.string "name" @@ -29,6 +29,23 @@ ActiveRecord::Schema.define(version: 20160403205734) do t.decimal "fiber", precision: 10, scale: 1 t.decimal "sugar", precision: 10, scale: 2 t.integer "user_id" + t.integer "calcium" + t.decimal "iron", precision: 10, scale: 2 + t.integer "magnesium" + t.integer "phosphorus" + t.integer "potassium" + t.integer "sodium" + t.decimal "zinc", precision: 10, scale: 2 + t.decimal "copper", precision: 10, scale: 3 + t.decimal "manganese", precision: 10, scale: 3 + t.decimal "vit_c", precision: 10, scale: 1 + t.decimal "vit_b6", precision: 10, scale: 3 + t.decimal "vit_b12", precision: 10, scale: 2 + t.integer "vit_a" + t.decimal "vit_e", precision: 10, scale: 2 + t.decimal "vit_d", precision: 10, scale: 1 + t.decimal "vit_k", precision: 10, scale: 1 + t.decimal "cholesterol", precision: 10, scale: 3 end create_table "recipe_ingredients", force: :cascade do |t| @@ -100,6 +117,23 @@ ActiveRecord::Schema.define(version: 20160403205734) do t.string "scientific_name" t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.integer "calcium" + t.decimal "iron", precision: 10, scale: 2 + t.integer "magnesium" + t.integer "phosphorus" + t.integer "potassium" + t.integer "sodium" + t.decimal "zinc", precision: 10, scale: 2 + t.decimal "copper", precision: 10, scale: 3 + t.decimal "manganese", precision: 10, scale: 3 + t.decimal "vit_c", precision: 10, scale: 1 + t.decimal "vit_b6", precision: 10, scale: 3 + t.decimal "vit_b12", precision: 10, scale: 2 + t.integer "vit_a" + t.decimal "vit_e", precision: 10, scale: 2 + t.decimal "vit_d", precision: 10, scale: 1 + t.decimal "vit_k", precision: 10, scale: 1 + t.decimal "cholesterol", precision: 10, scale: 3 end add_index "usda_foods", ["long_description"], name: "index_usda_foods_on_long_description" diff --git a/lib/usda_importer.rb b/lib/usda_importer.rb index bc8b483..eb76e77 100644 --- a/lib/usda_importer.rb +++ b/lib/usda_importer.rb @@ -75,7 +75,24 @@ class UsdaImporter gram_weight_2: 'GmWt_2', gram_weight_desc_1: 'GmWt_Desc1', gram_weight_desc_2: 'GmWt_Desc2', - refuse_percent: 'Refuse_Pct' + refuse_percent: 'Refuse_Pct', + calcium: 'Calcium', + iron: 'Iron', + magnesium: 'Magnesium', + phosphorus: 'Phosphorus', + potassium: 'Potassium', + sodium: 'Sodium', + zinc: 'Zinc', + copper: 'Copper', + manganese: 'Manganese', + vit_c: 'Vit_C', + vit_b6: 'Vit_B6', + vit_b12: 'Vit_B12', + vit_a: 'Vit_A_RAE', + vit_e: 'Vit_E', + vit_d: 'Vit_D_mcg', + vit_k: 'Vit_K', + cholesterol: 'Cholestrl' } },