Add GoalController: #index, #new, #create
This commit is contained in:
@@ -6,18 +6,22 @@ class Goal < ActiveRecord::Base
|
||||
class_name: 'Exposure', extend: BodyTracking::TogglableExposures
|
||||
has_many :quantities, -> { order "lft" }, through: :target_exposures
|
||||
|
||||
accepts_nested_attributes_for :targets, allow_destroy: true,
|
||||
reject_if: proc { |attrs| attrs['quantity_id'].blank? }
|
||||
validates :target_exposures, presence: true, unless: :is_binding?
|
||||
accepts_nested_attributes_for :targets, allow_destroy: true
|
||||
validates :is_binding, uniqueness: {scope: :project_id}, if: :is_binding?
|
||||
validates :name, presence: true, uniqueness: {scope: :project_id},
|
||||
exclusion: {in: [I18n.t('targets.form.binding_goal')], unless: :is_binding?}
|
||||
exclusion: {in: [I18n.t('goals.binding.name')], unless: :is_binding?}
|
||||
|
||||
after_initialize do
|
||||
if new_record?
|
||||
self.is_binding = false if self.is_binding.nil?
|
||||
self.targets.new if self.targets.empty?
|
||||
end
|
||||
end
|
||||
|
||||
before_save do
|
||||
quantities << targets.map(&:quantity)[0..5] if target_exposures.empty?
|
||||
end
|
||||
|
||||
before_destroy prepend: true do
|
||||
!is_binding?
|
||||
end
|
||||
|
||||
@@ -5,7 +5,7 @@ class QuantityValue < ActiveRecord::Base
|
||||
# to allow for accessing registry item without knowing QuantityValue (subitem)
|
||||
# type, e.g. qv.registry.completed_at
|
||||
belongs_to :registry, polymorphic: true
|
||||
belongs_to :quantity, -> { where(domain: DOMAIN) }, required: true
|
||||
belongs_to :quantity, ->(qv) { where(domain: qv.class::DOMAIN) }, required: true
|
||||
belongs_to :unit, required: true
|
||||
|
||||
# Uniqueness is checked exclusively on the other end of association level.
|
||||
|
||||
@@ -11,17 +11,20 @@ class Target < ActiveRecord::Base
|
||||
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
|
||||
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 :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?
|
||||
if self.thresholds.empty?
|
||||
self.thresholds.new(quantity: self.goal.project.quantities.target.first)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user