forked from fixin.me/fixin.me
Add Quantity #edit and #update
This commit is contained in:
parent
adcc6699ce
commit
d5e7ccacf5
@ -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
|
||||
|
@ -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]))
|
||||
|
13
app/views/quantities/edit.turbo_stream.erb
Normal file
13
app/views/quantities/edit.turbo_stream.erb
Normal file
@ -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 -%>
|
4
app/views/quantities/update.turbo_stream.erb
Normal file
4
app/views/quantities/update.turbo_stream.erb
Normal file
@ -0,0 +1,4 @@
|
||||
<%= turbo_stream.close_form dom_id(@quantity, :edit) %>
|
||||
<% @ancestors.map do |ancestor| %>
|
||||
<%= turbo_stream.replace ancestor %>
|
||||
<% end %>
|
@ -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:
|
||||
|
Loading…
x
Reference in New Issue
Block a user