Merged Nutrient and Readout into QuantityValue
Aliased date attributes of Meal and Measurement
This commit is contained in:
parent
abd7d02b89
commit
fd184df9c7
@ -21,6 +21,7 @@ class Meal < ActiveRecord::Base
|
||||
def eaten_at
|
||||
self[:eaten_at].getlocal if self[:eaten_at]
|
||||
end
|
||||
alias_attribute :completed_at, :eaten_at
|
||||
|
||||
def eaten_at_date
|
||||
self.eaten_at
|
||||
|
@ -25,6 +25,7 @@ class Measurement < ActiveRecord::Base
|
||||
end
|
||||
|
||||
validates :taken_at, presence: true
|
||||
alias_attribute :completed_at, :taken_at
|
||||
|
||||
after_initialize do
|
||||
if new_record?
|
||||
|
@ -1,10 +1,9 @@
|
||||
class Nutrient < ActiveRecord::Base
|
||||
class Nutrient < QuantityValue
|
||||
belongs_to :food, inverse_of: :nutrients, required: true
|
||||
belongs_to :quantity, required: true
|
||||
belongs_to :unit, required: true
|
||||
|
||||
# Uniqueness is checked exclusively on Food level (see Readout model for details)
|
||||
validates :value, numericality: {greater_than_or_equal_to: 0.0}
|
||||
# Uniqueness NOT validated here, see Value for explanation
|
||||
#validates :quantity, uniqueness: {scope: :food_id}
|
||||
|
||||
validates :amount, numericality: {greater_than_or_equal_to: 0.0}
|
||||
alias_attribute :amount, :value
|
||||
end
|
||||
|
13
app/models/quantity_value.rb
Normal file
13
app/models/quantity_value.rb
Normal file
@ -0,0 +1,13 @@
|
||||
class QuantityValue < ActiveRecord::Base
|
||||
belongs_to :quantity, required: true
|
||||
belongs_to :unit, required: true
|
||||
|
||||
# Uniqueness is checked exclusively on the other end of association level.
|
||||
# Otherwise validation may not pass when multiple Values are updated at once
|
||||
# and some quantity_id is moved from one Value to the other (without duplication).
|
||||
# For the same reason Value quantity_id uniqueness has to be checked on the
|
||||
# other end when multiple Values are first created. Relying on local check
|
||||
# only would make all newly added records pass as valid despite duplications.
|
||||
#validates :quantity, uniqueness: {scope: [:measurement_id, :unit_id]}
|
||||
#validates :quantity, uniqueness: {scope: :food_id}
|
||||
end
|
@ -1,15 +1,7 @@
|
||||
class Readout < ActiveRecord::Base
|
||||
class Readout < QuantityValue
|
||||
belongs_to :measurement, inverse_of: :readouts, required: true
|
||||
belongs_to :quantity, required: true
|
||||
belongs_to :unit, required: true
|
||||
|
||||
# Uniqueness is checked exclusively on Measurement level. Otherwise validation
|
||||
# may not pass when multiple Readouts are updated at once and some quantity_id
|
||||
# is moved from one Readout to the other (without duplication).
|
||||
# For the same reason Readout quantity_id uniqueness has to be checked by
|
||||
# Measurement when multiple Readouts are first created. Relying on this check
|
||||
# only would make all newly added records pass as valid despite duplications.
|
||||
#validates :quantity, uniqueness: {scope: [:measurement_id, :unit_id]}
|
||||
|
||||
validates :value, numericality: true
|
||||
# Uniqueness NOT validated here, see Value for explanation
|
||||
#validates :quantity, uniqueness: {scope: [:measurement_id, :unit_id]}
|
||||
end
|
||||
|
@ -1,12 +1,5 @@
|
||||
class CreateSchema < ActiveRecord::Migration
|
||||
def change
|
||||
create_table :units do |t|
|
||||
t.references :project
|
||||
t.string :name
|
||||
t.string :shortname
|
||||
t.timestamps null: false
|
||||
end
|
||||
|
||||
create_table :quantities do |t|
|
||||
t.references :project
|
||||
t.integer :domain
|
||||
@ -32,6 +25,22 @@ class CreateSchema < ActiveRecord::Migration
|
||||
t.references :quantity
|
||||
end
|
||||
|
||||
create_table :quantity_values do |t|
|
||||
t.string :type
|
||||
t.references :registry, polymorphic: true
|
||||
t.references :quantity
|
||||
t.decimal :value, precision: 12, scale: 6
|
||||
t.references :unit
|
||||
t.timestamps null: false
|
||||
end
|
||||
|
||||
create_table :units do |t|
|
||||
t.references :project
|
||||
t.string :name
|
||||
t.string :shortname
|
||||
t.timestamps null: false
|
||||
end
|
||||
|
||||
create_table :sources do |t|
|
||||
t.references :project
|
||||
t.string :name
|
||||
@ -39,51 +48,6 @@ class CreateSchema < ActiveRecord::Migration
|
||||
t.timestamps null: false
|
||||
end
|
||||
|
||||
create_table :foods do |t|
|
||||
t.references :project
|
||||
t.string :name
|
||||
t.text :notes
|
||||
t.decimal :ref_amount, precision: 12, scale: 6
|
||||
t.references :ref_unit
|
||||
t.integer :group
|
||||
t.references :source
|
||||
t.string :source_ident
|
||||
t.boolean :hidden
|
||||
t.decimal :ready_amount, precision: 12, scale: 6
|
||||
t.timestamps null: false
|
||||
end
|
||||
|
||||
create_table :nutrients do |t|
|
||||
t.references :food
|
||||
t.references :quantity
|
||||
t.decimal :amount, precision: 12, scale: 6
|
||||
t.references :unit
|
||||
t.timestamps null: false
|
||||
end
|
||||
|
||||
create_table :measurement_routines do |t|
|
||||
t.references :project
|
||||
t.string :name
|
||||
t.text :description
|
||||
t.timestamps null: false
|
||||
end
|
||||
|
||||
create_table :measurements do |t|
|
||||
t.references :routine
|
||||
t.references :source
|
||||
t.text :notes
|
||||
t.timestamp :taken_at
|
||||
t.timestamps null: false
|
||||
end
|
||||
|
||||
create_table :readouts do |t|
|
||||
t.references :measurement
|
||||
t.references :quantity
|
||||
t.decimal :value, precision: 12, scale: 6
|
||||
t.references :unit
|
||||
t.timestamps null: false
|
||||
end
|
||||
|
||||
create_table :meals do |t|
|
||||
t.references :project
|
||||
t.text :notes
|
||||
@ -99,5 +63,34 @@ class CreateSchema < ActiveRecord::Migration
|
||||
t.decimal :ready_ratio, precision: 12, scale: 6
|
||||
t.timestamps null: false
|
||||
end
|
||||
|
||||
create_table :foods do |t|
|
||||
t.references :project
|
||||
t.string :name
|
||||
t.text :notes
|
||||
t.decimal :ref_amount, precision: 12, scale: 6
|
||||
t.references :ref_unit
|
||||
t.integer :group
|
||||
t.references :source
|
||||
t.string :source_ident
|
||||
t.boolean :hidden
|
||||
t.decimal :ready_amount, precision: 12, scale: 6
|
||||
t.timestamps null: false
|
||||
end
|
||||
|
||||
create_table :measurement_routines do |t|
|
||||
t.references :project
|
||||
t.string :name
|
||||
t.text :description
|
||||
t.timestamps null: false
|
||||
end
|
||||
|
||||
create_table :measurements do |t|
|
||||
t.references :routine
|
||||
t.references :source
|
||||
t.text :notes
|
||||
t.timestamp :taken_at
|
||||
t.timestamps null: false
|
||||
end
|
||||
end
|
||||
end
|
||||
|
Reference in New Issue
Block a user