diff --git a/app/controllers/meals_controller.rb b/app/controllers/meals_controller.rb index 34e8a5c..0ff68b7 100644 --- a/app/controllers/meals_controller.rb +++ b/app/controllers/meals_controller.rb @@ -86,23 +86,34 @@ class MealsController < ApplicationController ingredients = @project.meal_ingredients @amount_mfu_unit = ingredients - .each_with_object(Hash.new(0)) { |i, h| h[i.food.ref_unit] += 1 }.max_by(&:last).first + .each_with_object(Hash.new(0)) { |i, h| h[i.food.ref_unit] += 1 } + .max_by(&:last).try(&:first) @nutrients = {} + @nutrient_summary = Hash.new { |h,k| h[k] = Hash.new(BigDecimal(0)) } @quantities.each do |q| - @nutrients[q] = ingredients.map do |i| + @nutrients[q] = ingredients.find_all { |i| foods[i.food][q] }.map do |i| n_amount, n_unit = foods[i.food][q] - [i, [n_amount && n_amount * i.amount / i.food.ref_amount, n_unit]] + [i, [n_amount * i.amount / i.food.ref_amount, n_unit]] end.to_h - max_value = @nutrients[q].values.max_by { |a, u| a || 0 }.first - @nutrients[q][:mfu_unit] = @nutrients[q] - .each_with_object(Hash.new(0)) { |(i, v), h| h[v.last] += 1 }.max_by(&:last).first - @nutrients[q][:precision] = max_value && [3 - max_value.exponent, 0].max + mfu_unit = @nutrients[q].each_with_object(Hash.new(0)) { |(i, v), h| h[v.last] += 1 } + .max_by(&:last).try(&:first) + max_value = @nutrients[q].values.max_by { |a, u| a || 0 }.try(&:first) || BigDecimal(0) + precision = [3 - max_value.exponent, 0].max + + # TODO: summing up ingredients should take units into account + @nutrients[q].each do |i, (a, v)| + meal = i.composition + @nutrient_summary[q][meal] += a + @nutrient_summary[q][meal.display_date] += a + end + + @nutrients[q][:mfu_unit] = mfu_unit + @nutrients[q][:precision] = precision end - @meals_by_date = @project.meals.reject { |m,*| m.new_record? } - .sort_by { |m,*| m.eaten_at || m.created_at } - .group_by { |m,*| m.eaten_at ? m.eaten_at.to_date : Date.current } + @meals_by_date = @project.meals.reject(&:new_record?) + .sort_by { |m| m.eaten_at || m.created_at }.group_by(&:display_date) end end diff --git a/app/models/meal.rb b/app/models/meal.rb index 5ce67f8..2d7fbe3 100644 --- a/app/models/meal.rb +++ b/app/models/meal.rb @@ -39,4 +39,8 @@ class Meal < ActiveRecord::Base def toggle_eaten! update(eaten_at: self.eaten_at ? nil : DateTime.current) end + + def display_date + self.eaten_at ? self.eaten_at.to_date : Date.current + end end diff --git a/app/views/meals/_index.html.erb b/app/views/meals/_index.html.erb index 77d8e40..7056d4c 100644 --- a/app/views/meals/_index.html.erb +++ b/app/views/meals/_index.html.erb @@ -22,8 +22,9 @@ <%= q.name %> <% end %> - + + <%= "[#{@amount_mfu_unit.shortname}]" %> diff --git a/app/views/meals/_show.html.erb b/app/views/meals/_show.html.erb index 1f559ad..277a45c 100644 --- a/app/views/meals/_show.html.erb +++ b/app/views/meals/_show.html.erb @@ -12,7 +12,7 @@ + style="text-align: left; border-left: none; width: 1%;"> <%= link_to l(:button_cancel), '', class: 'icon icon-cancel', style: 'display: none;', onclick: "$(event.target).closest('tr').find('form').remove(); $('td[id=notes-#{m.id}]').contents().show(); @@ -26,10 +26,19 @@ class: "icon icon-wiki-page" %> +<% if false %> <%= notes(m) %> +<% end %> + + <% @quantities.each do |q| %> + + <%= format_value(@nutrient_summary[q][m], @nutrients[q][:precision]) %> + + <% end %> + <%= meal_links(m) %> @@ -52,4 +61,3 @@ <% end %> -