diff --git a/app/controllers/foods_controller.rb b/app/controllers/foods_controller.rb index 1185ef9..c6e3cde 100644 --- a/app/controllers/foods_controller.rb +++ b/app/controllers/foods_controller.rb @@ -49,7 +49,7 @@ class FoodsController < ApplicationController def destroy if @food.destroy - flash[:notice] = 'Deleted food' + flash.now[:notice] = 'Deleted food' end end diff --git a/app/controllers/meals_controller.rb b/app/controllers/meals_controller.rb index 5a9003f..0681168 100644 --- a/app/controllers/meals_controller.rb +++ b/app/controllers/meals_controller.rb @@ -93,6 +93,11 @@ class MealsController < ApplicationController end 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) @ingredients = @project.meal_ingredients.compute_quantities(@quantities) do |q, items| Hash.new { |h,k| k.composition } if q == Meal @@ -100,7 +105,7 @@ class MealsController < ApplicationController @amount_mfu_unit = @ingredients .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)) } @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] } .max_by(&:last).try(&:first) - max_value = @ingredients.max_by { |i, qv| qv[q].try(&:first) || 0 }.last[q].try(&:first) - max_value ||= BigDecimal(0) + max_value = @ingredients.map { |i, qv| qv[q].try(&:first) || BigDecimal(0) }.max @ingredient_summary[:precision][q] = [3 - max_value.exponent, 0].max end @@ -121,8 +125,5 @@ class MealsController < ApplicationController @ingredient_summary[meal.display_date][q] += a 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 diff --git a/app/models/food.rb b/app/models/food.rb index 696857e..50d41f0 100644 --- a/app/models/food.rb +++ b/app/models/food.rb @@ -16,12 +16,11 @@ class Food < ActiveRecord::Base belongs_to :ref_unit, class_name: 'Unit', required: true belongs_to :source, required: false has_many :ingredients, dependent: :restrict_with_error - has_many :nutrients, foreign_key: 'registry_id', inverse_of: :food, dependent: :destroy, - validate: true + has_many :nutrients, as: :registry, inverse_of: :food, dependent: :destroy, validate: true DOMAIN = :diet alias_attribute :subitems, :nutrients - scope :subitems, -> { includes(nutrients: [:quantity, :unit]) } + scope :with_subitems, -> { includes(nutrients: [:quantity, :unit]) } validates :nutrients, presence: true accepts_nested_attributes_for :nutrients, allow_destroy: true, diff --git a/app/models/formula.rb b/app/models/formula.rb index eee0891..a0fbc3c 100644 --- a/app/models/formula.rb +++ b/app/models/formula.rb @@ -50,6 +50,8 @@ class Formula < ActiveRecord::Base def lastBefore(timepoints) self.map{ BigDecimal(2000) } + last_timepoint = timepoints.max + @quantity.quantity_values.includes(:registry) end end diff --git a/app/models/ingredient.rb b/app/models/ingredient.rb index d2c5215..81c78ea 100644 --- a/app/models/ingredient.rb +++ b/app/models/ingredient.rb @@ -6,7 +6,7 @@ class Ingredient < ActiveRecord::Base DOMAIN = :diet 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 :amount, numericality: {greater_than_or_equal_to: 0.0} diff --git a/app/models/measurement.rb b/app/models/measurement.rb index 48e87d9..ff1588c 100644 --- a/app/models/measurement.rb +++ b/app/models/measurement.rb @@ -3,12 +3,12 @@ class Measurement < ActiveRecord::Base class_name: 'MeasurementRoutine' belongs_to :source, required: false has_one :project, through: :routine - has_many :readouts, foreign_key: 'registry_id', inverse_of: :measurement, - dependent: :destroy, validate: true + has_many :readouts, as: :registry, inverse_of: :measurement, dependent: :destroy, + validate: true DOMAIN = :measurement 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, reject_if: proc { |attrs| attrs['name'].blank? } diff --git a/app/models/nutrient.rb b/app/models/nutrient.rb index c7f6e46..1d75aed 100644 --- a/app/models/nutrient.rb +++ b/app/models/nutrient.rb @@ -1,5 +1,6 @@ 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 #validates :quantity, uniqueness: {scope: :food_id} diff --git a/app/models/quantity.rb b/app/models/quantity.rb index 11840d7..ef5dcd1 100644 --- a/app/models/quantity.rb +++ b/app/models/quantity.rb @@ -9,6 +9,7 @@ class Quantity < ActiveRecord::Base belongs_to :project, required: false has_many :nutrients, 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_one :formula, inverse_of: :quantity, dependent: :destroy, validate: true diff --git a/app/models/quantity_value.rb b/app/models/quantity_value.rb index 7851f30..9dd04b1 100644 --- a/app/models/quantity_value.rb +++ b/app/models/quantity_value.rb @@ -1,4 +1,6 @@ class QuantityValue < ActiveRecord::Base + # Requirement validation for :registry left to subclasses + belongs_to :registry, polymorphic: true belongs_to :quantity, required: true belongs_to :unit, required: true diff --git a/app/models/readout.rb b/app/models/readout.rb index 52de298..d6efd38 100644 --- a/app/models/readout.rb +++ b/app/models/readout.rb @@ -1,7 +1,10 @@ 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 #validates :quantity, uniqueness: {scope: [:measurement_id, :unit_id]} validates :value, numericality: true + + delegate :completed_at, to: :measurement end diff --git a/app/views/foods/destroy.js.erb b/app/views/foods/destroy.js.erb index d1513c5..c2209b4 100644 --- a/app/views/foods/destroy.js.erb +++ b/app/views/foods/destroy.js.erb @@ -1,3 +1,5 @@ <% if @food.destroyed? %> $('tr[id=food-<%= @food.id %>]').nextUntil('tr.primary').addBack().remove(); +<% else %> + $('#content').prepend('<%= j error_messages_for @food %>'); <% end %> diff --git a/app/views/layouts/body_tracking.js.erb b/app/views/layouts/body_tracking.js.erb index 3c22679..3fd074c 100644 --- a/app/views/layouts/body_tracking.js.erb +++ b/app/views/layouts/body_tracking.js.erb @@ -1,3 +1,4 @@ $('div[id^=flash_]').remove(); +$('div[id=errorExplanation]').remove(); $('#content').prepend('<%= j render_flash_messages %>'); <%= yield %> diff --git a/lib/body_tracking/items_with_quantities.rb b/lib/body_tracking/items_with_quantities.rb index 8d52dad..9c5b4a6 100644 --- a/lib/body_tracking/items_with_quantities.rb +++ b/lib/body_tracking/items_with_quantities.rb @@ -38,7 +38,7 @@ module BodyTracking items = all 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| subitem_value = i.respond_to?(:amount) ? i.amount*s.amount/s.ref_amount : s.value subitems[s.quantity][i] = [subitem_value, s.unit]