From 1cddc794d26c5a262cb3726d57ad12558f249b1c Mon Sep 17 00:00:00 2001 From: cryptogopher Date: Sun, 23 Mar 2025 13:11:14 +0100 Subject: [PATCH] Cleanup Quantity :pathname related code --- app/controllers/measurements_controller.rb | 1 - app/models/quantity.rb | 50 +++---------------- app/views/measurements/_form.html.erb | 2 +- app/views/measurements/_form_frame.html.erb | 2 +- .../20250104194343_create_quantities.rb | 2 +- .../arel/crud_cte_update_and_delete.rb | 2 + 6 files changed, 13 insertions(+), 46 deletions(-) diff --git a/app/controllers/measurements_controller.rb b/app/controllers/measurements_controller.rb index 661c6c2..0c3ccd6 100644 --- a/app/controllers/measurements_controller.rb +++ b/app/controllers/measurements_controller.rb @@ -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 diff --git a/app/models/quantity.rb b/app/models/quantity.rb index bf3ce4b..ed5fe4e 100644 --- a/app/models/quantity.rb +++ b/app/models/quantity.rb @@ -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 diff --git a/app/views/measurements/_form.html.erb b/app/views/measurements/_form.html.erb index bf244e7..3852eab 100644 --- a/app/views/measurements/_form.html.erb +++ b/app/views/measurements/_form.html.erb @@ -3,7 +3,7 @@ <% row = dom_id(form.object.quantity, :new, :readout) %> <%- tag.tr id: row, onkeydown: 'processKey(event)' do %> - <%= 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 %> diff --git a/app/views/measurements/_form_frame.html.erb b/app/views/measurements/_form_frame.html.erb index 7917ab3..f95f230 100644 --- a/app/views/measurements/_form_frame.html.erb +++ b/app/views/measurements/_form_frame.html.erb @@ -1,7 +1,7 @@ <%= tabular_form_with url: new_measurement_path, html: {id: :new_readouts_form} do %> <% if @readouts&.present? %>
- <%= tag.legend @closest_ancestor.full_name if @closest_ancestor&.full_name %> + <%= tag.legend @closest_ancestor.pathname if @closest_ancestor&.pathname %> diff --git a/db/migrate/20250104194343_create_quantities.rb b/db/migrate/20250104194343_create_quantities.rb index 82a21ad..16292c2 100644 --- a/db/migrate/20250104194343_create_quantities.rb +++ b/db/migrate/20250104194343_create_quantities.rb @@ -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 diff --git a/lib/core_ext/arel/crud_cte_update_and_delete.rb b/lib/core_ext/arel/crud_cte_update_and_delete.rb index 34926b3..a9183e5 100644 --- a/lib/core_ext/arel/crud_cte_update_and_delete.rb +++ b/lib/core_ext/arel/crud_cte_update_and_delete.rb @@ -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