forked from fixin.me/fixin.me
Partial refresh of Measurements#new form
This commit is contained in:
parent
c48bf290fd
commit
8401424efa
@ -6,9 +6,8 @@ class MeasurementsController < ApplicationController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def new
|
def new
|
||||||
readouts_params = params.permit(user: [readouts_attributes: Readout::ATTRIBUTES])
|
prev_quantity_ids = params[:readouts]&.map { |r| r[:quantity_id] } || []
|
||||||
build_attrs = readouts_params.dig(:user, :readouts_attributes)&.values
|
@prev_quantities = current_user.quantities.find(prev_quantity_ids)
|
||||||
prev_readouts = build_attrs ? Array(current_user.readouts.build(build_attrs)) : []
|
|
||||||
|
|
||||||
quantities =
|
quantities =
|
||||||
case params[:scope]
|
case params[:scope]
|
||||||
@ -19,12 +18,15 @@ class MeasurementsController < ApplicationController
|
|||||||
else
|
else
|
||||||
[@quantity]
|
[@quantity]
|
||||||
end
|
end
|
||||||
quantities -= prev_readouts.map(&:quantity)
|
quantities -= @prev_quantities
|
||||||
new_readouts = current_user.readouts.build(quantities.map { |q| {quantity: q} })
|
@readouts = current_user.readouts.build(quantities.map { |q| {quantity: q} })
|
||||||
@readouts = prev_readouts + new_readouts
|
|
||||||
|
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
|
@units = current_user.units.ordered
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -3,8 +3,8 @@ class Quantity < ApplicationRecord
|
|||||||
|
|
||||||
belongs_to :user, optional: true
|
belongs_to :user, optional: true
|
||||||
belongs_to :parent, optional: true, class_name: "Quantity"
|
belongs_to :parent, optional: true, class_name: "Quantity"
|
||||||
has_many :subquantities, class_name: "Quantity", inverse_of: :parent,
|
has_many :subquantities, ->{ order(:name) }, class_name: "Quantity",
|
||||||
dependent: :restrict_with_error
|
inverse_of: :parent, dependent: :restrict_with_error
|
||||||
|
|
||||||
validate if: ->{ parent.present? } do
|
validate if: ->{ parent.present? } do
|
||||||
errors.add(:parent, :user_mismatch) unless user_id == parent.user_id
|
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)
|
user.quantities.with_ancestors(progeny.id).exists?(id)
|
||||||
end
|
end
|
||||||
|
|
||||||
def fullname
|
def self.full_names!(of)
|
||||||
self['fullname'] ||
|
self['fullname'] ||
|
||||||
user.quantities.with_ancestors(id).order(:depth).map(&:name).join(' → ')
|
user.quantities.with_ancestors(id).order(:depth).map(&:name).join(' → ')
|
||||||
end
|
end
|
||||||
|
scope :with_full_name, ->{
|
||||||
|
}
|
||||||
end
|
end
|
||||||
|
@ -1,45 +1,26 @@
|
|||||||
<%= tabular_form_with model: current_user, html: {id: :new_readouts_form} do |user| %>
|
<% @readouts.each do |readout| %>
|
||||||
<% if @readouts&.present? %>
|
<%= tabular_fields_for 'readouts[]', readout do |form| %>
|
||||||
<fieldset>
|
<% row = dom_id(form.object.quantity, :new, :readout) %>
|
||||||
<%= tag.legend @ancestor_fullname unless @ancestor_fullname.empty? %>
|
<%- tag.tr id: row, onkeydown: 'processKey(event)' do %>
|
||||||
<table class="items centered">
|
<td>
|
||||||
<tbody id="readouts">
|
<%= form.object.quantity.full_name.delete_prefix(@closest_ancestor.full_name) %>
|
||||||
<%= user.fields_for :readouts, @readouts do |form| %>
|
<%= form.hidden_field :quantity_id %>
|
||||||
<% row = dom_id(form.object.quantity, :new, :readout) %>
|
</td>
|
||||||
<%- tag.tr id: row, onkeydown: 'processKey(event)' do %>
|
<td>
|
||||||
<td>
|
<%= form.number_field :value, required: true, autofocus: true,
|
||||||
<%= form.object.quantity.fullname.delete_prefix(@ancestor_fullname) %>
|
size: 10 %>
|
||||||
<%= form.hidden_field :quantity_id %>
|
</td>
|
||||||
</td>
|
<td>
|
||||||
<td>
|
<%= form.select :unit_id, options_from_collection_for_select(
|
||||||
<%= form.number_field :value, required: true, autofocus: true,
|
@units, :id, ->(u){ sanitize(' '*(u.base_id ? 1 : 0) + u.symbol) }
|
||||||
size: 10 %>
|
) %>
|
||||||
</td>
|
</td>
|
||||||
<td>
|
|
||||||
<%= form.select :unit_id, options_from_collection_for_select(
|
|
||||||
@units, :id, ->(u){ sanitize(' '*(u.base_id ? 1 : 0) + u.symbol) }
|
|
||||||
) %>
|
|
||||||
</td>
|
|
||||||
|
|
||||||
<td class="actions">
|
<td class="actions">
|
||||||
<%= image_link_to t(:delete), 'delete-outline', quantities_path,
|
<%= image_link_to t(:delete), 'delete-outline', quantities_path,
|
||||||
class: 'dangerous',
|
class: 'dangerous',
|
||||||
onclick: render_turbo_stream('form_destroy_row', {row: row}) %>
|
onclick: render_turbo_stream('form_destroy_row', {row: row}) %>
|
||||||
</td>
|
</td>
|
||||||
<% end %>
|
<% 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 %>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
21
app/views/measurements/_form_frame.html.erb
Normal file
21
app/views/measurements/_form_frame.html.erb
Normal 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 %>
|
@ -1,6 +1,6 @@
|
|||||||
<div class="topside vflex">
|
<div class="topside vflex">
|
||||||
<% if current_user.at_least(:active) %>
|
<% if current_user.at_least(:active) %>
|
||||||
<%= render partial: 'form' %>
|
<%= render partial: 'form_frame' %>
|
||||||
<%# TODO: show hint when no quantities/units defined %>
|
<%# TODO: show hint when no quantities/units defined %>
|
||||||
<div class="hflex">
|
<div class="hflex">
|
||||||
<%= select_tag :id, options_from_collection_for_select(
|
<%= select_tag :id, options_from_collection_for_select(
|
||||||
|
@ -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' %>
|
<%= render partial: 'form' %>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user