From fd184df9c773caed74b6679b78fd801517005320 Mon Sep 17 00:00:00 2001 From: cryptogopher Date: Mon, 11 May 2020 19:36:30 +0200 Subject: [PATCH] Merged Nutrient and Readout into QuantityValue Aliased date attributes of Meal and Measurement --- app/models/meal.rb | 1 + app/models/measurement.rb | 1 + app/models/nutrient.rb | 9 ++- app/models/quantity_value.rb | 13 +++++ app/models/readout.rb | 14 +---- db/migrate/001_create_schema.rb | 97 +++++++++++++++------------------ 6 files changed, 67 insertions(+), 68 deletions(-) create mode 100644 app/models/quantity_value.rb diff --git a/app/models/meal.rb b/app/models/meal.rb index 2d7fbe3..7378f67 100644 --- a/app/models/meal.rb +++ b/app/models/meal.rb @@ -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 diff --git a/app/models/measurement.rb b/app/models/measurement.rb index 7eb89ea..87685fa 100644 --- a/app/models/measurement.rb +++ b/app/models/measurement.rb @@ -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? diff --git a/app/models/nutrient.rb b/app/models/nutrient.rb index 45b9fc4..f896bd4 100644 --- a/app/models/nutrient.rb +++ b/app/models/nutrient.rb @@ -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 diff --git a/app/models/quantity_value.rb b/app/models/quantity_value.rb new file mode 100644 index 0000000..7851f30 --- /dev/null +++ b/app/models/quantity_value.rb @@ -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 diff --git a/app/models/readout.rb b/app/models/readout.rb index b3a7993..ca72b6d 100644 --- a/app/models/readout.rb +++ b/app/models/readout.rb @@ -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 diff --git a/db/migrate/001_create_schema.rb b/db/migrate/001_create_schema.rb index df697c7..39e4570 100644 --- a/db/migrate/001_create_schema.rb +++ b/db/migrate/001_create_schema.rb @@ -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