From ef3484dfdf3cb621b50ec6fefdd653e07a7f4b56 Mon Sep 17 00:00:00 2001 From: cryptogopher Date: Wed, 7 May 2025 00:24:05 +0200 Subject: [PATCH] Update Measurements#new form pathnames on actions --- app/controllers/measurements_controller.rb | 27 ++++++++++++------- app/models/quantity.rb | 6 ++++- app/views/measurements/_form.html.erb | 21 +++++++-------- .../measurements/_form_destroy_row.html.erb | 2 -- app/views/measurements/_form_frame.html.erb | 2 +- app/views/measurements/_form_repath.html.erb | 7 +++++ .../measurements/discard.turbo_stream.erb | 2 ++ app/views/measurements/index.html.erb | 17 ++++++------ app/views/measurements/new.turbo_stream.erb | 2 ++ config/locales/en.yml | 6 ++--- config/routes.rb | 6 ++++- 11 files changed, 61 insertions(+), 37 deletions(-) delete mode 100644 app/views/measurements/_form_destroy_row.html.erb create mode 100644 app/views/measurements/_form_repath.html.erb create mode 100644 app/views/measurements/discard.turbo_stream.erb diff --git a/app/controllers/measurements_controller.rb b/app/controllers/measurements_controller.rb index 22ce980..307aac8 100644 --- a/app/controllers/measurements_controller.rb +++ b/app/controllers/measurements_controller.rb @@ -1,14 +1,12 @@ class MeasurementsController < ApplicationController - before_action :find_quantity, only: [:new] + before_action :find_quantity, only: [:new, :discard] + before_action :find_prev_quantities, only: [:new, :discard] def index @quantities = current_user.quantities.ordered end def new - prev_quantity_ids = params[:readouts]&.map { |r| r[:quantity_id] } || [] - @prev_quantities = current_user.quantities.find(prev_quantity_ids) - quantities = case params[:scope] when 'children' @@ -21,12 +19,18 @@ class MeasurementsController < ApplicationController quantities -= @prev_quantities @readouts = current_user.readouts.build(quantities.map { |q| {quantity: q} }) - all_quantities = @prev_quantities + quantities - @closest_ancestor = current_user.quantities - .common_ancestors(all_quantities.map(&:parent_id)).first - all_quantities << @closest_ancestor if @closest_ancestor - @units = current_user.units.ordered + + all_quantities = @prev_quantities + quantities + @common_ancestor = current_user.quantities + .common_ancestors(all_quantities.map(&:parent_id)).first + end + + def discard + @prev_quantities -= [@quantity] + + @common_ancestor = current_user.quantities + .common_ancestors(@prev_quantities.map(&:parent_id)).first end def create @@ -40,4 +44,9 @@ class MeasurementsController < ApplicationController def find_quantity @quantity = current_user.quantities.find_by!(id: params[:id]) end + + def find_prev_quantities + prev_quantity_ids = params[:readouts]&.map { |r| r[:quantity_id] } || [] + @prev_quantities = current_user.quantities.find(prev_quantity_ids) + end end diff --git a/app/models/quantity.rb b/app/models/quantity.rb index 992b168..1af0e01 100644 --- a/app/models/quantity.rb +++ b/app/models/quantity.rb @@ -144,7 +144,7 @@ class Quantity < ApplicationRecord user.quantities.ordered(root: id, include_root: false).to_a end - # Return: record with ID `of` with its ancestors, sorted by `depth` + # Return: record with ID `of` with its ancestors, sorted by :depth scope :with_ancestors, ->(of) { selected = Arel::Table.new('selected') @@ -163,4 +163,8 @@ class Quantity < ApplicationRecord def ancestor_of?(progeny) user.quantities.with_ancestors(progeny.id).exists?(id) end + + def relative_pathname(ancestor) + pathname.delete_prefix(ancestor&.pathname || '') + end end diff --git a/app/views/measurements/_form.html.erb b/app/views/measurements/_form.html.erb index 3852eab..8012d79 100644 --- a/app/views/measurements/_form.html.erb +++ b/app/views/measurements/_form.html.erb @@ -1,14 +1,12 @@ <% @readouts.each do |readout| %> <%= tabular_fields_for 'readouts[]', readout do |form| %> - <% row = dom_id(form.object.quantity, :new, :readout) %> - <%- tag.tr id: row, onkeydown: 'processKey(event)' do %> + <%- tag.tr id: dom_id(readout.quantity, :new, :readout), + onkeydown: 'processKey(event)' do %> + <%= tag.td id: dom_id(readout.quantity, nil, :pathname) do %> + <%= readout.quantity.relative_pathname(@common_ancestor) %> + <%end%> - <%= form.object.quantity.pathname.delete_prefix(@closest_ancestor.pathname) %> - <%= form.hidden_field :quantity_id %> - - - <%= form.number_field :value, required: true, autofocus: true, - size: 10 %> + <%= form.number_field :value, required: true, autofocus: true, size: 10 %> <%= form.select :unit_id, options_from_collection_for_select( @@ -17,9 +15,10 @@ - <%= image_link_to t(:delete), 'delete-outline', quantities_path, - class: 'dangerous', - onclick: render_turbo_stream('form_destroy_row', {row: row}) %> + <%= image_button_tag t(:delete), 'delete-outline', class: 'dangerous', + formaction: discard_new_measurement_path(readout.quantity), + formmethod: :get, formnovalidate: true, data: {turbo_stream: true} %> + <%= form.hidden_field :quantity_id %> <% end %> <% end %> diff --git a/app/views/measurements/_form_destroy_row.html.erb b/app/views/measurements/_form_destroy_row.html.erb deleted file mode 100644 index f567f95..0000000 --- a/app/views/measurements/_form_destroy_row.html.erb +++ /dev/null @@ -1,2 +0,0 @@ -<%= turbo_stream.remove row %> -<%= turbo_stream.update :flashes %> diff --git a/app/views/measurements/_form_frame.html.erb b/app/views/measurements/_form_frame.html.erb index f95f230..59dede4 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.pathname if @closest_ancestor&.pathname %> + <%= tag.legend id: :new_readouts_form_legend %> diff --git a/app/views/measurements/_form_repath.html.erb b/app/views/measurements/_form_repath.html.erb new file mode 100644 index 0000000..3edfd12 --- /dev/null +++ b/app/views/measurements/_form_repath.html.erb @@ -0,0 +1,7 @@ +<%= turbo_stream.update(:new_readouts_form_legend) { @common_ancestor&.pathname } %> + +<% @prev_quantities.each do |pq| %> + <%= turbo_stream.update dom_id(pq, nil, :pathname) do %> + <%= pq.relative_pathname(@common_ancestor) %> + <% end %> +<% end %> diff --git a/app/views/measurements/discard.turbo_stream.erb b/app/views/measurements/discard.turbo_stream.erb new file mode 100644 index 0000000..192a732 --- /dev/null +++ b/app/views/measurements/discard.turbo_stream.erb @@ -0,0 +1,2 @@ +<%= turbo_stream.remove dom_id(@quantity, :new, :readout) %> +<%= render partial: 'form_repath' %> diff --git a/app/views/measurements/index.html.erb b/app/views/measurements/index.html.erb index 25d2013..86c5c4f 100644 --- a/app/views/measurements/index.html.erb +++ b/app/views/measurements/index.html.erb @@ -6,15 +6,14 @@ <%= select_tag :id, options_from_collection_for_select( @quantities, :id, ->(q){ sanitize(' ' * q.depth + q.name) } ), form: :new_readouts_form %> - <% common_options = {form: :new_readouts_form, formaction: new_measurement_path, - formmethod: :get, formnovalidate: true, - data: {turbo_stream: true}} %> - <%= image_button_tag t('.new_quantity'), 'plus-outline', name: :scope, - **common_options -%> - <%= image_button_tag t('.new_children'), 'plus-multiple-outline', name: :scope, - value: :children, **common_options -%> - <%= image_button_tag t('.new_subtree'), 'plus-multiple-outline', name: :scope, - value: :subtree, **common_options -%> + <% common_options = {form: :new_readouts_form, formmethod: :get, + formnovalidate: true, data: {turbo_stream: true}} %> + <%= image_button_tag t('.new_quantity'), 'plus-outline', + formaction: new_measurement_path, **common_options -%> + <%= image_button_tag t('.new_children'), 'plus-multiple-outline', + formaction: new_measurement_path(:children), **common_options -%> + <%= image_button_tag t('.new_subtree'), 'plus-multiple-outline', + formaction: new_measurement_path(:subtree), **common_options -%> <% end %> diff --git a/app/views/measurements/new.turbo_stream.erb b/app/views/measurements/new.turbo_stream.erb index a70123e..74871be 100644 --- a/app/views/measurements/new.turbo_stream.erb +++ b/app/views/measurements/new.turbo_stream.erb @@ -2,6 +2,8 @@ <%= render partial: 'form_frame' %> <% end if @prev_quantities.empty? %> +<%= render partial: 'form_repath' %> + <%= turbo_stream.before :new_readouts_actions do %> <%= render partial: 'form' %> <% end %> diff --git a/config/locales/en.yml b/config/locales/en.yml index bee1caa..91d310f 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -65,9 +65,9 @@ en: measurements: navigation: Measurements index: - new_quantity: Add selected - new_children: Add children - new_subtree: Add subtree + new_quantity: Selected + new_children: Children + new_subtree: Subtree quantities: navigation: Quantities no_items: There are no configured quantities. You can Add some or Import from defaults. diff --git a/config/routes.rb b/config/routes.rb index 7d97b26..b40d356 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,5 +1,9 @@ Rails.application.routes.draw do - resources :measurements + resources :measurements, path_names: {new: '/new(/:scope)'}, + constraints: {scope: /children|subtree/}, defaults: {scope: nil} do + + get 'discard/:id', on: :new, action: :discard, as: :discard + end resources :quantities, except: [:show], path_names: {new: '(/:id)/new'} do member { post :reparent }