Partial refresh of Measurements#new form

This commit is contained in:
cryptogopher 2025-02-18 18:27:47 +01:00
parent c48bf290fd
commit 8401424efa
6 changed files with 65 additions and 55 deletions

View File

@ -6,9 +6,8 @@ class MeasurementsController < ApplicationController
end
def new
readouts_params = params.permit(user: [readouts_attributes: Readout::ATTRIBUTES])
build_attrs = readouts_params.dig(:user, :readouts_attributes)&.values
prev_readouts = build_attrs ? Array(current_user.readouts.build(build_attrs)) : []
prev_quantity_ids = params[:readouts]&.map { |r| r[:quantity_id] } || []
@prev_quantities = current_user.quantities.find(prev_quantity_ids)
quantities =
case params[:scope]
@ -19,12 +18,15 @@ class MeasurementsController < ApplicationController
else
[@quantity]
end
quantities -= prev_readouts.map(&:quantity)
new_readouts = current_user.readouts.build(quantities.map { |q| {quantity: q} })
@readouts = prev_readouts + new_readouts
quantities -= @prev_quantities
@readouts = current_user.readouts.build(quantities.map { |q| {quantity: q} })
all_quantities = @prev_qantities + quantities
@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)
@ancestor_fullname = current_user.quantities
.common_ancestors(@readouts.map { |r| r.quantity.parent_id }).first&.fullname || ''
@units = current_user.units.ordered
end

View File

@ -3,8 +3,8 @@ class Quantity < ApplicationRecord
belongs_to :user, optional: true
belongs_to :parent, optional: true, class_name: "Quantity"
has_many :subquantities, class_name: "Quantity", inverse_of: :parent,
dependent: :restrict_with_error
has_many :subquantities, ->{ order(:name) }, class_name: "Quantity",
inverse_of: :parent, dependent: :restrict_with_error
validate if: ->{ parent.present? } do
errors.add(:parent, :user_mismatch) unless user_id == parent.user_id
@ -168,8 +168,10 @@ class Quantity < ApplicationRecord
user.quantities.with_ancestors(progeny.id).exists?(id)
end
def fullname
def self.full_names!(of)
self['fullname'] ||
user.quantities.with_ancestors(id).order(:depth).map(&:name).join(' → ')
end
scope :with_full_name, ->{
}
end

View File

@ -1,14 +1,9 @@
<%= tabular_form_with model: current_user, html: {id: :new_readouts_form} do |user| %>
<% if @readouts&.present? %>
<fieldset>
<%= tag.legend @ancestor_fullname unless @ancestor_fullname.empty? %>
<table class="items centered">
<tbody id="readouts">
<%= user.fields_for :readouts, @readouts do |form| %>
<% @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 %>
<td>
<%= form.object.quantity.fullname.delete_prefix(@ancestor_fullname) %>
<%= form.object.quantity.full_name.delete_prefix(@closest_ancestor.full_name) %>
<%= form.hidden_field :quantity_id %>
</td>
<td>
@ -28,18 +23,4 @@
</td>
<% end %>
<% end %>
<tr id="new_readouts_actions">
<td></td>
<td></td>
<td><div class="actions"><%= user.button %></div></td>
<td><div class="actions">
<%= image_link_to t(:cancel), "close-outline", measurements_path,
class: 'dangerous', name: :cancel,
onclick: render_turbo_stream('form_close') %>
</div></td>
</tr>
</tbody>
</table>
</fieldset>
<% end %>
<% end %>

View File

@ -0,0 +1,21 @@
<%= 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 %>
<table class="items centered">
<tbody id="readouts">
<tr id="new_readouts_actions">
<td></td>
<td></td>
<td><div class="actions"><%= button_tag %></div></td>
<td><div class="actions">
<%= image_link_to t(:cancel), "close-outline", measurements_path,
class: 'dangerous', name: :cancel,
onclick: render_turbo_stream('form_close') %>
</div></td>
</tr>
</tbody>
</table>
</fieldset>
<% end %>
<% end %>

View File

@ -1,6 +1,6 @@
<div class="topside vflex">
<% if current_user.at_least(:active) %>
<%= render partial: 'form' %>
<%= render partial: 'form_frame' %>
<%# TODO: show hint when no quantities/units defined %>
<div class="hflex">
<%= select_tag :id, options_from_collection_for_select(

View File

@ -1,3 +1,7 @@
<%= turbo_stream.replace :new_readouts_form, method: :morph do %>
<%= turbo_stream.replace :new_readouts_form do %>
<%= render partial: 'form_frame' %>
<% end if @prev_quantities.empty? %>
<%= turbo_stream.before :new_readouts_actions do %>
<%= render partial: 'form' %>
<% end %>