Fixed Quantity defaults import after removing :name uniqueness
Added awesome_nested_set #each_with_path
This commit is contained in:
@@ -24,22 +24,22 @@ class BodyTrackersController < ApplicationController
|
||||
" #{'unit'.pluralize(new_units.length)}"
|
||||
|
||||
# Quantities
|
||||
available_quantities = @project.quantities.map { |q| [[q.name, q.domain], q] }.to_h
|
||||
available_quantities = Quantity.each_with_path(@project.quantities).map(&:rotate).to_h
|
||||
quantities_count = available_quantities.length
|
||||
defaults = Quantity.where(project: nil)
|
||||
Quantity.each_with_level(defaults) do |q, level|
|
||||
unless available_quantities.has_key?([q.name, q.domain])
|
||||
Quantity.each_with_path(defaults) do |q, path|
|
||||
unless available_quantities.has_key?(path)
|
||||
attrs = q.attributes.except('id', 'project_id', 'parent_id', 'lft', 'rgt',
|
||||
'created_at', 'updated_at')
|
||||
if q.parent
|
||||
attrs['parent'] = available_quantities[[q.parent.name, q.parent.domain]]
|
||||
attrs['parent'] = available_quantities[path.rpartition('::').first]
|
||||
end
|
||||
if q.formula
|
||||
attrs['formula_attributes'] = q.formula.attributes
|
||||
.except('id', 'quantity_id', 'unit_id', 'created_at', 'updated_at')
|
||||
attrs['formula_attributes']['unit_id'] = available_units[q.formula.unit.shortname]
|
||||
end
|
||||
available_quantities[[q.name, q.domain]] = @project.quantities.build(attrs)
|
||||
available_quantities[path] = @project.quantities.build(attrs)
|
||||
end
|
||||
end
|
||||
Quantity.transaction do
|
||||
|
||||
@@ -22,8 +22,8 @@ class Quantity < ActiveRecord::Base
|
||||
# TODO: :name should be validated against model names (Meal, Ingredient etc.)
|
||||
# Quantity :name uniqueness relaxed to formulas unambiguity
|
||||
validates :name, presence: true, uniqueness: {scope: [:project_id, :parent_id]}
|
||||
# Formula ambiguity vlidation delayed after save, as otherwise there seems to
|
||||
# be no other way to validate against newly changed :name
|
||||
# Formula ambiguity validation delayed after_save, as there seems to be no
|
||||
# other way to validate against newly changed Quantity :name from Formula
|
||||
after_save do
|
||||
next unless name_changed? || changes.empty?
|
||||
formulas = Formula.joins(:quantity).where(quantities: {project_id: project})
|
||||
|
||||
Reference in New Issue
Block a user