diff --git a/app/controllers/measurements_controller.rb b/app/controllers/measurements_controller.rb
index 0ef5763..661c6c2 100644
--- a/app/controllers/measurements_controller.rb
+++ b/app/controllers/measurements_controller.rb
@@ -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
diff --git a/app/models/quantity.rb b/app/models/quantity.rb
index 604d3f5..fa7b14c 100644
--- a/app/models/quantity.rb
+++ b/app/models/quantity.rb
@@ -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
diff --git a/app/views/measurements/_form.html.erb b/app/views/measurements/_form.html.erb
index dcd4ff0..bf244e7 100644
--- a/app/views/measurements/_form.html.erb
+++ b/app/views/measurements/_form.html.erb
@@ -1,45 +1,26 @@
-<%= tabular_form_with model: current_user, html: {id: :new_readouts_form} do |user| %>
- <% if @readouts&.present? %>
-
- <%= tag.legend @ancestor_fullname unless @ancestor_fullname.empty? %>
-
-
- <%= user.fields_for :readouts, @readouts do |form| %>
- <% row = dom_id(form.object.quantity, :new, :readout) %>
- <%- tag.tr id: row, onkeydown: 'processKey(event)' do %>
-
- <%= form.object.quantity.fullname.delete_prefix(@ancestor_fullname) %>
- <%= form.hidden_field :quantity_id %>
-
-
- <%= form.number_field :value, required: true, autofocus: true,
- size: 10 %>
-
-
- <%= form.select :unit_id, options_from_collection_for_select(
- @units, :id, ->(u){ sanitize(' '*(u.base_id ? 1 : 0) + u.symbol) }
- ) %>
-
+<% @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 %>
+
+ <%= form.object.quantity.full_name.delete_prefix(@closest_ancestor.full_name) %>
+ <%= form.hidden_field :quantity_id %>
+
+
+ <%= form.number_field :value, required: true, autofocus: true,
+ size: 10 %>
+
+
+ <%= form.select :unit_id, options_from_collection_for_select(
+ @units, :id, ->(u){ sanitize(' '*(u.base_id ? 1 : 0) + u.symbol) }
+ ) %>
+
-
- <%= image_link_to t(:delete), 'delete-outline', quantities_path,
- class: 'dangerous',
- onclick: render_turbo_stream('form_destroy_row', {row: row}) %>
-
- <% end %>
- <% end %>
-
-
-
- <%= user.button %>
-
- <%= image_link_to t(:cancel), "close-outline", measurements_path,
- class: 'dangerous', name: :cancel,
- onclick: render_turbo_stream('form_close') %>
-
-
-
-
-
+
+ <%= image_link_to t(:delete), 'delete-outline', quantities_path,
+ class: 'dangerous',
+ onclick: render_turbo_stream('form_destroy_row', {row: row}) %>
+
+ <% end %>
<% end %>
<% end %>
diff --git a/app/views/measurements/_form_frame.html.erb b/app/views/measurements/_form_frame.html.erb
new file mode 100644
index 0000000..7917ab3
--- /dev/null
+++ b/app/views/measurements/_form_frame.html.erb
@@ -0,0 +1,21 @@
+<%= tabular_form_with url: new_measurement_path, html: {id: :new_readouts_form} do %>
+ <% if @readouts&.present? %>
+
+ <%= tag.legend @closest_ancestor.full_name if @closest_ancestor&.full_name %>
+
+
+
+
+
+ <%= button_tag %>
+
+ <%= image_link_to t(:cancel), "close-outline", measurements_path,
+ class: 'dangerous', name: :cancel,
+ onclick: render_turbo_stream('form_close') %>
+
+
+
+
+
+ <% end %>
+<% end %>
diff --git a/app/views/measurements/index.html.erb b/app/views/measurements/index.html.erb
index 9d5aec4..7be4cbf 100644
--- a/app/views/measurements/index.html.erb
+++ b/app/views/measurements/index.html.erb
@@ -1,6 +1,6 @@
<% if current_user.at_least(:active) %>
- <%= render partial: 'form' %>
+ <%= render partial: 'form_frame' %>
<%# TODO: show hint when no quantities/units defined %>
<%= select_tag :id, options_from_collection_for_select(
diff --git a/app/views/measurements/new.turbo_stream.erb b/app/views/measurements/new.turbo_stream.erb
index 4c3a55c..a70123e 100644
--- a/app/views/measurements/new.turbo_stream.erb
+++ b/app/views/measurements/new.turbo_stream.erb
@@ -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 %>