Fix migration of default quantities
Allow Quantity formula to be nil but not blank Add computed energy quantity Reverse default quantities deletion order
This commit is contained in:
parent
0e5a53b4b9
commit
3ce5d5c940
@ -59,6 +59,7 @@ class QuantitiesController < ApplicationController
|
|||||||
private
|
private
|
||||||
|
|
||||||
def quantity_params
|
def quantity_params
|
||||||
|
params[:quantity].delete(:formula) if params[:quantity][:formula].blank?
|
||||||
params.require(:quantity).permit(
|
params.require(:quantity).permit(
|
||||||
:domain,
|
:domain,
|
||||||
:parent_id,
|
:parent_id,
|
||||||
|
@ -15,7 +15,7 @@ class Quantity < ActiveRecord::Base
|
|||||||
validate if: -> { parent.present? } do
|
validate if: -> { parent.present? } do
|
||||||
errors.add(:parent, :parent_domain_mismatch) unless domain == parent.domain
|
errors.add(:parent, :parent_domain_mismatch) unless domain == parent.domain
|
||||||
end
|
end
|
||||||
validates :formula, formula: true
|
validates :formula, formula: {allow_nil: true}
|
||||||
|
|
||||||
after_initialize do
|
after_initialize do
|
||||||
if new_record?
|
if new_record?
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
class LoadDefaults < ActiveRecord::Migration
|
class LoadDefaults < ActiveRecord::Migration
|
||||||
|
def change
|
||||||
reversible do |dir|
|
reversible do |dir|
|
||||||
dir.up do
|
dir.up do
|
||||||
Unit.create project: nil, shortname: "g", name: "gram"
|
Unit.create project: nil, shortname: "g", name: "gram"
|
||||||
@ -13,7 +14,7 @@ class LoadDefaults < ActiveRecord::Migration
|
|||||||
e = Quantity.create project: nil, domain: :diet, parent: nil, name: "Energy",
|
e = Quantity.create project: nil, domain: :diet, parent: nil, name: "Energy",
|
||||||
description: "Total energy", primary: true
|
description: "Total energy", primary: true
|
||||||
|
|
||||||
Quantity.create project: nil, domain: :diet, parent: nil, name: "Proteins",
|
p = Quantity.create project: nil, domain: :diet, parent: nil, name: "Proteins",
|
||||||
description: "Total amount of proteins", primary: true
|
description: "Total amount of proteins", primary: true
|
||||||
|
|
||||||
f = Quantity.create project: nil, domain: :diet, parent: nil, name: "Fats",
|
f = Quantity.create project: nil, domain: :diet, parent: nil, name: "Fats",
|
||||||
@ -121,13 +122,20 @@ class LoadDefaults < ActiveRecord::Migration
|
|||||||
v21 = Quantity.create project: nil, domain: :diet, parent: v1, name: "Vitamin K",
|
v21 = Quantity.create project: nil, domain: :diet, parent: v1, name: "Vitamin K",
|
||||||
description: ""
|
description: ""
|
||||||
|
|
||||||
|
# Calculated quantities go at the and to make sure dependencies exist
|
||||||
|
e1 = Quantity.create project: nil, domain: :diet, parent: e, name: "Calculated",
|
||||||
|
description: "Total energy calculated from macronutrients", primary: true,
|
||||||
|
formula: "4*Proteins + 9*Fats + 4*Carbohydrates"
|
||||||
|
|
||||||
Source.create project: nil, name: "nutrition label",
|
Source.create project: nil, name: "nutrition label",
|
||||||
description: "nutrition facts taken from package nutrition label"
|
description: "nutrition facts taken from package nutrition label"
|
||||||
end
|
end
|
||||||
|
|
||||||
dir.down do
|
dir.down do
|
||||||
Unit.where(project: nil).delete_all
|
|
||||||
Quantity.where(project: nil).delete_all
|
|
||||||
Source.where(project: nil).delete_all
|
Source.where(project: nil).delete_all
|
||||||
|
Quantity.where(project: nil).delete_all
|
||||||
|
Unit.where(project: nil).delete_all
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -7,7 +7,7 @@ module BodyTracking
|
|||||||
class InvalidFormula < RuntimeError; end
|
class InvalidFormula < RuntimeError; end
|
||||||
class Formula
|
class Formula
|
||||||
def initialize(project, formula)
|
def initialize(project, formula)
|
||||||
@project = project
|
@project_quantities = Quantity.where(project: project)
|
||||||
@formula = formula
|
@formula = formula
|
||||||
@paramed_formula = nil
|
@paramed_formula = nil
|
||||||
@quantities = nil
|
@quantities = nil
|
||||||
@ -79,7 +79,7 @@ module BodyTracking
|
|||||||
|
|
||||||
# 4th: check if identifiers used in formula correspond to existing quantities
|
# 4th: check if identifiers used in formula correspond to existing quantities
|
||||||
identifiers.uniq!
|
identifiers.uniq!
|
||||||
quantities = @project.quantities.where(name: identifiers)
|
quantities = @project_quantities.where(name: identifiers)
|
||||||
quantities_names = quantities.pluck(:name)
|
quantities_names = quantities.pluck(:name)
|
||||||
(identifiers - quantities_names).each do |q|
|
(identifiers - quantities_names).each do |q|
|
||||||
errors << [:unknown_quantity, {quantity: q}]
|
errors << [:unknown_quantity, {quantity: q}]
|
||||||
|
Reference in New Issue
Block a user