Relaxed quantity name uniqueness check
This commit is contained in:
parent
1bfe2f11fb
commit
008cfdcd26
@ -61,7 +61,7 @@ module BodyTrackersHelper
|
||||
single_columns = []
|
||||
spec[1..-1].each_with_index do |row, i|
|
||||
row.each do |q, span|
|
||||
# Current span nil and previous span == 1
|
||||
# Current span is nil and previous span == 1
|
||||
if span.nil? && (spec[i][q] == 1)
|
||||
spec[i][q] = -(spec.length - i)
|
||||
single_columns << q
|
||||
|
@ -13,11 +13,20 @@ class Quantity < ActiveRecord::Base
|
||||
has_many :exposures, dependent: :destroy
|
||||
|
||||
has_one :formula, inverse_of: :quantity, dependent: :destroy, validate: true
|
||||
accepts_nested_attributes_for :formula, allow_destroy: true, reject_if: proc { |attrs|
|
||||
attrs['code'].blank?
|
||||
}
|
||||
accepts_nested_attributes_for :formula, allow_destroy: true,
|
||||
reject_if: proc { |attrs| attrs['code'].blank? }
|
||||
|
||||
validates :name, presence: true, uniqueness: {scope: :project_id}
|
||||
validates :name, presence: true
|
||||
# Quantity :name uniqueness relaxed to formulas unambiguity
|
||||
validate if: -> { name_changed? } do
|
||||
formulas = project.formulas.where('formulas.code LIKE ?', "%#{name}%").includes(:quantity)
|
||||
# FIXME: should actually parse formulas in formulas and check for exact name match;
|
||||
# current code is just quick'n'dirty partial solution
|
||||
if formulas.exists?
|
||||
quantity_names = formulas.map { |f| "'#{f.quantity.name}'" }.join(',')
|
||||
errors.add(:name, :name_ambiguous, names: quantity_names)
|
||||
end
|
||||
end
|
||||
validates :domain, inclusion: {in: domains.keys}
|
||||
validate if: -> { parent.present? } do
|
||||
errors.add(:parent, :parent_domain_mismatch) unless domain == parent.domain
|
||||
|
@ -44,6 +44,8 @@ en:
|
||||
attributes:
|
||||
parent:
|
||||
parent_domain_mismatch: 'parent quantity has to be in the same domain'
|
||||
name:
|
||||
name_ambiguous: 'name creates ambiguity in quantity formulas for: %{names}'
|
||||
formula:
|
||||
attributes:
|
||||
code:
|
||||
|
@ -2,6 +2,7 @@ module BodyTracking::ProjectPatch
|
||||
Project.class_eval do
|
||||
has_many :sources, dependent: :destroy
|
||||
has_many :quantities, -> { order "lft" }, inverse_of: :project, dependent: :destroy
|
||||
has_many :formulas, through: :quantities
|
||||
has_many :units, dependent: :destroy
|
||||
|
||||
has_many :foods, -> { order "foods.name" }, dependent: :destroy,
|
||||
|
Reference in New Issue
Block a user