Merged Nutrient and Readout into QuantityValue
Aliased date attributes of Meal and Measurement
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user