diff --git a/app/controllers/ingredients_controller.rb b/app/controllers/ingredients_controller.rb index a5068f0..ab5f9dc 100644 --- a/app/controllers/ingredients_controller.rb +++ b/app/controllers/ingredients_controller.rb @@ -196,13 +196,13 @@ class IngredientsController < ApplicationController end def prepare_ingredients - @ingredients = @project.ingredients.includes(:ref_unit, :source) + @ingredients, @formula_q = @project.ingredients.includes(:ref_unit, :source) .filter(@project, session[:filters]) end def prepare_nutrients @quantities = @project.quantities.where(primary: true) - ingredients, requested_n, extra_n = @project.ingredients + ingredients, requested_n, extra_n, @formula_q = @project.ingredients .filter(@project, session[:filters], @quantities) @nutrients = {} diff --git a/app/models/ingredient.rb b/app/models/ingredient.rb index bbe66a8..1a9d8d4 100644 --- a/app/models/ingredient.rb +++ b/app/models/ingredient.rb @@ -49,18 +49,20 @@ class Ingredient < ActiveRecord::Base end formula_q = if filters[:nutrients].present? - formula = Formula.new(project, filters[:nutrients]) - if formula.valid? - project.quantities.new(name: '__internal_q', formula: filters[:nutrients]) - end - end + project.quantities.new(name: '__internal_q', + formula: filters[:nutrients], + domain: :diet) + end + apply_formula = formula_q.present? && formula_q.valid? - if !requested_q.empty? || formula_q.present? - result = self.compute_nutrients(requested_q, formula_q) - requested_q.present? ? result : result[0] - else - ingredients - end + result = + if !requested_q.empty? || apply_formula + computed = ingredients.compute_nutrients(requested_q, apply_formula && formula_q) + requested_q.present? ? computed : [computed[0]] + else + [ingredients] + end + result.push(formula_q) end def self.compute_nutrients(requested_q, filter_q = nil) diff --git a/app/views/ingredients/_filters.html.erb b/app/views/ingredients/_filters.html.erb index 1bfea56..2189dc9 100644 --- a/app/views/ingredients/_filters.html.erb +++ b/app/views/ingredients/_filters.html.erb @@ -1,5 +1,7 @@
<%= l(:label_filter_plural) %> + <%= error_messages_for @formula_q %> + <%= form_tag url, id: 'filters_form', method: :get, remote: true do %> diff --git a/app/views/ingredients/_list.html.erb b/app/views/ingredients/_list.html.erb index 766b161..40ad3ef 100644 --- a/app/views/ingredients/_list.html.erb +++ b/app/views/ingredients/_list.html.erb @@ -1,4 +1,7 @@ <% if @ingredients.any? { |i| i.persisted? } %> + <%= render :partial => 'ingredients/filters', + :locals => {:url => filter_project_ingredients_path(@project)} %> +
diff --git a/app/views/ingredients/_list_nutrients.html.erb b/app/views/ingredients/_list_nutrients.html.erb index 6bfdfa0..9608a1a 100644 --- a/app/views/ingredients/_list_nutrients.html.erb +++ b/app/views/ingredients/_list_nutrients.html.erb @@ -1,4 +1,7 @@ <% if @nutrients.any? %> + <%= render :partial => 'ingredients/filters', + :locals => {:url => filter_nutrients_project_ingredients_path(@project)} %> + <%= render :partial => 'ingredients/options' %>
diff --git a/app/views/ingredients/index.html.erb b/app/views/ingredients/index.html.erb index c921d3a..8e6d44f 100644 --- a/app/views/ingredients/index.html.erb +++ b/app/views/ingredients/index.html.erb @@ -13,8 +13,6 @@ <%= render :partial => 'ingredients/form' %>

<%= t ".heading" %>

-<%= render :partial => 'ingredients/filters', - :locals => {:url => filter_project_ingredients_path(@project)} %>
<%= render :partial => 'ingredients/list' %>
diff --git a/app/views/ingredients/nutrients.html.erb b/app/views/ingredients/nutrients.html.erb index 4fb8de3..2096bd4 100644 --- a/app/views/ingredients/nutrients.html.erb +++ b/app/views/ingredients/nutrients.html.erb @@ -13,8 +13,6 @@ <%= render :partial => 'ingredients/form' %>

<%= t ".heading" %>

-<%= render :partial => 'ingredients/filters', - :locals => {:url => filter_nutrients_project_ingredients_path(@project)} %>
<%= render :partial => 'ingredients/list_nutrients' %>
diff --git a/lib/body_tracking/formula.rb b/lib/body_tracking/formula.rb index 8b7880c..fdace66 100644 --- a/lib/body_tracking/formula.rb +++ b/lib/body_tracking/formula.rb @@ -66,7 +66,14 @@ module BodyTracking paramed_formula = Ripper.lex(@formula).map do |*, ttype, token| QUANTITY_TTYPES.include?(ttype) ? "params['#{token}'].to_d" : token end.join - inputs.map { |i, values| [i, get_binding(values).eval(paramed_formula)] } + + inputs.map do |i, values| + begin + [i, get_binding(values).eval(paramed_formula)] + rescue + [i, nil] + end + end end private @@ -82,7 +89,7 @@ module BodyTracking end def validate_each(record, attribute, value) - Formula.new(record.project, value, options).validate.each do |message, params| + Formula.new(record.project, value).validate.each do |message, params| record.errors.add(attribute, message, params) end end