1
0

Added Target and Threshold models

This commit is contained in:
cryptogopher 2020-06-21 02:48:32 +02:00
parent 2c4caed737
commit 8240e5e868
3 changed files with 21 additions and 1 deletions

14
app/models/target.rb Normal file
View File

@ -0,0 +1,14 @@
class Target < ActiveRecord::Base
belongs_to :goal, inverse_of: :targets
belongs_to :item, polymorphic: true, inverse_of: :targets
has_many :thresholds, 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? }
# TODO: validate thresholds count according to condition type
validates :condition, inclusion: {in: [:<, :<=, :>, :>=, :==]}
validates :scope, inclusion: {in: [:day], if: -> { thresholds.first.domain == :diet }}
validates :effective_from, presence: {unless: goal?}, absence: {if: goal?}
end

6
app/models/threshold.rb Normal file
View File

@ -0,0 +1,6 @@
class Threshold < QuantityValue
belongs_to :target, foreign_key: 'registry_id', foreign_type: 'foreign_type',
inverse_of: :thresholds, polymorphic: true, required: true
validates :value, numericality: true
end

View File

@ -95,7 +95,7 @@ class CreateSchema < ActiveRecord::Migration
create_table :targets do |t|
t.references :goal
t.references :threshold
t.references :item, polymorphic: true
t.string :condition
t.string :scope
t.date :effective_from