diff --git a/app/controllers/meals_controller.rb b/app/controllers/meals_controller.rb index 572f76c..915f6ff 100644 --- a/app/controllers/meals_controller.rb +++ b/app/controllers/meals_controller.rb @@ -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 diff --git a/app/helpers/body_trackers_helper.rb b/app/helpers/body_trackers_helper.rb index 31f5d79..f246ae9 100644 --- a/app/helpers/body_trackers_helper.rb +++ b/app/helpers/body_trackers_helper.rb @@ -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 diff --git a/app/views/foods/_nutrients.html.erb b/app/views/foods/_nutrients.html.erb index 3f73da2..de3e160 100644 --- a/app/views/foods/_nutrients.html.erb +++ b/app/views/foods/_nutrients.html.erb @@ -12,7 +12,7 @@ <% total_width = 4 + @quantities.length %> - <%= l(:field_name) %> + <%= l(:field_name) %> <% @quantities.each do |q| %>
diff --git a/app/views/meals/_index.html.erb b/app/views/meals/_index.html.erb index 26a101f..0548736 100644 --- a/app/views/meals/_index.html.erb +++ b/app/views/meals/_index.html.erb @@ -1,13 +1,32 @@ <% if @meals_by_date.any? %> <%= render partial: 'meals/options' %> - + <% formulas = @quantities.map { |q| q.formula } %> + <% formulas.unshift(@filter_q.formula) if @filter_q %> + <%= error_messages_for *formulas %> + +
+ <% total_width = 4 + @quantities.length %> <% @meals_by_date.reverse_each do |date, meals| %> - + + <% @quantities.each do |q| %> + + <% end %> + <% meals.each_with_index do |m, index| %> diff --git a/app/views/meals/_show.html.erb b/app/views/meals/_show.html.erb index 773786e..8bcabe5 100644 --- a/app/views/meals/_show.html.erb +++ b/app/views/meals/_show.html.erb @@ -26,19 +26,25 @@ class: "icon icon-wiki-page" %> - - + <% m.ingredients.each do |i| %> - - + + <% @quantities.each do |q| %> + + <% end %>
+

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

+ <%= l(:field_amount) %> + +
+ <%= link_to '', + toggle_exposure_project_meals_path(@project, quantity_id: q.id), + {class: "icon icon-close", method: :post, remote: true} %> +
+ <%= q.name %> +
<%= notes(m) %> <%= meal_links(m) %><%= meal_links(m) %>
<%= i.food.name %> + <%= i.food.name %> <%= i.amount %>[<%= i.food.ref_unit.shortname %>] + <%= format_value(@nutrients[q][i], @nutrients[q][:precision], + @nutrients[q][:mfu_unit]) %> + <%# Moved to helper to avoid spaces between buttons %> <%= adjustment_buttons(i) %>