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