diff --git a/app/controllers/ingredients_controller.rb b/app/controllers/ingredients_controller.rb index 5c29476..18ca0b2 100644 --- a/app/controllers/ingredients_controller.rb +++ b/app/controllers/ingredients_controller.rb @@ -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 diff --git a/app/models/quantity.rb b/app/models/quantity.rb index acef00d..f2ad00f 100644 --- a/app/models/quantity.rb +++ b/app/models/quantity.rb @@ -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 diff --git a/app/views/ingredients/_list_nutrients.html.erb b/app/views/ingredients/_list_nutrients.html.erb index b865cf4..c9fbd5b 100644 --- a/app/views/ingredients/_list_nutrients.html.erb +++ b/app/views/ingredients/_list_nutrients.html.erb @@ -1,24 +1,24 @@ -<% if @primary_nutrients.any? %> +<% if @nutrients.any? %> <%= render :partial => 'ingredients/options' %> - <% total_width = 3 + @primary_quantities.length %> + <% total_width = 3 + @quantities.length %> - <% @primary_quantities.each do |q| %> + <% @quantities.each do |q| %> <% end %> - <% @primary_nutrients.each do |i, values| %> + <% @nutrients.each do |i, values| %> <% row_class = "ingredient#{' hidden' if i.hidden} #{cycle('odd', 'even')}" %> - <% @primary_quantities.each do |q| %> + <% @quantities.each do |q| %> @@ -29,7 +29,7 @@ - <% @primary_quantities.each do |q| %> + <% @quantities.each do |q| %> @@ -37,7 +37,7 @@ - <% @primary_quantities.each do |q| %> + <% @quantities.each do |q| %> @@ -45,7 +45,7 @@ <% 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| %> <% names.each do |name| %> @@ -53,8 +53,8 @@ <%= name %> <% end %> - <% if @primary_quantities.length > names.length %> - + <% if @quantities.length > names.length %> + <% end %> @@ -64,8 +64,8 @@ <%= @extra_nutrients[i][name] %> <% end %> - <% if @primary_quantities.length > names.length %> - + <% if @quantities.length > names.length %> + <% end %> <% end %>
<%= l(:field_name) %><%= q.name %>
<%= i.name %> <%= values[q.id] || '-' %> <%= i.name %> <%= q.name %>