1
0

Summarizing nutrients + displaying meal summary

This commit is contained in:
cryptogopher 2020-05-01 18:34:40 +02:00
parent 9ccd41b422
commit f6a7f0219e
4 changed files with 37 additions and 13 deletions

View File

@ -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

View File

@ -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

View File

@ -22,8 +22,9 @@
<%= q.name %>
</td>
<% end %>
<td style="width:<%= 100/total_width %>%" rowspan="2"></td>
<td style="width:<%= 100/total_width %>%"></td>
</tr>
<tr>
<td class="quantityhead" style="width:<%= 100/total_width %>%;">
<%= "[#{@amount_mfu_unit.shortname}]" %>

View File

@ -12,7 +12,7 @@
</td>
<td id="notes-links-<%= m.id %>" class="unwrappable"
style="text-align: left; border-right: none; width: 1%;">
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" %>
</td>
<% if false %>
<td id="notes-<%= m.id %>" class="notes unwrappable" colspan="<%= @quantities.length + 1 %>"
style="text-align: left; border-left: none;">
<%= notes(m) %>
</td>
<% end %>
<td class="value ellipsible"></td>
<% @quantities.each do |q| %>
<td class="value ellipsible">
<%= format_value(@nutrient_summary[q][m], @nutrients[q][:precision]) %>
</td>
<% end %>
<td class="action unwrappable" style="width: 1%;"><%= meal_links(m) %></td>
</tr>
@ -52,4 +61,3 @@
</td>
</tr>
<% end %>
</tr>