1
0
This repository has been archived on 2023-12-07. You can view files and clone it, but cannot push or open issues or pull requests.
body_tracking/app/models/quantity.rb
2019-11-15 00:31:41 +01:00

38 lines
881 B
Ruby

class Quantity < ActiveRecord::Base
include BodyTracking::Formula
enum domain: {
measurement: 1,
exercise: 2,
diet: 0
}
acts_as_nested_set dependent: :destroy, scope: :project
belongs_to :project, required: false
validates :name, presence: true, uniqueness: {scope: :project_id}
validates :domain, inclusion: {in: domains.keys}
validate if: -> { parent.present? } do
errors.add(:parent, :parent_domain_mismatch) unless domain == parent.domain
end
validates :formula, formula: {allow_nil: true}
after_initialize do
if new_record?
self.primary = false if self.primary.nil?
end
end
def toggle_primary!
self.toggle!(:primary)
end
def formula_quantities
Formula.new(self.project, self.formula).get_quantities
end
def calculate(inputs)
Formula.new(self.project, self.formula).calculate(inputs)
end
end