1
0

Fixed importing Foods with QuantityValue

Fixed double flash when not followed by request
Added Food#destroy error reporting
Simplified prepare_meals with no ingredients
Renamed scope on item with subitems: subitems -> with_subitems
This commit is contained in:
cryptogopher 2020-05-20 23:33:34 +02:00
parent fa9c329a81
commit a416e1ce9b
13 changed files with 29 additions and 17 deletions

View File

@ -49,7 +49,7 @@ class FoodsController < ApplicationController
def destroy def destroy
if @food.destroy if @food.destroy
flash[:notice] = 'Deleted food' flash.now[:notice] = 'Deleted food'
end end
end end

View File

@ -93,6 +93,11 @@ class MealsController < ApplicationController
end end
def prepare_meals def prepare_meals
@meals_by_date = @project.meals.reject(&:new_record?)
.sort_by { |m| m.eaten_at || m.created_at }.group_by(&:display_date)
return if @meals_by_date.empty?
@quantities = @project.meal_quantities.includes(:formula) @quantities = @project.meal_quantities.includes(:formula)
@ingredients = @project.meal_ingredients.compute_quantities(@quantities) do |q, items| @ingredients = @project.meal_ingredients.compute_quantities(@quantities) do |q, items|
Hash.new { |h,k| k.composition } if q == Meal Hash.new { |h,k| k.composition } if q == Meal
@ -100,7 +105,7 @@ class MealsController < ApplicationController
@amount_mfu_unit = @ingredients @amount_mfu_unit = @ingredients
.each_with_object(Hash.new(0)) { |(i, qv), h| h[i.food.ref_unit] += 1 } .each_with_object(Hash.new(0)) { |(i, qv), h| h[i.food.ref_unit] += 1 }
.max_by(&:last).try(&:first) .max_by(&:last).first
@ingredient_summary = Hash.new { |h,k| h[k] = Hash.new(BigDecimal(0)) } @ingredient_summary = Hash.new { |h,k| h[k] = Hash.new(BigDecimal(0)) }
@quantities.each do |q| @quantities.each do |q|
@ -108,8 +113,7 @@ class MealsController < ApplicationController
.each_with_object(Hash.new(0)) { |(i, qv), h| h[qv[q].last] += 1 if qv[q] } .each_with_object(Hash.new(0)) { |(i, qv), h| h[qv[q].last] += 1 if qv[q] }
.max_by(&:last).try(&:first) .max_by(&:last).try(&:first)
max_value = @ingredients.max_by { |i, qv| qv[q].try(&:first) || 0 }.last[q].try(&:first) max_value = @ingredients.map { |i, qv| qv[q].try(&:first) || BigDecimal(0) }.max
max_value ||= BigDecimal(0)
@ingredient_summary[:precision][q] = [3 - max_value.exponent, 0].max @ingredient_summary[:precision][q] = [3 - max_value.exponent, 0].max
end end
@ -121,8 +125,5 @@ class MealsController < ApplicationController
@ingredient_summary[meal.display_date][q] += a @ingredient_summary[meal.display_date][q] += a
end end
end end
@meals_by_date = @project.meals.reject(&:new_record?)
.sort_by { |m| m.eaten_at || m.created_at }.group_by(&:display_date)
end end
end end

View File

@ -16,12 +16,11 @@ class Food < ActiveRecord::Base
belongs_to :ref_unit, class_name: 'Unit', required: true belongs_to :ref_unit, class_name: 'Unit', required: true
belongs_to :source, required: false belongs_to :source, required: false
has_many :ingredients, dependent: :restrict_with_error has_many :ingredients, dependent: :restrict_with_error
has_many :nutrients, foreign_key: 'registry_id', inverse_of: :food, dependent: :destroy, has_many :nutrients, as: :registry, inverse_of: :food, dependent: :destroy, validate: true
validate: true
DOMAIN = :diet DOMAIN = :diet
alias_attribute :subitems, :nutrients alias_attribute :subitems, :nutrients
scope :subitems, -> { includes(nutrients: [:quantity, :unit]) } scope :with_subitems, -> { includes(nutrients: [:quantity, :unit]) }
validates :nutrients, presence: true validates :nutrients, presence: true
accepts_nested_attributes_for :nutrients, allow_destroy: true, accepts_nested_attributes_for :nutrients, allow_destroy: true,

View File

@ -50,6 +50,8 @@ class Formula < ActiveRecord::Base
def lastBefore(timepoints) def lastBefore(timepoints)
self.map{ BigDecimal(2000) } self.map{ BigDecimal(2000) }
last_timepoint = timepoints.max
@quantity.quantity_values.includes(:registry)
end end
end end

View File

@ -6,7 +6,7 @@ class Ingredient < ActiveRecord::Base
DOMAIN = :diet DOMAIN = :diet
alias_attribute :subitems, :nutrients alias_attribute :subitems, :nutrients
scope :subitems, -> { includes(nutrients: [:quantity, :unit]) } scope :with_subitems, -> { includes(nutrients: [:quantity, :unit]) }
validates :ready_ratio, numericality: {greater_than_or_equal_to: 0.0} validates :ready_ratio, numericality: {greater_than_or_equal_to: 0.0}
validates :amount, numericality: {greater_than_or_equal_to: 0.0} validates :amount, numericality: {greater_than_or_equal_to: 0.0}

View File

@ -3,12 +3,12 @@ class Measurement < ActiveRecord::Base
class_name: 'MeasurementRoutine' class_name: 'MeasurementRoutine'
belongs_to :source, required: false belongs_to :source, required: false
has_one :project, through: :routine has_one :project, through: :routine
has_many :readouts, foreign_key: 'registry_id', inverse_of: :measurement, has_many :readouts, as: :registry, inverse_of: :measurement, dependent: :destroy,
dependent: :destroy, validate: true validate: true
DOMAIN = :measurement DOMAIN = :measurement
alias_attribute :subitems, :readouts alias_attribute :subitems, :readouts
scope :subitems, -> { includes(readouts: [:quantity, :unit]) } scope :with_subitems, -> { includes(readouts: [:quantity, :unit]) }
accepts_nested_attributes_for :routine, allow_destroy: true, accepts_nested_attributes_for :routine, allow_destroy: true,
reject_if: proc { |attrs| attrs['name'].blank? } reject_if: proc { |attrs| attrs['name'].blank? }

View File

@ -1,5 +1,6 @@
class Nutrient < QuantityValue class Nutrient < QuantityValue
belongs_to :food, foreign_key: 'registry_id', inverse_of: :nutrients, required: true belongs_to :food, foreign_key: 'registry_id', foreign_type: 'registry_type',
inverse_of: :nutrients, polymorphic: true, required: true
# Uniqueness NOT validated here, see Value for explanation # Uniqueness NOT validated here, see Value for explanation
#validates :quantity, uniqueness: {scope: :food_id} #validates :quantity, uniqueness: {scope: :food_id}

View File

@ -9,6 +9,7 @@ class Quantity < ActiveRecord::Base
belongs_to :project, required: false belongs_to :project, required: false
has_many :nutrients, dependent: :restrict_with_error has_many :nutrients, dependent: :restrict_with_error
has_many :readouts, dependent: :restrict_with_error has_many :readouts, dependent: :restrict_with_error
has_many :quantity_values, dependent: :restrict_with_error
has_many :exposures, dependent: :destroy has_many :exposures, dependent: :destroy
has_one :formula, inverse_of: :quantity, dependent: :destroy, validate: true has_one :formula, inverse_of: :quantity, dependent: :destroy, validate: true

View File

@ -1,4 +1,6 @@
class QuantityValue < ActiveRecord::Base class QuantityValue < ActiveRecord::Base
# Requirement validation for :registry left to subclasses
belongs_to :registry, polymorphic: true
belongs_to :quantity, required: true belongs_to :quantity, required: true
belongs_to :unit, required: true belongs_to :unit, required: true

View File

@ -1,7 +1,10 @@
class Readout < QuantityValue class Readout < QuantityValue
belongs_to :measurement, foreign_key: 'registry_id', inverse_of: :readouts, required: true belongs_to :measurement, foreign_key: 'registry_id', inverse_of: :readouts,
polymorphic: true, required: true
# Uniqueness NOT validated here, see Value for explanation # Uniqueness NOT validated here, see Value for explanation
#validates :quantity, uniqueness: {scope: [:measurement_id, :unit_id]} #validates :quantity, uniqueness: {scope: [:measurement_id, :unit_id]}
validates :value, numericality: true validates :value, numericality: true
delegate :completed_at, to: :measurement
end end

View File

@ -1,3 +1,5 @@
<% if @food.destroyed? %> <% if @food.destroyed? %>
$('tr[id=food-<%= @food.id %>]').nextUntil('tr.primary').addBack().remove(); $('tr[id=food-<%= @food.id %>]').nextUntil('tr.primary').addBack().remove();
<% else %>
$('#content').prepend('<%= j error_messages_for @food %>');
<% end %> <% end %>

View File

@ -1,3 +1,4 @@
$('div[id^=flash_]').remove(); $('div[id^=flash_]').remove();
$('div[id=errorExplanation]').remove();
$('#content').prepend('<%= j render_flash_messages %>'); $('#content').prepend('<%= j render_flash_messages %>');
<%= yield %> <%= yield %>

View File

@ -38,7 +38,7 @@ module BodyTracking
items = all items = all
subitems = Hash.new { |h,k| h[k] = {} } subitems = Hash.new { |h,k| h[k] = {} }
all.subitems.order('quantities.lft').each do |i| all.with_subitems.order('quantities.lft').each do |i|
i.subitems.each do |s| i.subitems.each do |s|
subitem_value = i.respond_to?(:amount) ? i.amount*s.amount/s.ref_amount : s.value subitem_value = i.respond_to?(:amount) ? i.amount*s.amount/s.ref_amount : s.value
subitems[s.quantity][i] = [subitem_value, s.unit] subitems[s.quantity][i] = [subitem_value, s.unit]