1
0

Added Targets index

This commit is contained in:
cryptogopher
2020-08-06 21:52:33 +02:00
parent ffcc9553d5
commit cf3bbb205d
15 changed files with 245 additions and 45 deletions

View File

@@ -1,6 +1,10 @@
class Goal < ActiveRecord::Base
belongs_to :project, required: true
has_many :targets, inverse_of: :goal, dependent: :destroy
has_many :targets, -> { order "effective_from DESC" }, inverse_of: :goal,
dependent: :destroy, extend: BodyTracking::ItemsWithQuantities
has_many :target_exposures, as: :view, dependent: :destroy,
class_name: 'Exposure', extend: BodyTracking::TogglableExposures
has_many :quantities, -> { order "lft" }, through: :target_exposures
validates :name, presence: true, uniqueness: {scope: :project_id}

View File

@@ -16,7 +16,7 @@ class Target < ActiveRecord::Base
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?}
validates :effective_from, presence: {if: :is_binding?}, absence: {unless: :is_binding?}
after_initialize do
if new_record?
@@ -30,4 +30,8 @@ class Target < ActiveRecord::Base
def arity
BigDecimal.method(condition).arity
end
def to_s
"#{condition} #{thresholds.first.value} [#{thresholds.first.unit.shortname}]"
end
end