1
0

Added MealsController#new and form autocomplete for Food

Renamed QuantityColumn -> Exposure
This commit is contained in:
cryptogopher
2020-04-15 23:42:58 +02:00
parent 8e8160c41a
commit e78803e474
27 changed files with 227 additions and 26 deletions

4
app/models/exposure.rb Normal file
View File

@@ -0,0 +1,4 @@
class Exposure < ActiveRecord::Base
belongs_to :view, polymorphic: true
belongs_to :quantity
end

View File

@@ -34,6 +34,9 @@ class Food < ActiveRecord::Base
validates :ref_amount, numericality: {greater_than: 0}
validates :group, inclusion: {in: groups.keys}
scope :visible, -> { where(hidden: false) }
scope :hidden, -> { where(hidden: true) }
after_initialize do
if new_record?
self.ref_amount ||= 100

8
app/models/ingredient.rb Normal file
View File

@@ -0,0 +1,8 @@
class Ingredient < ActiveRecord::Base
belongs_to :composition, inverse_of: :ingredients, required: true
belongs_to :food, required: true
belongs_to :part_of, required: false
validates :ready_ratio, numericality: {greater_than_or_equal_to: 0.0}
validates :amount, numericality: {greater_than_or_equal_to: 0.0}
end

View File

@@ -1,6 +1,19 @@
class Meal < ActiveRecord::Base
belongs_to :project, required: true
has_many :ingredients, as: :composition, dependent: :destroy
has_many :ingredients, as: :composition, dependent: :destroy, validate: true
has_many :foods, through: :ingredients
validates :ingredients, presence: true
accepts_nested_attributes_for :ingredients, allow_destroy: true, reject_if: proc { |attrs|
attrs['food_id'].blank? && attrs['amount'].blank?
}
# Ingredient food_id + part_of_id uniqueness validation. Cannot be effectively
# checked on Ingredient model level.
validate do
ingredients = self.ingredients.reject { |i| i.marked_for_destruction? }
.map { |i| [i.food_id, i.part_of_id] }
if ingredients.length != ingredients.uniq.length
errors.add(:ingredients, :duplicated_ingredient)
end
end
end

View File

@@ -3,9 +3,9 @@ class MeasurementRoutine < ActiveRecord::Base
has_many :measurements, -> { order "taken_at DESC" }, inverse_of: :routine,
foreign_key: 'routine_id', dependent: :restrict_with_error,
extend: BodyTracking::ItemsWithQuantities
has_many :readout_columns, as: :column_view, dependent: :destroy,
class_name: 'QuantityColumn', extend: BodyTracking::TogglableColumns
has_many :quantities, -> { order "lft" }, through: :readout_columns
has_many :readout_exposures, as: :view, dependent: :destroy,
class_name: 'Exposure', extend: BodyTracking::TogglableColumns
has_many :quantities, -> { order "lft" }, through: :readout_exposures
validates :name, presence: true, uniqueness: {scope: :project_id}
end

View File

@@ -9,7 +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 :columns, dependent: :destroy
has_many :exposures, dependent: :destroy
has_one :formula, inverse_of: :quantity, dependent: :destroy, validate: true
accepts_nested_attributes_for :formula, allow_destroy: true, reject_if: proc { |attrs|

View File

@@ -1,4 +0,0 @@
class QuantityColumn < ActiveRecord::Base
belongs_to :column_view, polymorphic: true
belongs_to :quantity
end