1
0

Target form WIP

This commit is contained in:
cryptogopher
2020-07-02 18:59:55 +02:00
parent 0df2c6ec4f
commit 5b83860ed7
6 changed files with 24 additions and 20 deletions

View File

@@ -3,4 +3,8 @@ class Goal < ActiveRecord::Base
has_many :targets, inverse_of: :goal, dependent: :destroy
validates :name, presence: true, uniqueness: {scope: :project_id}
def is_binding?
self == project.goals.binding
end
end

View File

@@ -13,21 +13,20 @@ class Target < ActiveRecord::Base
errors.add(:thresholds, :count_mismatch) unless thresholds.count == arity
errors.add(:thresholds, :quantity_mismatch) if thresholds.to_a.uniq(&:quantity) != 1
end
validates :condition, inclusion: {in: CONDITIONS }
validates :scope, inclusion: {in: [:day], if: -> { thresholds.first.domain == :diet }}
validates :condition, inclusion: {in: CONDITIONS}
validates :scope, inclusion: {in: [:ingredient, :meal, :day],
if: -> { thresholds.first.quantity.domain == :diet }}
validates :effective_from, presence: {unless: :is_binding?}, absence: {if: :is_binding?}
after_initialize do
if new_record?
self.condition = CONDITIONS.first
self.effective_from = Date.current if is_binding?
end
end
delegate :is_binding?, to: :goal
def arity
BigDecimal.method(condition).arity
end
def is_binding?
goal == goal.project.goals.binding
end
end