Quantity computing from formula looks done, savepoint
This commit is contained in:
parent
d70a17f58b
commit
eff672318b
@ -201,30 +201,27 @@ class IngredientsController < ApplicationController
|
||||
|
||||
def prepare_nutrients
|
||||
@quantities = @project.quantities.where(primary: true)
|
||||
compute_nutrients(filter_ingredients, @quantities)
|
||||
ingredients, nutrients = compute_nutrients(filter_ingredients, @quantities)
|
||||
|
||||
@primary_nutrients = {}
|
||||
@nutrients = {}
|
||||
@extra_nutrients = {}
|
||||
ingredients.each do |i|
|
||||
@primary_nutrients[i] = {}
|
||||
@nutrients[i] = []
|
||||
@extra_nutrients[i] = {}
|
||||
i.nutrients.sort_by { |n| n.quantity.lft }.each do |n|
|
||||
if @primary_quantities.include?(n.quantity)
|
||||
@primary_nutrients[i][n.quantity_id] = "#{n.amount} [#{n.unit.shortname}]"
|
||||
else
|
||||
@extra_nutrients[i][n.quantity.name] = "#{n.amount} [#{n.unit.shortname}]"
|
||||
end
|
||||
@quantities.each do |q|
|
||||
@nutrients[i] << [q.name, "%d [%s]" % nutrients[i][q.name]]
|
||||
#@extra_nutrients[i][n.quantity.name] = "#{n.amount} [#{n.unit.shortname}]"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def compute_nutrients(ingredients, requested_q)
|
||||
unchecked_q = requested_q.map { |q| [q, nil] }.to_h
|
||||
unchecked_q = requested_q.map { |q| [q, nil] }
|
||||
|
||||
nutrients = Hash.new { |h,k| h[k] = {} }
|
||||
Nutrient.joins(:ingredient).where(ingredient: ingredients)
|
||||
.pluck(:quantity_id, :ingredient_id, :amount, :unit_id)
|
||||
.each { |[q_id, i_id, a, u_id]| nutrients[q_id][i_id] = [a, u_id] }
|
||||
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
|
||||
@ -232,26 +229,38 @@ class IngredientsController < ApplicationController
|
||||
while !unchecked_q.empty?
|
||||
q, deps = unchecked_q.shift
|
||||
|
||||
if q.formula.blank? || (nutrients[q.id].length == ingredients.length)
|
||||
completed_q[q] = nutrients.delete(q.id)
|
||||
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?
|
||||
formula_deps = deps || q.formula_quantities
|
||||
unchecked_q[q] = formula_deps.delete_if { |q| completed_q.has_key?(q) }
|
||||
unchecked_q[q].each { |q| unchecked_q[q] = nil unless unchecked_q.has_key?(q) }
|
||||
deps = unchecked_q[q]
|
||||
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|
|
||||
inputs = nutrients.map { |q_name, *| [q_name, nutrients[q_name][i.id][0]] }.to_h
|
||||
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
|
||||
unchecked_q[q] = deps
|
||||
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
|
||||
|
@ -1,5 +1,6 @@
|
||||
class Quantity < ActiveRecord::Base
|
||||
require 'ripper'
|
||||
QUANTITY_TTYPES = [:on_ident, :on_tstring_content, :on_const]
|
||||
|
||||
enum domain: {
|
||||
diet: 0,
|
||||
@ -26,7 +27,7 @@ class Quantity < ActiveRecord::Base
|
||||
identifiers = []
|
||||
Ripper.lex(formula).each do |location, ttype, token|
|
||||
case
|
||||
when [:on_ident, :on_tstring_content, :on_const].include?(ttype)
|
||||
when QUANTITY_TTYPES.include?(ttype)
|
||||
identifiers << token
|
||||
when [:on_sp, :on_int, :on_rational, :on_float, :on_tstring_beg, :on_tstring_end,
|
||||
:on_lparen, :on_rparen].include?(ttype)
|
||||
@ -62,8 +63,13 @@ class Quantity < ActiveRecord::Base
|
||||
end
|
||||
|
||||
def formula_quantities
|
||||
q_names = Ripper.lex(formula).each do |*, ttype, token|
|
||||
token if QUANTITY_TTYPES.include?(ttype)
|
||||
end.compact
|
||||
self.project.quantities.where(name: q_names).to_a
|
||||
end
|
||||
|
||||
def calculate(inputs)
|
||||
[1.0, nil]
|
||||
end
|
||||
end
|
||||
|
@ -1,24 +1,24 @@
|
||||
<% if @primary_nutrients.any? %>
|
||||
<% if @nutrients.any? %>
|
||||
<%= render :partial => 'ingredients/options' %>
|
||||
|
||||
<table class="nutrients list odd-even">
|
||||
<thead>
|
||||
<tr>
|
||||
<% total_width = 3 + @primary_quantities.length %>
|
||||
<% total_width = 3 + @quantities.length %>
|
||||
<th style="width:<%= 3 * 100/total_width%>%"><%= l(:field_name) %></th>
|
||||
<% @primary_quantities.each do |q| %>
|
||||
<% @quantities.each do |q| %>
|
||||
<th style="width:<%= 100/total_width %>%"><%= q.name %></th>
|
||||
<% end %>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<% @primary_nutrients.each do |i, values| %>
|
||||
<% @nutrients.each do |i, values| %>
|
||||
<% row_class = "ingredient#{' hidden' if i.hidden} #{cycle('odd', 'even')}" %>
|
||||
<tr id="ingredient-<%= i.id %>" class="primary <%= row_class %>">
|
||||
<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>
|
||||
</td>
|
||||
<% @primary_quantities.each do |q| %>
|
||||
<% @quantities.each do |q| %>
|
||||
<td class="primary value">
|
||||
<%= values[q.id] || '-' %>
|
||||
</td>
|
||||
@ -29,7 +29,7 @@
|
||||
<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>
|
||||
</td>
|
||||
<% @primary_quantities.each do |q| %>
|
||||
<% @quantities.each do |q| %>
|
||||
<td class="primary quantity">
|
||||
<%= q.name %>
|
||||
</td>
|
||||
@ -37,7 +37,7 @@
|
||||
</tr>
|
||||
<tr class="<%= row_class %>" style="display:none">
|
||||
<td class="space"></td>
|
||||
<% @primary_quantities.each do |q| %>
|
||||
<% @quantities.each do |q| %>
|
||||
<td class="primary value">
|
||||
<%= values[q.id] || '-' %>
|
||||
</td>
|
||||
@ -45,7 +45,7 @@
|
||||
</tr>
|
||||
|
||||
<% extras = @extra_nutrients[i].keys %>
|
||||
<% extras.each_slice(@primary_quantities.length).with_index do |names, index| %>
|
||||
<% extras.each_slice(@quantities.length).with_index do |names, index| %>
|
||||
<tr class="extra <%= row_class %>" style="display:none">
|
||||
<td class="space"></td>
|
||||
<% names.each do |name| %>
|
||||
@ -53,8 +53,8 @@
|
||||
<%= name %>
|
||||
</td>
|
||||
<% end %>
|
||||
<% if @primary_quantities.length > names.length %>
|
||||
<td class="space" colspan="<%= @primary_quantities.length-names.length %>"></td>
|
||||
<% if @quantities.length > names.length %>
|
||||
<td class="space" colspan="<%= @quantities.length-names.length %>"></td>
|
||||
<% end %>
|
||||
</tr>
|
||||
<tr class="extra <%= row_class %>" style="display:none">
|
||||
@ -64,8 +64,8 @@
|
||||
<%= @extra_nutrients[i][name] %>
|
||||
</td>
|
||||
<% end %>
|
||||
<% if @primary_quantities.length > names.length %>
|
||||
<td class="space" colspan="<%= @primary_quantities.length-names.length %>"></td>
|
||||
<% if @quantities.length > names.length %>
|
||||
<td class="space" colspan="<%= @quantities.length-names.length %>"></td>
|
||||
<% end %>
|
||||
</tr>
|
||||
<% end %>
|
||||
|
Reference in New Issue
Block a user