1
0

Dispplaying units of computed values

Fixed formula error messages display when filter formula not given
This commit is contained in:
cryptogopher 2020-03-25 23:07:59 +01:00
parent 2efdc08931
commit e7a33c684f
7 changed files with 16 additions and 14 deletions

View File

@ -1,13 +1,13 @@
module BodyTrackersHelper module BodyTrackersHelper
def format_value(value) def format_value(value)
amount, unitname = value amount, unit = value
case case
when amount.nil? when amount.nil?
'-' '-'
when amount.nan? when amount.nan?
'?' '?'
else else
"#{amount} [#{unitname || '-'}]" "#{amount} [#{unit.shortname || '-'}]"
end end
end end

View File

@ -33,7 +33,7 @@ class Formula < ActiveRecord::Base
"length.times.map { |index| #{p[:content]} }" : p[:content] "length.times.map { |index| #{p[:content]} }" : p[:content]
args << get_binding(quantities, args, length).eval(code) args << get_binding(quantities, args, length).eval(code)
end end
args.last.map { |v| [v, nil] } args.last.map { |v| [v, self.unit] }
end end
private private

View File

@ -2,7 +2,7 @@
locals: {url: filter_project_ingredients_path(@project)} %> locals: {url: filter_project_ingredients_path(@project)} %>
<% if @ingredients.any? { |i| i.persisted? } %> <% if @ingredients.any? { |i| i.persisted? } %>
<%= error_messages_for @formula_q.formula %> <%= error_messages_for @formula_q.formula if @formula_q %>
<table class="list"> <table class="list">
<thead> <thead>

View File

@ -4,7 +4,9 @@
<% if @ingredients.any? %> <% if @ingredients.any? %>
<%= render partial: 'ingredients/options' %> <%= render partial: 'ingredients/options' %>
<%= error_messages_for @formula_q.formula, *@quantities.map { |q| q.formula } %> <% formulas = @quantities.map { |q| q.formula } %>
<% formulas.unshift(@formula_q.formula) if @formula_q %>
<%= error_messages_for *formulas %>
<table class="nutrients list odd-even"> <table class="nutrients list odd-even">
<thead> <thead>

View File

@ -2,7 +2,7 @@
locals: {url: filter_project_measurements_path(@project)} %> locals: {url: filter_project_measurements_path(@project)} %>
<% if @measurements.any? { |m| m.persisted? } %> <% if @measurements.any? { |m| m.persisted? } %>
<%= error_messages_for @formula_q.formula %> <%= error_messages_for @formula_q.formula if @formula_q %>
<table class="list"> <table class="list">
<thead> <thead>

View File

@ -4,7 +4,9 @@
<% if @measurements.any? %> <% if @measurements.any? %>
<%= render partial: 'measurements/options' %> <%= render partial: 'measurements/options' %>
<%= error_messages_for @formula_q.formula, *@quantities.map { |q| q.formula } %> <% formulas = @quantities.map { |q| q.formula } %>
<% formulas.unshift(@formula_q.formula) if @formula_q %>
<%= error_messages_for *formulas %>
<table class="readouts list odd-even"> <table class="readouts list odd-even">
<thead> <thead>

View File

@ -50,14 +50,12 @@ module BodyTracking
subitem_reflection = item_class.reflections[subitem_type] subitem_reflection = item_class.reflections[subitem_type]
subitem_class = subitem_reflection.klass subitem_class = subitem_reflection.klass
subitems_scope = subitem_class.where(subitem_reflection.options[:inverse_of] => items) subitems_scope = subitem_class.where(subitem_reflection.options[:inverse_of] => items)
item_foreign_key = subitem_reflection.foreign_key
subitems = Hash.new { |h,k| h[k] = {} } subitems = Hash.new { |h,k| h[k] = {} }
subitems_scope.includes(:quantity, :unit) subitems_scope.includes(:quantity, :unit).order('quantities.lft').each do |s|
.order('quantities.lft') item_id = s.send(subitem_reflection.foreign_key)
.pluck('quantities.id', item_foreign_key, VALUE_COLUMNS[item_class], subitem_value = s.send(VALUE_COLUMNS[item_class])
'units.shortname') subitems[s.quantity][item_id] = [subitem_value, s.unit]
.each { |q_id, item_id, a, u_id| subitems[q_id][item_id] = [a, u_id] } end
Quantity.find(subitems.keys).each { |q| subitems[q] = subitems.delete(q.id) }
extra_q = subitems.keys - requested_q extra_q = subitems.keys - requested_q