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
cryptogopher aa26e66312 Fixed calculation for indexed formulas
Delegated Quantity formula calls
2020-02-04 00:13:49 +01:00

68 lines
1.5 KiB
Ruby

class Quantity < ActiveRecord::Base
include BodyTracking::Formula
enum domain: {
diet: 0,
measurement: 1,
exercise: 2
}
# Has to go before any 'dependent:' association
before_destroy do
# FIXME: disallow destruction if any object depends on this quantity
nil
end
acts_as_nested_set dependent: :destroy, scope: :project
belongs_to :project, required: false
has_and_belongs_to_many :column_views
has_many :readouts
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.domain ||= :diet
end
end
delegate :valid?, :quantities, :calculate, to: :f_obj, prefix: :formula, allow_nil: true
def movable?(direction)
case direction
when :up
self.left_sibling.present?
when :down
self.right_sibling.present?
when :left
self.parent.present?
when :right
left = self.left_sibling
left.present? && (left.domain == self.domain)
else
false
end
end
def self.filter(project, filters)
quantities = all
if filters[:domain].present?
quantities = quantities.where(domain: domains[filters[:domain]])
end
quantities
end
private
def f_obj
Formula.new(self.project, self.formula) if self.formula?
end
end