diff --git a/app/controllers/foods_controller.rb b/app/controllers/foods_controller.rb index c6e3cde..3c86e15 100644 --- a/app/controllers/foods_controller.rb +++ b/app/controllers/foods_controller.rb @@ -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 diff --git a/app/views/foods/_nutrients.html.erb b/app/views/foods/_nutrients.html.erb index de3e160..46a45f7 100644 --- a/app/views/foods/_nutrients.html.erb +++ b/app/views/foods/_nutrients.html.erb @@ -9,12 +9,13 @@ <%= error_messages_for *formulas %>
<%= l(:field_name) %> | +<%= l(:field_name) %> | <% @quantities.each do |q| %> -+ |
<%= link_to '',
toggle_exposure_project_foods_path(@project, quantity_id: q.id),
@@ -23,8 +24,11 @@
<%= q.name %>
|
<% end %>
- <%= l(:field_action) %> | +<%= l(:field_action) %> | <%= "[#{@food_summary[:mfu_unit][q].try(&:shortname) || '-'}]" %> | + <% end %> @@ -37,7 +41,10 @@ <%= food.name %> <% @quantities.each do |q| %> -<%= format_value(nutrients[q]) %> | ++ <%= format_value(nutrients[q], @food_summary[:precision][q], + @food_summary[:mfu_unit][q]) %> + | <% end %><%= action_links(food, :nutrients) %> | diff --git a/app/views/meals/_index.html.erb b/app/views/meals/_index.html.erb index deaaea3..14f2fc8 100644 --- a/app/views/meals/_index.html.erb +++ b/app/views/meals/_index.html.erb @@ -39,8 +39,9 @@
---|---|---|---|---|---|
<%= "[#{@amount_mfu_unit.shortname}]" %> | <% @quantities.each do |q| %> - <% mfu_unit = @ingredient_summary[:mfu_unit][q] %> -<%= "[#{mfu_unit ? mfu_unit.shortname : '-'}]" %> | ++ <%= "[#{@ingredient_summary[:mfu_unit][q].try(&:shortname) || '-'}]" %> + | <% end %>