1
0

Cleaned up nutrients index table

This commit is contained in:
cryptogopher 2020-06-02 23:12:50 +02:00
parent ba634eb516
commit 356e264c51
3 changed files with 25 additions and 7 deletions

View File

@ -216,5 +216,15 @@ class FoodsController < ApplicationController
def prepare_nutrients
@quantities = @project.nutrient_quantities.includes(:formula)
@foods, @filter_q = @project.foods.filter(session[:f_filters], @quantities)
@food_summary = Hash.new { |h,k| h[k] = Hash.new(BigDecimal(0)) }
@quantities.each do |q|
@food_summary[:mfu_unit][q] = @foods
.each_with_object(Hash.new(0)) { |(i, qv), h| h[qv[q].last] += 1 if qv[q] }
.max_by(&:last).try(&:first)
max_value = @foods.map { |i, qv| qv[q].try(&:first) || BigDecimal(0) }.max
@food_summary[:precision][q] = [4 - max_value.exponent, 0].max
end
end
end

View File

@ -9,12 +9,13 @@
<%= error_messages_for *formulas %>
<table class="nutrients list odd-even">
<% total_width = 4 + @quantities.length %>
<thead>
<tr>
<% total_width = 4 + @quantities.length %>
<th style="width:<%= 3 * 100/total_width %>%"><%= l(:field_name) %></th>
<th rowspan="2" style="width:<%= 3 * 100/total_width %>%"><%= l(:field_name) %></th>
<% @quantities.each do |q| %>
<th style="width:<%= 100/total_width %>%" class="closable ellipsible">
<th class="closable ellipsible"
style="width:<%= 100/total_width %>%; border-bottom:none;">
<div style="float:right;position:relative;">
<%= link_to '',
toggle_exposure_project_foods_path(@project, quantity_id: q.id),
@ -23,8 +24,11 @@
<%= q.name %>
</th>
<% end %>
<th style="width:<%= 100/total_width %>%"><%= l(:field_action) %></th>
<th rowspan="2" style="width:<%= 100/total_width %>%"><%= l(:field_action) %></th>
</tr>
<% @quantities.each do |q| %>
<th><%= "[#{@food_summary[:mfu_unit][q].try(&:shortname) || '-'}]" %></th>
<% end %>
</thead>
<tbody>
@ -37,7 +41,10 @@
<span class="icon icon-bullet-closed"><%= food.name %></span>
</td>
<% @quantities.each do |q| %>
<td class="primary value ellipsible"><%= format_value(nutrients[q]) %></td>
<td class="primary value ellipsible">
<%= format_value(nutrients[q], @food_summary[:precision][q],
@food_summary[:mfu_unit][q]) %>
</td>
<% end %>
<td class="action unwrappable"><%= action_links(food, :nutrients) %></td>
</tr>

View File

@ -39,8 +39,9 @@
<tr class="header">
<td class="quantityhead"><%= "[#{@amount_mfu_unit.shortname}]" %></td>
<% @quantities.each do |q| %>
<% mfu_unit = @ingredient_summary[:mfu_unit][q] %>
<td class="quantityhead"><%= "[#{mfu_unit ? mfu_unit.shortname : '-'}]" %></td>
<td class="quantityhead">
<%= "[#{@ingredient_summary[:mfu_unit][q].try(&:shortname) || '-'}]" %>
</td>
<% end %>
</tr>