From d5e7ccacf5295f702ba7c4000869cadf4c839f91 Mon Sep 17 00:00:00 2001 From: cryptogopher Date: Sat, 11 Jan 2025 22:51:49 +0100 Subject: [PATCH] Add Quantity #edit and #update --- app/controllers/quantities_controller.rb | 12 ++++++++++++ app/models/quantity.rb | 4 ++-- app/views/quantities/edit.turbo_stream.erb | 13 +++++++++++++ app/views/quantities/update.turbo_stream.erb | 4 ++++ config/locales/en.yml | 2 ++ 5 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 app/views/quantities/edit.turbo_stream.erb create mode 100644 app/views/quantities/update.turbo_stream.erb diff --git a/app/controllers/quantities_controller.rb b/app/controllers/quantities_controller.rb index 0f5cf13..fd7f667 100644 --- a/app/controllers/quantities_controller.rb +++ b/app/controllers/quantities_controller.rb @@ -26,6 +26,18 @@ class QuantitiesController < ApplicationController end end + def edit + end + + def update + if @quantity.update(quantity_params.except(:parent_id)) + @ancestors = @quantity.ancestors(include_self: true) + flash.now[:notice] = t('.success', quantity: @quantity) + else + render :edit + end + end + def destroy @quantity.destroy! @ancestors = @quantity.ancestors diff --git a/app/models/quantity.rb b/app/models/quantity.rb index 32cc0d0..62e59c4 100644 --- a/app/models/quantity.rb +++ b/app/models/quantity.rb @@ -84,7 +84,7 @@ class Quantity < ApplicationRecord end end - def ancestors + def ancestors(include_self: false) quantities = Quantity.arel_table ancestors = Arel::Table.new('ancestors') @@ -93,7 +93,7 @@ class Quantity < ApplicationRecord # amount needed to set biggest negative depth to 0. Quantity.with_recursive(ancestors: [ user.quantities.select(quantities[Arel.star], Arel::Nodes.build_quoted(0).as('depth')) - .where(id: parent_id), + .where(id: include_self ? id : parent_id), user.quantities.select(quantities[Arel.star], ancestors[:depth] - 1) .joins(quantities.create_join( ancestors, quantities.create_on(quantities[:id].eq(ancestors[:parent_id])) diff --git a/app/views/quantities/edit.turbo_stream.erb b/app/views/quantities/edit.turbo_stream.erb new file mode 100644 index 0000000..3d07525 --- /dev/null +++ b/app/views/quantities/edit.turbo_stream.erb @@ -0,0 +1,13 @@ +<% ids = {row: dom_id(@quantity, :edit), + hidden_row: dom_id(@quantity), + link: nil, + form_tag: dom_id(@quantity, :edit, :form)} %> + +<%= turbo_stream.append :quantity_form do %> + <%- tabular_form_with model: @quantity, html: {id: ids[:form_tag]} do %> + <% end %> +<% end %> + +<%= turbo_stream.hide ids[:hidden_row] %> +<%= turbo_stream.remove ids[:row] %> +<%= turbo_stream.after @quantity, partial: 'form', locals: ids -%> diff --git a/app/views/quantities/update.turbo_stream.erb b/app/views/quantities/update.turbo_stream.erb new file mode 100644 index 0000000..ea880c8 --- /dev/null +++ b/app/views/quantities/update.turbo_stream.erb @@ -0,0 +1,4 @@ +<%= turbo_stream.close_form dom_id(@quantity, :edit) %> +<% @ancestors.map do |ancestor| %> + <%= turbo_stream.replace ancestor %> +<% end %> diff --git a/config/locales/en.yml b/config/locales/en.yml index 7527933..da77ce8 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -63,6 +63,8 @@ en: new_quantity: Add quantity create: success: Created new quantity "%{quantity}" + update: + success: Updated quantity "%{quantity}" destroy: success: Deleted quantity "%{quantity}" units: