diff --git a/app/controllers/measurements_controller.rb b/app/controllers/measurements_controller.rb
index 22ce980..307aac8 100644
--- a/app/controllers/measurements_controller.rb
+++ b/app/controllers/measurements_controller.rb
@@ -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
diff --git a/app/models/quantity.rb b/app/models/quantity.rb
index 992b168..1af0e01 100644
--- a/app/models/quantity.rb
+++ b/app/models/quantity.rb
@@ -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
diff --git a/app/views/measurements/_form.html.erb b/app/views/measurements/_form.html.erb
index 3852eab..8012d79 100644
--- a/app/views/measurements/_form.html.erb
+++ b/app/views/measurements/_form.html.erb
@@ -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%>
- <%= form.object.quantity.pathname.delete_prefix(@closest_ancestor.pathname) %>
- <%= form.hidden_field :quantity_id %>
- |
-
- <%= form.number_field :value, required: true, autofocus: true,
- size: 10 %>
+ <%= form.number_field :value, required: true, autofocus: true, size: 10 %>
|
<%= form.select :unit_id, options_from_collection_for_select(
@@ -17,9 +15,10 @@
|
- <%= 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 %>
|
<% end %>
<% end %>
diff --git a/app/views/measurements/_form_destroy_row.html.erb b/app/views/measurements/_form_destroy_row.html.erb
deleted file mode 100644
index f567f95..0000000
--- a/app/views/measurements/_form_destroy_row.html.erb
+++ /dev/null
@@ -1,2 +0,0 @@
-<%= turbo_stream.remove row %>
-<%= turbo_stream.update :flashes %>
diff --git a/app/views/measurements/_form_frame.html.erb b/app/views/measurements/_form_frame.html.erb
index f95f230..59dede4 100644
--- a/app/views/measurements/_form_frame.html.erb
+++ b/app/views/measurements/_form_frame.html.erb
@@ -1,7 +1,7 @@
<%= tabular_form_with url: new_measurement_path, html: {id: :new_readouts_form} do %>
<% if @readouts&.present? %>