1
0

Unevaluable formulas return nil (instead of throwing error)

Filter formula errors are shown in filter view
This commit is contained in:
cryptogopher 2019-11-09 20:36:10 +01:00
parent 70b6e97b87
commit 67b0cd9b66
8 changed files with 32 additions and 19 deletions

View File

@ -196,13 +196,13 @@ class IngredientsController < ApplicationController
end end
def prepare_ingredients def prepare_ingredients
@ingredients = @project.ingredients.includes(:ref_unit, :source) @ingredients, @formula_q = @project.ingredients.includes(:ref_unit, :source)
.filter(@project, session[:filters]) .filter(@project, session[:filters])
end end
def prepare_nutrients def prepare_nutrients
@quantities = @project.quantities.where(primary: true) @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) .filter(@project, session[:filters], @quantities)
@nutrients = {} @nutrients = {}

View File

@ -49,18 +49,20 @@ class Ingredient < ActiveRecord::Base
end end
formula_q = if filters[:nutrients].present? formula_q = if filters[:nutrients].present?
formula = Formula.new(project, filters[:nutrients]) project.quantities.new(name: '__internal_q',
if formula.valid? formula: filters[:nutrients],
project.quantities.new(name: '__internal_q', formula: filters[:nutrients]) domain: :diet)
end
end end
apply_formula = formula_q.present? && formula_q.valid?
if !requested_q.empty? || formula_q.present? result =
result = self.compute_nutrients(requested_q, formula_q) if !requested_q.empty? || apply_formula
requested_q.present? ? result : result[0] computed = ingredients.compute_nutrients(requested_q, apply_formula && formula_q)
requested_q.present? ? computed : [computed[0]]
else else
ingredients [ingredients]
end end
result.push(formula_q)
end end
def self.compute_nutrients(requested_q, filter_q = nil) def self.compute_nutrients(requested_q, filter_q = nil)

View File

@ -1,5 +1,7 @@
<fieldset id="filters" class="collapsible"> <fieldset id="filters" class="collapsible">
<legend onclick="toggleFieldset(this);"><%= l(:label_filter_plural) %></legend> <legend onclick="toggleFieldset(this);"><%= l(:label_filter_plural) %></legend>
<%= error_messages_for @formula_q %>
<%= form_tag url, id: 'filters_form', method: :get, remote: true do %> <%= form_tag url, id: 'filters_form', method: :get, remote: true do %>
<table class="filter"> <table class="filter">
<tr> <tr>

View File

@ -1,4 +1,7 @@
<% if @ingredients.any? { |i| i.persisted? } %> <% if @ingredients.any? { |i| i.persisted? } %>
<%= render :partial => 'ingredients/filters',
:locals => {:url => filter_project_ingredients_path(@project)} %>
<table class="list"> <table class="list">
<thead> <thead>
<tr> <tr>

View File

@ -1,4 +1,7 @@
<% if @nutrients.any? %> <% if @nutrients.any? %>
<%= render :partial => 'ingredients/filters',
:locals => {:url => filter_nutrients_project_ingredients_path(@project)} %>
<%= render :partial => 'ingredients/options' %> <%= render :partial => 'ingredients/options' %>
<table class="nutrients list odd-even"> <table class="nutrients list odd-even">

View File

@ -13,8 +13,6 @@
<%= render :partial => 'ingredients/form' %> <%= render :partial => 'ingredients/form' %>
<h2><%= t ".heading" %></h2> <h2><%= t ".heading" %></h2>
<%= render :partial => 'ingredients/filters',
:locals => {:url => filter_project_ingredients_path(@project)} %>
<div id='ingredients'> <div id='ingredients'>
<%= render :partial => 'ingredients/list' %> <%= render :partial => 'ingredients/list' %>
</div> </div>

View File

@ -13,8 +13,6 @@
<%= render :partial => 'ingredients/form' %> <%= render :partial => 'ingredients/form' %>
<h2><%= t ".heading" %></h2> <h2><%= t ".heading" %></h2>
<%= render :partial => 'ingredients/filters',
:locals => {:url => filter_nutrients_project_ingredients_path(@project)} %>
<div id='nutrients'> <div id='nutrients'>
<%= render :partial => 'ingredients/list_nutrients' %> <%= render :partial => 'ingredients/list_nutrients' %>
</div> </div>

View File

@ -66,7 +66,14 @@ module BodyTracking
paramed_formula = Ripper.lex(@formula).map do |*, ttype, token| paramed_formula = Ripper.lex(@formula).map do |*, ttype, token|
QUANTITY_TTYPES.include?(ttype) ? "params['#{token}'].to_d" : token QUANTITY_TTYPES.include?(ttype) ? "params['#{token}'].to_d" : token
end.join 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 end
private private
@ -82,7 +89,7 @@ module BodyTracking
end end
def validate_each(record, attribute, value) 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) record.errors.add(attribute, message, params)
end end
end end