From e74c7077299bb72bc35fd296fa42ac7d8ebf2a3f Mon Sep 17 00:00:00 2001 From: cryptogopher Date: Wed, 29 Apr 2020 23:12:59 +0200 Subject: [PATCH] Displaying amount column in same format as quantities --- app/controllers/meals_controller.rb | 3 +++ app/views/meals/_index.html.erb | 9 ++++++--- app/views/meals/_show.html.erb | 3 ++- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/app/controllers/meals_controller.rb b/app/controllers/meals_controller.rb index a7de62d..34e8a5c 100644 --- a/app/controllers/meals_controller.rb +++ b/app/controllers/meals_controller.rb @@ -85,6 +85,9 @@ class MealsController < ApplicationController foods = @project.meal_foods.compute_quantities(@quantities) ingredients = @project.meal_ingredients + @amount_mfu_unit = ingredients + .each_with_object(Hash.new(0)) { |i, h| h[i.food.ref_unit] += 1 }.max_by(&:last).first + @nutrients = {} @quantities.each do |q| @nutrients[q] = ingredients.map do |i| diff --git a/app/views/meals/_index.html.erb b/app/views/meals/_index.html.erb index d3c2755..77d8e40 100644 --- a/app/views/meals/_index.html.erb +++ b/app/views/meals/_index.html.erb @@ -13,8 +13,8 @@

<%= date == Date.current ? 'Today' : date.strftime('%F') %>

- - <%= l(:field_amount) %> + + <%= l(:field_amount) %> <% @quantities.each do |q| %> + + <%= "[#{@amount_mfu_unit.shortname}]" %> + <% @quantities.each do |q| %> <% mfu_unit = @nutrients[q][:mfu_unit] %> - <%= "[#{ mfu_unit ? mfu_unit.shortname : '-'}]" %> + <%= "[#{mfu_unit ? mfu_unit.shortname : '-'}]" %> <% end %> diff --git a/app/views/meals/_show.html.erb b/app/views/meals/_show.html.erb index 8bcabe5..cc0351f 100644 --- a/app/views/meals/_show.html.erb +++ b/app/views/meals/_show.html.erb @@ -37,7 +37,8 @@ <%= i.food.name %> - <%= i.amount %>[<%= i.food.ref_unit.shortname %>] + <%= "%g" % i.amount %> + <%= "[#{i.food.ref_unit.shortname}]" unless i.food.ref_unit == @amount_mfu_unit %> <% @quantities.each do |q| %>