Displaying computed nutrients
This commit is contained in:
parent
eff672318b
commit
5c8b7e0def
@ -201,68 +201,25 @@ class IngredientsController < ApplicationController
|
|||||||
|
|
||||||
def prepare_nutrients
|
def prepare_nutrients
|
||||||
@quantities = @project.quantities.where(primary: true)
|
@quantities = @project.quantities.where(primary: true)
|
||||||
ingredients, nutrients = compute_nutrients(filter_ingredients, @quantities)
|
nutrients = filter_ingredients.compute_nutrients(@quantities)
|
||||||
|
|
||||||
@nutrients = {}
|
@nutrients = {}
|
||||||
@extra_nutrients = {}
|
@extra_nutrients = {}
|
||||||
ingredients.each do |i|
|
nutrients.each do |i, requested_n, extra_n|
|
||||||
@nutrients[i] = []
|
@nutrients[i] = []
|
||||||
@extra_nutrients[i] = {}
|
requested_n.each do |q_name, value|
|
||||||
@quantities.each do |q|
|
amount, unitname = value
|
||||||
@nutrients[i] << [q.name, "%d [%s]" % nutrients[i][q.name]]
|
@nutrients[i] << [q_name, "#{amount || '-'} [#{unitname || '-'}]"]
|
||||||
#@extra_nutrients[i][n.quantity.name] = "#{n.amount} [#{n.unit.shortname}]"
|
end
|
||||||
|
|
||||||
|
@extra_nutrients[i] = []
|
||||||
|
extra_n.each do |q_name, value|
|
||||||
|
amount, unitname = value
|
||||||
|
@extra_nutrients[i] << [q_name, "#{amount || '-'} [#{unitname || '-'}]"]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def compute_nutrients(ingredients, requested_q)
|
|
||||||
unchecked_q = requested_q.map { |q| [q, nil] }
|
|
||||||
|
|
||||||
nutrients = Hash.new { |h,k| h[k] = {} }
|
|
||||||
Nutrient.where(ingredient: ingredients).includes(:quantity, :unit)
|
|
||||||
.pluck('quantities.name', :ingredient_id, :amount, 'units.shortname')
|
|
||||||
.each { |q_name, i_id, a, u_id| nutrients[q_name][i_id] = [a, u_id] }
|
|
||||||
|
|
||||||
completed_q = {}
|
|
||||||
# FIXME: loop should finish unless there is circular dependency in formulas
|
|
||||||
# for now we don't guard against that
|
|
||||||
while !unchecked_q.empty?
|
|
||||||
q, deps = unchecked_q.shift
|
|
||||||
|
|
||||||
if q.formula.blank? || (nutrients[q.name].length == ingredients.length)
|
|
||||||
completed_q[q.name] = nutrients.delete(q.name)
|
|
||||||
next
|
|
||||||
end
|
|
||||||
|
|
||||||
if deps.nil? || !deps.empty?
|
|
||||||
deps ||= q.formula_quantities
|
|
||||||
deps.reject! { |q| completed_q.has_key?(q.name) }
|
|
||||||
deps.each { |q| unchecked_q << [q, nil] unless unchecked_q.index { |u| u[0] == q } }
|
|
||||||
end
|
|
||||||
|
|
||||||
if deps.empty?
|
|
||||||
input_q = q.formula_quantities
|
|
||||||
ingredients.each do |i|
|
|
||||||
next if !nutrients[q.name][i.id].nil?
|
|
||||||
inputs = input_q.map do |i_q|
|
|
||||||
default_data = [nil, nil]
|
|
||||||
nutrient_data = (completed_q[i_q.name] || nutrients[i_q.name])[i.id]
|
|
||||||
[i_q.name, (nutrient_data || default_data)[0]]
|
|
||||||
end
|
|
||||||
nutrients[q.name][i.id] = q.calculate(inputs)
|
|
||||||
end
|
|
||||||
unchecked_q.unshift([q, deps])
|
|
||||||
else
|
|
||||||
unchecked_q << [q, deps]
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
all_q = nutrients.merge!(completed_q)
|
|
||||||
results = Hash.new { |h,k| h[k] = {} }
|
|
||||||
requested_q.each { |q| ingredients.each { |i| results[i][q.name] = all_q[q.name][i.id] } }
|
|
||||||
[ingredients, results]
|
|
||||||
end
|
|
||||||
|
|
||||||
def filter_ingredients
|
def filter_ingredients
|
||||||
filters = session[:filters] || {}
|
filters = session[:filters] || {}
|
||||||
ingredients = @project.ingredients
|
ingredients = @project.ingredients
|
||||||
|
@ -35,4 +35,62 @@ class Ingredient < ActiveRecord::Base
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.compute_nutrients(requested_q)
|
||||||
|
ingredients = all
|
||||||
|
unchecked_q = requested_q.map { |q| [q, nil] }
|
||||||
|
|
||||||
|
nutrients = Hash.new { |h,k| h[k] = {} }
|
||||||
|
Nutrient.where(ingredient: ingredients).includes(:quantity, :unit)
|
||||||
|
.order('quantities.lft')
|
||||||
|
.pluck('quantities.name', :ingredient_id, :amount, 'units.shortname')
|
||||||
|
.each { |q_name, i_id, a, u_id| nutrients[q_name][i_id] = [a, u_id] }
|
||||||
|
|
||||||
|
extra_q = nutrients.keys - requested_q.pluck(:name)
|
||||||
|
|
||||||
|
completed_q = {}
|
||||||
|
# FIXME: loop should finish unless there is circular dependency in formulas
|
||||||
|
# for now we don't guard against that
|
||||||
|
while !unchecked_q.empty?
|
||||||
|
q, deps = unchecked_q.shift
|
||||||
|
|
||||||
|
if q.formula.blank? || (nutrients[q.name].length == ingredients.count)
|
||||||
|
completed_q[q.name] = nutrients.delete(q.name)
|
||||||
|
next
|
||||||
|
end
|
||||||
|
|
||||||
|
if deps.nil? || !deps.empty?
|
||||||
|
deps ||= q.formula_quantities
|
||||||
|
deps.reject! { |q| completed_q.has_key?(q.name) }
|
||||||
|
deps.each { |q| unchecked_q << [q, nil] unless unchecked_q.index { |u| u[0] == q } }
|
||||||
|
end
|
||||||
|
|
||||||
|
if deps.empty?
|
||||||
|
input_q = q.formula_quantities
|
||||||
|
ingredients.each do |i|
|
||||||
|
next if !nutrients[q.name][i.id].nil?
|
||||||
|
inputs = input_q.map do |i_q|
|
||||||
|
default_input = [nil, nil]
|
||||||
|
nutrient_data = (completed_q[i_q.name] || nutrients[i_q.name])[i.id]
|
||||||
|
[i_q.name, (nutrient_data || [nil, nil])[0]]
|
||||||
|
end
|
||||||
|
nutrients[q.name][i.id] = q.calculate(inputs)
|
||||||
|
end
|
||||||
|
unchecked_q.unshift([q, deps])
|
||||||
|
else
|
||||||
|
unchecked_q << [q, deps]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
all_q = nutrients.merge(completed_q)
|
||||||
|
results = []
|
||||||
|
ingredients.each do |i|
|
||||||
|
results << [i,
|
||||||
|
requested_q.map { |q| [q.name, all_q[q.name][i.id]] },
|
||||||
|
extra_q.map do |q_name|
|
||||||
|
[q_name, all_q[q_name][i.id]] if all_q[q_name][i.id]
|
||||||
|
end.compact
|
||||||
|
]
|
||||||
|
end
|
||||||
|
results
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
@ -63,7 +63,7 @@ class Quantity < ActiveRecord::Base
|
|||||||
end
|
end
|
||||||
|
|
||||||
def formula_quantities
|
def formula_quantities
|
||||||
q_names = Ripper.lex(formula).each do |*, ttype, token|
|
q_names = Ripper.lex(formula).map do |*, ttype, token|
|
||||||
token if QUANTITY_TTYPES.include?(ttype)
|
token if QUANTITY_TTYPES.include?(ttype)
|
||||||
end.compact
|
end.compact
|
||||||
self.project.quantities.where(name: q_names).to_a
|
self.project.quantities.where(name: q_names).to_a
|
||||||
|
@ -18,10 +18,8 @@
|
|||||||
<td class="name" style="cursor: pointer;" onclick="$(this).closest('tr').toggle(); $(this).closest('tr').nextUntil('tr.primary', 'tr').toggle(); return false;">
|
<td class="name" style="cursor: pointer;" onclick="$(this).closest('tr').toggle(); $(this).closest('tr').nextUntil('tr.primary', 'tr').toggle(); return false;">
|
||||||
<span class="icon icon-bullet-closed"><%= i.name %></span>
|
<span class="icon icon-bullet-closed"><%= i.name %></span>
|
||||||
</td>
|
</td>
|
||||||
<% @quantities.each do |q| %>
|
<% values.each do |*, value| %>
|
||||||
<td class="primary value">
|
<td class="primary value"><%= value %></td>
|
||||||
<%= values[q.id] || '-' %>
|
|
||||||
</td>
|
|
||||||
<% end %>
|
<% end %>
|
||||||
</tr>
|
</tr>
|
||||||
|
|
||||||
@ -29,43 +27,35 @@
|
|||||||
<td class="name" style="cursor: pointer;" onclick="$(this).closest('tr').prev('tr.primary').toggle(); $(this).closest('tr').prev('tr.primary').nextUntil('tr.primary', 'tr').toggle(); return false;">
|
<td class="name" style="cursor: pointer;" onclick="$(this).closest('tr').prev('tr.primary').toggle(); $(this).closest('tr').prev('tr.primary').nextUntil('tr.primary', 'tr').toggle(); return false;">
|
||||||
<span class="icon icon-bullet-closed"><%= i.name %></span>
|
<span class="icon icon-bullet-closed"><%= i.name %></span>
|
||||||
</td>
|
</td>
|
||||||
<% @quantities.each do |q| %>
|
<% values.each do |q_name, *| %>
|
||||||
<td class="primary quantity">
|
<td class="primary quantity"><%= q_name %></td>
|
||||||
<%= q.name %>
|
|
||||||
</td>
|
|
||||||
<% end %>
|
<% end %>
|
||||||
</tr>
|
</tr>
|
||||||
<tr class="<%= row_class %>" style="display:none">
|
<tr class="<%= row_class %>" style="display:none">
|
||||||
<td class="space"></td>
|
<td class="space"></td>
|
||||||
<% @quantities.each do |q| %>
|
<% values.each do |*, value| %>
|
||||||
<td class="primary value">
|
<td class="primary value"><%= value %></td>
|
||||||
<%= values[q.id] || '-' %>
|
|
||||||
</td>
|
|
||||||
<% end %>
|
<% end %>
|
||||||
</tr>
|
</tr>
|
||||||
|
|
||||||
<% extras = @extra_nutrients[i].keys %>
|
<% extras = @extra_nutrients[i] %>
|
||||||
<% extras.each_slice(@quantities.length).with_index do |names, index| %>
|
<% extras.each_slice(@quantities.length).with_index do |values, index| %>
|
||||||
<tr class="extra <%= row_class %>" style="display:none">
|
<tr class="extra <%= row_class %>" style="display:none">
|
||||||
<td class="space"></td>
|
<td class="space"></td>
|
||||||
<% names.each do |name| %>
|
<% values.each do |q_name, *| %>
|
||||||
<td class="extra quantity">
|
<td class="extra quantity"><%= q_name %></td>
|
||||||
<%= name %>
|
|
||||||
</td>
|
|
||||||
<% end %>
|
<% end %>
|
||||||
<% if @quantities.length > names.length %>
|
<% if @quantities.length > values.length %>
|
||||||
<td class="space" colspan="<%= @quantities.length-names.length %>"></td>
|
<td class="space" colspan="<%= @quantities.length-values.length %>"></td>
|
||||||
<% end %>
|
<% end %>
|
||||||
</tr>
|
</tr>
|
||||||
<tr class="extra <%= row_class %>" style="display:none">
|
<tr class="extra <%= row_class %>" style="display:none">
|
||||||
<td class="space"></td>
|
<td class="space"></td>
|
||||||
<% names.each do |name| %>
|
<% values.each do |*, value| %>
|
||||||
<td class="extra value">
|
<td class="extra value"><%= value %></td>
|
||||||
<%= @extra_nutrients[i][name] %>
|
|
||||||
</td>
|
|
||||||
<% end %>
|
<% end %>
|
||||||
<% if @quantities.length > names.length %>
|
<% if @quantities.length > values.length %>
|
||||||
<td class="space" colspan="<%= @quantities.length-names.length %>"></td>
|
<td class="space" colspan="<%= @quantities.length-values.length %>"></td>
|
||||||
<% end %>
|
<% end %>
|
||||||
</tr>
|
</tr>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
@ -18,9 +18,9 @@
|
|||||||
|
|
||||||
<table class="list" style="border:none; width:100%">
|
<table class="list" style="border:none; width:100%">
|
||||||
<tr>
|
<tr>
|
||||||
<% total_width = 3 + @primary_quantities.length %>
|
<% total_width = 3 + @quantities.length %>
|
||||||
<td style="visibility: hidden; border: none; width:<%= 3 * 100/total_width%>%"></td>
|
<td style="visibility: hidden; border: none; width:<%= 3 * 100/total_width%>%"></td>
|
||||||
<% @primary_quantities.each do |q| %>
|
<% @quantities.each do |q| %>
|
||||||
<td class="action" style="width:<%= 100/total_width %>%">
|
<td class="action" style="width:<%= 100/total_width %>%">
|
||||||
<%= link_to l(:button_hide),
|
<%= link_to l(:button_hide),
|
||||||
toggle_nutrient_column_project_ingredients_path(@project, id: q.id),
|
toggle_nutrient_column_project_ingredients_path(@project, id: q.id),
|
||||||
|
Reference in New Issue
Block a user