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.
2021-02-07 11:02:41 +01:00

38 lines
1.5 KiB
Ruby

class Target < ActiveRecord::Base
belongs_to :goal, inverse_of: :targets, required: true
belongs_to :quantity, inverse_of: :targets, required: true
belongs_to :item, polymorphic: true, inverse_of: :targets
has_many :thresholds, -> { joins(:quantity).order(:lft) },
as: :registry, inverse_of: :target, dependent: :destroy, validate: true
validates :thresholds, presence: true
accepts_nested_attributes_for :thresholds, allow_destroy: true,
reject_if: proc { |attrs| attrs['quantity_id'].blank? && attrs['value'].blank? }
validate do
quantities = thresholds.map(&:quantity)
ancestors = quantities.max_by(:lft).self_and_ancestors
errors.add(:thresholds, :count_mismatch) unless quantities.length == ancestors.length
errors.add(:thresholds, :quantity_mismatch) unless quantities == ancestors
end
validates :scope, inclusion: {in: [:ingredient, :meal, :day], if: -> { quantity.diet? }}
validates :effective_from, presence: {if: :is_binding?}, absence: {unless: :is_binding?}
after_initialize do
if new_record?
# Target should be only instantiated through Goal, so :is_binding? will be available
self.effective_from ||= Date.current if is_binding?
end
end
delegate :is_binding?, to: :goal
# NOTE: remove if not used in controller
def arity
thresholds.size
end
def to_s
thresholds.last.quantity.description %
thresholds.map { |t| [t.quantity.name, "#{t.value} [#{t.unit.shortname}]"] }.to_h
end
end