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
|
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
|
def index
|
||||||
@quantities = current_user.quantities.ordered
|
@quantities = current_user.quantities.ordered
|
||||||
end
|
end
|
||||||
|
|
||||||
def new
|
def new
|
||||||
prev_quantity_ids = params[:readouts]&.map { |r| r[:quantity_id] } || []
|
|
||||||
@prev_quantities = current_user.quantities.find(prev_quantity_ids)
|
|
||||||
|
|
||||||
quantities =
|
quantities =
|
||||||
case params[:scope]
|
case params[:scope]
|
||||||
when 'children'
|
when 'children'
|
||||||
@ -21,12 +19,18 @@ class MeasurementsController < ApplicationController
|
|||||||
quantities -= @prev_quantities
|
quantities -= @prev_quantities
|
||||||
@readouts = current_user.readouts.build(quantities.map { |q| {quantity: q} })
|
@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
|
@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
|
end
|
||||||
|
|
||||||
def create
|
def create
|
||||||
@ -40,4 +44,9 @@ class MeasurementsController < ApplicationController
|
|||||||
def find_quantity
|
def find_quantity
|
||||||
@quantity = current_user.quantities.find_by!(id: params[:id])
|
@quantity = current_user.quantities.find_by!(id: params[:id])
|
||||||
end
|
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
|
end
|
||||||
|
@ -144,7 +144,7 @@ class Quantity < ApplicationRecord
|
|||||||
user.quantities.ordered(root: id, include_root: false).to_a
|
user.quantities.ordered(root: id, include_root: false).to_a
|
||||||
end
|
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) {
|
scope :with_ancestors, ->(of) {
|
||||||
selected = Arel::Table.new('selected')
|
selected = Arel::Table.new('selected')
|
||||||
|
|
||||||
@ -163,4 +163,8 @@ class Quantity < ApplicationRecord
|
|||||||
def ancestor_of?(progeny)
|
def ancestor_of?(progeny)
|
||||||
user.quantities.with_ancestors(progeny.id).exists?(id)
|
user.quantities.with_ancestors(progeny.id).exists?(id)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def relative_pathname(ancestor)
|
||||||
|
pathname.delete_prefix(ancestor&.pathname || '')
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
@ -1,14 +1,12 @@
|
|||||||
<% @readouts.each do |readout| %>
|
<% @readouts.each do |readout| %>
|
||||||
<%= tabular_fields_for 'readouts[]', readout do |form| %>
|
<%= tabular_fields_for 'readouts[]', readout do |form| %>
|
||||||
<% row = dom_id(form.object.quantity, :new, :readout) %>
|
<%- tag.tr id: dom_id(readout.quantity, :new, :readout),
|
||||||
<%- tag.tr id: row, onkeydown: 'processKey(event)' do %>
|
onkeydown: 'processKey(event)' do %>
|
||||||
|
<%= tag.td id: dom_id(readout.quantity, nil, :pathname) do %>
|
||||||
|
<%= readout.quantity.relative_pathname(@common_ancestor) %>
|
||||||
|
<%end%>
|
||||||
<td>
|
<td>
|
||||||
<%= form.object.quantity.pathname.delete_prefix(@closest_ancestor.pathname) %>
|
<%= form.number_field :value, required: true, autofocus: true, size: 10 %>
|
||||||
<%= form.hidden_field :quantity_id %>
|
|
||||||
</td>
|
|
||||||
<td>
|
|
||||||
<%= form.number_field :value, required: true, autofocus: true,
|
|
||||||
size: 10 %>
|
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
<%= form.select :unit_id, options_from_collection_for_select(
|
<%= form.select :unit_id, options_from_collection_for_select(
|
||||||
@ -17,9 +15,10 @@
|
|||||||
</td>
|
</td>
|
||||||
|
|
||||||
<td class="actions">
|
<td class="actions">
|
||||||
<%= image_link_to t(:delete), 'delete-outline', quantities_path,
|
<%= image_button_tag t(:delete), 'delete-outline', class: 'dangerous',
|
||||||
class: 'dangerous',
|
formaction: discard_new_measurement_path(readout.quantity),
|
||||||
onclick: render_turbo_stream('form_destroy_row', {row: row}) %>
|
formmethod: :get, formnovalidate: true, data: {turbo_stream: true} %>
|
||||||
|
<%= form.hidden_field :quantity_id %>
|
||||||
</td>
|
</td>
|
||||||
<% end %>
|
<% end %>
|
||||||
<% 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 %>
|
<%= tabular_form_with url: new_measurement_path, html: {id: :new_readouts_form} do %>
|
||||||
<% if @readouts&.present? %>
|
<% if @readouts&.present? %>
|
||||||
<fieldset>
|
<fieldset>
|
||||||
<%= tag.legend @closest_ancestor.pathname if @closest_ancestor&.pathname %>
|
<%= tag.legend id: :new_readouts_form_legend %>
|
||||||
<table class="items centered">
|
<table class="items centered">
|
||||||
<tbody id="readouts">
|
<tbody id="readouts">
|
||||||
<tr id="new_readouts_actions">
|
<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(
|
<%= select_tag :id, options_from_collection_for_select(
|
||||||
@quantities, :id, ->(q){ sanitize(' ' * q.depth + q.name) }
|
@quantities, :id, ->(q){ sanitize(' ' * q.depth + q.name) }
|
||||||
), form: :new_readouts_form %>
|
), form: :new_readouts_form %>
|
||||||
<% common_options = {form: :new_readouts_form, formaction: new_measurement_path,
|
<% common_options = {form: :new_readouts_form, formmethod: :get,
|
||||||
formmethod: :get, formnovalidate: true,
|
formnovalidate: true, data: {turbo_stream: true}} %>
|
||||||
data: {turbo_stream: true}} %>
|
<%= image_button_tag t('.new_quantity'), 'plus-outline',
|
||||||
<%= image_button_tag t('.new_quantity'), 'plus-outline', name: :scope,
|
formaction: new_measurement_path, **common_options -%>
|
||||||
**common_options -%>
|
<%= image_button_tag t('.new_children'), 'plus-multiple-outline',
|
||||||
<%= image_button_tag t('.new_children'), 'plus-multiple-outline', name: :scope,
|
formaction: new_measurement_path(:children), **common_options -%>
|
||||||
value: :children, **common_options -%>
|
<%= image_button_tag t('.new_subtree'), 'plus-multiple-outline',
|
||||||
<%= image_button_tag t('.new_subtree'), 'plus-multiple-outline', name: :scope,
|
formaction: new_measurement_path(:subtree), **common_options -%>
|
||||||
value: :subtree, **common_options -%>
|
|
||||||
</div>
|
</div>
|
||||||
<% end %>
|
<% end %>
|
||||||
</div>
|
</div>
|
||||||
|
@ -2,6 +2,8 @@
|
|||||||
<%= render partial: 'form_frame' %>
|
<%= render partial: 'form_frame' %>
|
||||||
<% end if @prev_quantities.empty? %>
|
<% end if @prev_quantities.empty? %>
|
||||||
|
|
||||||
|
<%= render partial: 'form_repath' %>
|
||||||
|
|
||||||
<%= turbo_stream.before :new_readouts_actions do %>
|
<%= turbo_stream.before :new_readouts_actions do %>
|
||||||
<%= render partial: 'form' %>
|
<%= render partial: 'form' %>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
@ -65,9 +65,9 @@ en:
|
|||||||
measurements:
|
measurements:
|
||||||
navigation: Measurements
|
navigation: Measurements
|
||||||
index:
|
index:
|
||||||
new_quantity: Add selected
|
new_quantity: Selected
|
||||||
new_children: Add children
|
new_children: Children
|
||||||
new_subtree: Add subtree
|
new_subtree: Subtree
|
||||||
quantities:
|
quantities:
|
||||||
navigation: Quantities
|
navigation: Quantities
|
||||||
no_items: There are no configured quantities. You can Add some or Import from defaults.
|
no_items: There are no configured quantities. You can Add some or Import from defaults.
|
||||||
|
@ -1,5 +1,9 @@
|
|||||||
Rails.application.routes.draw do
|
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
|
resources :quantities, except: [:show], path_names: {new: '(/:id)/new'} do
|
||||||
member { post :reparent }
|
member { post :reparent }
|
||||||
|
Loading…
x
Reference in New Issue
Block a user