Summarizing nutrients + displaying meal summary
This commit is contained in:
parent
9ccd41b422
commit
f6a7f0219e
@ -86,23 +86,34 @@ class MealsController < ApplicationController
|
|||||||
ingredients = @project.meal_ingredients
|
ingredients = @project.meal_ingredients
|
||||||
|
|
||||||
@amount_mfu_unit = 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 = {}
|
@nutrients = {}
|
||||||
|
@nutrient_summary = Hash.new { |h,k| h[k] = Hash.new(BigDecimal(0)) }
|
||||||
@quantities.each do |q|
|
@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]
|
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
|
end.to_h
|
||||||
max_value = @nutrients[q].values.max_by { |a, u| a || 0 }.first
|
|
||||||
|
|
||||||
@nutrients[q][:mfu_unit] = @nutrients[q]
|
mfu_unit = @nutrients[q].each_with_object(Hash.new(0)) { |(i, v), h| h[v.last] += 1 }
|
||||||
.each_with_object(Hash.new(0)) { |(i, v), h| h[v.last] += 1 }.max_by(&:last).first
|
.max_by(&:last).try(&:first)
|
||||||
@nutrients[q][:precision] = max_value && [3 - max_value.exponent, 0].max
|
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
|
end
|
||||||
|
|
||||||
@meals_by_date = @project.meals.reject { |m,*| m.new_record? }
|
@meals_by_date = @project.meals.reject(&:new_record?)
|
||||||
.sort_by { |m,*| m.eaten_at || m.created_at }
|
.sort_by { |m| m.eaten_at || m.created_at }.group_by(&:display_date)
|
||||||
.group_by { |m,*| m.eaten_at ? m.eaten_at.to_date : Date.current }
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -39,4 +39,8 @@ class Meal < ActiveRecord::Base
|
|||||||
def toggle_eaten!
|
def toggle_eaten!
|
||||||
update(eaten_at: self.eaten_at ? nil : DateTime.current)
|
update(eaten_at: self.eaten_at ? nil : DateTime.current)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def display_date
|
||||||
|
self.eaten_at ? self.eaten_at.to_date : Date.current
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
@ -22,8 +22,9 @@
|
|||||||
<%= q.name %>
|
<%= q.name %>
|
||||||
</td>
|
</td>
|
||||||
<% end %>
|
<% end %>
|
||||||
<td style="width:<%= 100/total_width %>%" rowspan="2"></td>
|
<td style="width:<%= 100/total_width %>%"></td>
|
||||||
</tr>
|
</tr>
|
||||||
|
|
||||||
<tr>
|
<tr>
|
||||||
<td class="quantityhead" style="width:<%= 100/total_width %>%;">
|
<td class="quantityhead" style="width:<%= 100/total_width %>%;">
|
||||||
<%= "[#{@amount_mfu_unit.shortname}]" %>
|
<%= "[#{@amount_mfu_unit.shortname}]" %>
|
||||||
|
@ -12,7 +12,7 @@
|
|||||||
</td>
|
</td>
|
||||||
|
|
||||||
<td id="notes-links-<%= m.id %>" class="unwrappable"
|
<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;',
|
<%= link_to l(:button_cancel), '', class: 'icon icon-cancel', style: 'display: none;',
|
||||||
onclick: "$(event.target).closest('tr').find('form').remove();
|
onclick: "$(event.target).closest('tr').find('form').remove();
|
||||||
$('td[id=notes-#{m.id}]').contents().show();
|
$('td[id=notes-#{m.id}]').contents().show();
|
||||||
@ -26,10 +26,19 @@
|
|||||||
class: "icon icon-wiki-page" %>
|
class: "icon icon-wiki-page" %>
|
||||||
</td>
|
</td>
|
||||||
|
|
||||||
|
<% if false %>
|
||||||
<td id="notes-<%= m.id %>" class="notes unwrappable" colspan="<%= @quantities.length + 1 %>"
|
<td id="notes-<%= m.id %>" class="notes unwrappable" colspan="<%= @quantities.length + 1 %>"
|
||||||
style="text-align: left; border-left: none;">
|
style="text-align: left; border-left: none;">
|
||||||
<%= notes(m) %>
|
<%= notes(m) %>
|
||||||
</td>
|
</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>
|
<td class="action unwrappable" style="width: 1%;"><%= meal_links(m) %></td>
|
||||||
</tr>
|
</tr>
|
||||||
|
|
||||||
@ -52,4 +61,3 @@
|
|||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
<% end %>
|
<% end %>
|
||||||
</tr>
|
|
||||||
|
Reference in New Issue
Block a user