Added Targets index
This commit is contained in:
@@ -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}
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user