forked from fixin.me/fixin.me
Update Measurements#new form pathnames on actions
This commit is contained in:
parent
9dbcfddf98
commit
ef3484dfdf
@ -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
|
||||
|
@ -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
|
||||
|
@ -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%>
|
||||
<td>
|
||||
<%= form.object.quantity.pathname.delete_prefix(@closest_ancestor.pathname) %>
|
||||
<%= form.hidden_field :quantity_id %>
|
||||
</td>
|
||||
<td>
|
||||
<%= form.number_field :value, required: true, autofocus: true,
|
||||
size: 10 %>
|
||||
<%= form.number_field :value, required: true, autofocus: true, size: 10 %>
|
||||
</td>
|
||||
<td>
|
||||
<%= form.select :unit_id, options_from_collection_for_select(
|
||||
@ -17,9 +15,10 @@
|
||||
</td>
|
||||
|
||||
<td class="actions">
|
||||
<%= 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 %>
|
||||
</td>
|
||||
<% end %>
|
||||
<% end %>
|
||||
|
@ -1,2 +0,0 @@
|
||||
<%= turbo_stream.remove row %>
|
||||
<%= turbo_stream.update :flashes %>
|
@ -1,7 +1,7 @@
|
||||
<%= tabular_form_with url: new_measurement_path, html: {id: :new_readouts_form} do %>
|
||||
<% if @readouts&.present? %>
|
||||
<fieldset>
|
||||
<%= tag.legend @closest_ancestor.pathname if @closest_ancestor&.pathname %>
|
||||
<%= tag.legend id: :new_readouts_form_legend %>
|
||||
<table class="items centered">
|
||||
<tbody id="readouts">
|
||||
<tr id="new_readouts_actions">
|
||||
|
7
app/views/measurements/_form_repath.html.erb
Normal file
7
app/views/measurements/_form_repath.html.erb
Normal file
@ -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 %>
|
2
app/views/measurements/discard.turbo_stream.erb
Normal file
2
app/views/measurements/discard.turbo_stream.erb
Normal file
@ -0,0 +1,2 @@
|
||||
<%= turbo_stream.remove dom_id(@quantity, :new, :readout) %>
|
||||
<%= render partial: 'form_repath' %>
|
@ -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 -%>
|
||||
</div>
|
||||
<% end %>
|
||||
</div>
|
||||
|
@ -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 %>
|
||||
|
@ -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.
|
||||
|
@ -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 }
|
||||
|
Loading…
x
Reference in New Issue
Block a user