1
0

Checking ambiguity of quantity paths in formulas

This commit is contained in:
cryptogopher 2020-06-06 00:18:13 +02:00
parent 356e264c51
commit 6785090045

View File

@ -79,22 +79,28 @@ class Formula < ActiveRecord::Base
identifiers, parts = parser.parse identifiers, parts = parser.parse
errors = parser.errors errors = parser.errors
project = self.quantity.project # Search through association if possible to properly validate with in-memory records,
quantities = # e.g. during import of defaults (so impossible to use recursive sql instead)
if project q_names = identifiers.map { |i| i.split('::').last }
# This is required to properly validate with in-memory records, e.g. q_paths = {}
# during import of defaults (quantity.project.try(&:quantities) || Quantity.where(project: nil))
project.quantities.select { |q| identifiers.include?(q.name) } .select { |q| q_names.include?(q.name) }.each do |q|
else
Quantity.where(project: nil, name: identifiers.to_a) # NOTE: after upgrade do Ruby 2.7 replace with Enumerator#produce
current, path = q, q.name
while current
q_paths[path] = q_paths.has_key?(path) ? nil : q
current, path = current.parent, "#{current.parent.try(:name)}::#{path}"
end end
identifiers -= quantities.map(&:name) end
quantities = []
identifiers.reject! { |i| q_paths[i] && quantities << q_paths[i] }
models = identifiers.map(&:safe_constantize).compact || [] models = identifiers.map(&:safe_constantize).compact || []
(identifiers - models.map(&:class_name)).each do |i| (identifiers - models.map(&:class_name)).each do |i|
errors << [:unknown_dependency, {identifier: i}] errors << [:unknown_dependency, {identifier: i}]
end end
@parts, @quantity_deps, @model_deps = parts, quantities.to_a, models if errors.empty? @parts, @quantity_deps, @model_deps = parts, quantities, models if errors.empty?
errors errors
end end