1
0

Fixed Quantity defaults import after removing :name uniqueness

Added awesome_nested_set #each_with_path
This commit is contained in:
cryptogopher 2020-06-20 00:52:37 +02:00
parent 61498018e7
commit 4df88598f8
6 changed files with 35 additions and 7 deletions

View File

@ -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

View File

@ -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})

View File

@ -186,6 +186,7 @@ class LoadDefaults < ActiveRecord::Migration
dir.down do
Source.where(project: nil).delete_all
Formula.joins(:quantity).where(quantities: {project_id: nil}).delete_all
Quantity.where(project: nil).delete_all
Unit.where(project: nil).delete_all
end

View File

@ -1,5 +1,6 @@
(Rails::VERSION::MAJOR < 5 ? ActionDispatch : ActiveSupport)::Reloader.to_prepare do
Project.include BodyTracking::ProjectPatch
CollectiveIdea.include BodyTracking::AwesomeNestedSetPatch
end
Redmine::Plugin.register :body_tracking do

View File

@ -0,0 +1,25 @@
module BodyTracking::AwesomeNestedSetPatch
CollectiveIdea::Acts::NestedSet.class_eval do
module CollectiveIdea::Acts::NestedSet
class Iterator
def each_with_path
return to_enum(__method__) { objects.length } unless block_given?
path = [nil]
objects.each do |o|
path[path.rindex(o.parent)+1..-1] = o
yield [o, path.map { |q| q.try(:name) }.join('::')]
end
end
end
module Model
module ClassMethods
def each_with_path(objects, &block)
Iterator.new(objects).each_with_path(&block)
end
end
end
end
end
end

View File

@ -1,6 +1,7 @@
module BodyTracking::ProjectPatch
Project.class_eval do
has_many :sources, dependent: :destroy
# TODO: includes(:parent) ?
has_many :quantities, -> { order "lft" }, inverse_of: :project, dependent: :destroy
has_many :formulas, through: :quantities
has_many :units, dependent: :destroy