1
0

Finished CSV import

Fixed problem with excessive validation
This commit is contained in:
cryptogopher
2019-09-22 20:26:07 +02:00
parent eb43c4be21
commit 3ebc2f58d0
6 changed files with 33 additions and 31 deletions

View File

@@ -4,26 +4,24 @@ class Ingredient < ActiveRecord::Base
meat: 1
}
belongs_to :project
belongs_to :ref_unit, class_name: 'Unit'
belongs_to :project, required: true
belongs_to :ref_unit, class_name: 'Unit', required: true
has_many :nutrients, inverse_of: :ingredient, dependent: :destroy
has_many :nutrients, inverse_of: :ingredient, dependent: :destroy, validate: true
validates :nutrients, presence: true
accepts_nested_attributes_for :nutrients, allow_destroy: true, reject_if: proc { |attrs|
attrs['quantity_id'].blank? && attrs['amount'].blank?
}
validates_associated :nutrients
# Nutrient quantity_id uniqueness check for nested attributes
validate on: :create do
validate do
quantities = self.nutrients.map { |n| n.quantity_id }
if quantities.length != quantities.uniq.length
errors.add(:nutrients, :duplicated_quantity)
end
end
validates :project, associated: true
validates :name, presence: true, uniqueness: {scope: :project_id}
validates :ref_amount, numericality: {greater_than: 0}
validates :ref_unit, presence: true, associated: true
validates :group, inclusion: {in: groups.keys}
after_initialize do
@@ -32,6 +30,7 @@ class Ingredient < ActiveRecord::Base
units = self.project.units
self.ref_unit ||= units.find_by(shortname: 'g') || units.first
self.group ||= :other
self.hidden = false if self.hidden.nil?
end
end

View File

@@ -1,13 +1,10 @@
class Nutrient < ActiveRecord::Base
belongs_to :ingredient, inverse_of: :nutrients
belongs_to :quantity
belongs_to :unit
belongs_to :ingredient, inverse_of: :nutrients, required: true
belongs_to :quantity, required: true
belongs_to :unit, required: true
# disabled to avoid loop with Ingredient 'validates_associated :nutrients'
#validates :ingredient, presence: true, associated: true
validates :quantity, presence: true, associated: true, uniqueness: {scope: :ingredient_id}
validates :amount, numericality: {greater_than: 0}
validates :unit, presence: true, associated: true
validates :quantity, uniqueness: {scope: :ingredient_id}
validates :amount, numericality: {greater_thani_or_equal_to: 0.0}
after_initialize do
if new_record?

View File

@@ -1,18 +1,15 @@
class Quantity < ActiveRecord::Base
acts_as_nested_set dependent: :nullify, scope: :project
enum domain: {
diet: 0,
measurement: 1,
exercise: 2
}
belongs_to :project
acts_as_nested_set dependent: :nullify, scope: :project
belongs_to :project, required: false
validates :project, associated: true
validates :name, presence: true, uniqueness: {scope: :project_id}
validates :domain, inclusion: {in: domains.keys}
validates :parent, associated: true
validate if: -> { parent.present? } do
errors.add(:parent, :parent_domain_mismatch) unless domain == parent.domain
end

View File

@@ -1,6 +1,5 @@
class Unit < ActiveRecord::Base
belongs_to :project
belongs_to :project, required: true
validates :project, associated: true
validates :shortname, presence: true, uniqueness: {scope: :project_id}
end