Cleanup Quantity :pathname related code

This commit is contained in:
cryptogopher 2025-03-23 13:11:14 +01:00
parent 3b30e58ff3
commit 1cddc794d2
6 changed files with 13 additions and 46 deletions

View File

@ -25,7 +25,6 @@ class MeasurementsController < ApplicationController
@closest_ancestor = current_user.quantities
.common_ancestors(all_quantities.map(&:parent_id)).first
all_quantites << @closest_ancestor if @closest_ancestor
current_user.quantities.full_names!(all_quantities)
@units = current_user.units.ordered
end

View File

@ -42,40 +42,22 @@ class Quantity < ApplicationRecord
).set(quantities[:depth] => selected[:depth]),
"#{self.class} Update All"
)
#quantities = Quantity.arel_table
#selected = Arel::Table.new('selected')
#Quantity.with_recursive(selected: [
# quantities.project(quantities[:id], quantities[:depth])
# .where(quantities[:id].eq(21).and(quantities[:user_id].eq(1))),
# quantities.project(quantities[:id], selected[:depth] + 1)
# .join(selected).on(selected[:id].eq(quantities[:parent_id]))
#]).joins(quantities.create_join(selected, quantities.create_on(quantities[:id].eq(selected[:id]))))
# .update_all(depth: selected[:depth])
#quantities = Quantity.arel_table
#selected = Arel::Table.new('selected')
#Quantity.with_recursive(selected: [
# quantities.project(quantities[:id].as('quantity_id'), quantities[:depth])
# .where(quantities[:id].eq(21).and(quantities[:user_id].eq(1))),
# quantities.project(quantities[:id], selected[:depth] + 1)
# .join(selected).on(selected[:quantity_id].eq(quantities[:parent_id]))
# ]).joins(:selected).update_all(depth: selected[:depth])
end
after_update if: -> { name_previously_changed? || parent_previously_changed? } do
quantities = Quantity.arel_table
selected = Arel::Table.new('selected')
# Add :name/:parent setters and update self :pathname there
Quantity.with_recursive(selected: [
user.quantities.select(
quantities[:id].as('quantity_id'),
quantities.cast(quantities[:name], 'CHAR(512)').as('full_name')
).where(id: id),
user.quantities.select(
quantities.project(quantities[:id].as('quantity_id'), quantities[:pathname])
.where(quantities[:id].eq(id)),
quantities.project(
quantities[:id],
selected[:full_name].concat(Arel::Nodes.build_quoted(' → '))
.concat(quantities[:name])
).join(quantities).on(selected[:quantity_id].eq(quantities[:parent_id]))
]).joins(:selected).update_all(full_name: selected[:full_name])
selected[:pathname].concat(Arel::Nodes.build_quoted(' → '))
.concat(quantities[:name])
).join(selected).on(selected[:quantity_id].eq(quantities[:parent_id]))
]).joins(:selected).update_all(pathname: selected[:pathname])
end
def parent=(value)
@ -198,20 +180,4 @@ class Quantity < ApplicationRecord
def ancestor_of?(progeny)
user.quantities.with_ancestors(progeny.id).exists?(id)
end
scope :with_full_name, ->{
selected = Arel::Table.new('selected')
model.with(selected: self).with_recursive(arel_table.name => [
selected.project(
selected[Arel.star],
selected.cast(selected[:name], 'CHAR(512)').as('full_name')
),
selected.project(
selected[Arel.star],
selected[:name].concat(Arel::Nodes.build_quoted(' → '))
.concat(arel_table[:full_name])
).join(arel_table).on(selected[:parent_id].eq(arel_table[:id]))
])
}
end

View File

@ -3,7 +3,7 @@
<% row = dom_id(form.object.quantity, :new, :readout) %>
<%- tag.tr id: row, onkeydown: 'processKey(event)' do %>
<td>
<%= form.object.quantity.full_name.delete_prefix(@closest_ancestor.full_name) %>
<%= form.object.quantity.pathname.delete_prefix(@closest_ancestor.pathname) %>
<%= form.hidden_field :quantity_id %>
</td>
<td>

View File

@ -1,7 +1,7 @@
<%= tabular_form_with url: new_measurement_path, html: {id: :new_readouts_form} do %>
<% if @readouts&.present? %>
<fieldset>
<%= tag.legend @closest_ancestor.full_name if @closest_ancestor&.full_name %>
<%= tag.legend @closest_ancestor.pathname if @closest_ancestor&.pathname %>
<table class="items centered">
<tbody id="readouts">
<tr id="new_readouts_actions">

View File

@ -10,7 +10,7 @@ class CreateQuantities < ActiveRecord::Migration[7.2]
# Caches; can be computed from other attributes
t.integer :depth, null: false, default: 0
t.string :path, null: false, limit: 512
t.string :pathname, null: false, limit: 511
end
add_index :quantities, [:user_id, :parent_id, :name], unique: true
end

View File

@ -2,10 +2,12 @@ module CoreExt::Arel::CrudCteUpdateAndDelete
def compile_update(...)
um = super
um.with = subqueries
um
end
def compile_delete(...)
dm = super
dm.with = subqueries
dm
end
end