1
0

Displaying quantities with fixed precision/MFU unit removed

This commit is contained in:
cryptogopher 2020-04-28 16:05:42 +02:00
parent dfa20a55d1
commit 3418af87c1
5 changed files with 44 additions and 13 deletions

View File

@ -86,13 +86,18 @@ class MealsController < ApplicationController
ingredients = @project.meal_ingredients
@nutrients = {}
ingredients.each do |i|
@nutrients[i] = foods[i.food].map do |q, v|
[q, v && [v[0]*i.amount/i.food.ref_amount, v[1]]]
@quantities.each do |q|
@nutrients[q] = ingredients.map do |i|
n_amount, n_unit = foods[i.food][q]
[i, [n_amount && 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].values.map(&:last)
.each_with_object(Hash.new(0)) { |u, h| h[u] += 1 }.max_by(&:last).first
@nutrients[q][:precision] = [3 - max_value.exponent, 0].max
end
@meals_by_date = ingredients.group_by { |i| i.composition }.reject { |m,*| m.new_record? }
@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 }
end

View File

@ -1,5 +1,5 @@
module BodyTrackersHelper
def format_value(value)
def format_value(value, precision=2, mfu_unit=nil)
amount, unit = value
case
when amount.nil?
@ -7,7 +7,8 @@ module BodyTrackersHelper
when amount.nan?
'?'
else
"#{amount} [#{unit.shortname || '-'}]"
unit_desc = unit != mfu_unit ? unit && " [#{unit.shortname}]" || ' [-]' : ''
"%.#{precision}f%s" % [amount, unit_desc]
end
end

View File

@ -12,7 +12,7 @@
<thead>
<tr>
<% total_width = 4 + @quantities.length %>
<th style="width:<%= 3 * 100/total_width%>%"><%= l(:field_name) %></th>
<th style="width:<%= 3 * 100/total_width %>%"><%= l(:field_name) %></th>
<% @quantities.each do |q| %>
<th style="width:<%= 100/total_width %>%" class="closable ellipsible">
<div style="float:right;position:relative;">

View File

@ -1,13 +1,32 @@
<% if @meals_by_date.any? %>
<%= render partial: 'meals/options' %>
<table id="meals" class="odd-even list">
<% formulas = @quantities.map { |q| q.formula } %>
<% formulas.unshift(@filter_q.formula) if @filter_q %>
<%= error_messages_for *formulas %>
<table id="meals" class="list odd-even">
<tbody>
<% total_width = 4 + @quantities.length %>
<% @meals_by_date.reverse_each do |date, meals| %>
<tr id="date-<%= date.strftime('%Y%m%d') %>" class="date">
<td class="date" colspan="4">
<td class="date" colspan="2" style="width:<%= 3 * 100/total_width %>%">
<h3><%= date == Date.current ? 'Today' : date.strftime('%F') %></h3>
</td>
<td style="width:<%= 100/total_width %>%; text-align: left;">
<b><%= l(:field_amount) %></b>
</td>
<% @quantities.each do |q| %>
<td style="width:<%= 100/total_width %>%; text-align: left;" class="closable">
<div style="float: right; position: relative;">
<%= link_to '',
toggle_exposure_project_meals_path(@project, quantity_id: q.id),
{class: "icon icon-close", method: :post, remote: true} %>
</div>
<b><%= q.name %></b>
</td>
<% end %>
<td style="width:<%= 100/total_width %>%"></td>
</tr>
<% meals.each_with_index do |m, index| %>

View File

@ -26,19 +26,25 @@
class: "icon icon-wiki-page" %>
</td>
<td id="notes-<%= m.id %>" class="notes unwrappable"
<td id="notes-<%= m.id %>" class="notes unwrappable" colspan="<%= @quantities.length + 1 %>"
style="text-align: left; border-left: none;">
<%= notes(m) %>
</td>
<td class="action" style="width: 1%;"><%= meal_links(m) %></td>
<td class="action unwrappable" style="width: 1%;"><%= meal_links(m) %></td>
</tr>
<% m.ingredients.each do |i| %>
<tr id="ingredient-<%= i.id %>" class="ingredient project idnt idnt-2 buttonable">
<td class="name unwrappable" colspan="2"><%= i.food.name %></td>
<td class="amount unwrappable" style="text-align: right;">
<td class="name ellipsible" colspan="2"><%= i.food.name %></td>
<td class="value ellipsible">
<span><%= i.amount %></span>[<%= i.food.ref_unit.shortname %>]
</td>
<% @quantities.each do |q| %>
<td class="value ellipsible">
<%= format_value(@nutrients[q][i], @nutrients[q][:precision],
@nutrients[q][:mfu_unit]) %>
</td>
<% end %>
<td class="unwrappable" style="width: 1%; text-align: right;">
<%# Moved to helper to avoid spaces between buttons %>
<%= adjustment_buttons(i) %>