forked from fixin.me/fixin.me
Replace JS-generated wide table with ERB partial and Turbo Streams
- Add _wide_table.html.erb partial (server-rendered pivot table) - Add load_measurements helper in controller to prepare @wide_groups and @wide_quantities for all mutating actions - Update index view to render the wide_table partial in #measurements-wide - Add/update create, destroy, update turbo_stream views to refresh the wide table atomically after each mutation - Remove buildWideTable() and editMeasurementWide() from application.js - Fix create.turbo_stream.erb condition (empty readouts are vacuously all persisted) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
41
app/views/measurements/_wide_table.html.erb
Normal file
41
app/views/measurements/_wide_table.html.erb
Normal file
@@ -0,0 +1,41 @@
|
||||
<table class="items-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th><%= Readout.human_attribute_name(:taken_at) %></th>
|
||||
<% wide_quantities.each do |q| %>
|
||||
<th><%= q.name %></th>
|
||||
<% end %>
|
||||
<th><%= Readout.human_attribute_name(:created_at) %></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<% wide_groups.each do |taken_at, readouts| %>
|
||||
<tr>
|
||||
<td><%= l(taken_at) if taken_at %></td>
|
||||
<% wide_quantities.each do |q| %>
|
||||
<% readout = readouts.find { |r| r.quantity_id == q.id } %>
|
||||
<td class="ralign">
|
||||
<% if readout %>
|
||||
<span class="wide-cell">
|
||||
<% if current_user.at_least(:active) %>
|
||||
<%= link_to format("%.10g", readout.value),
|
||||
edit_measurement_path(readout, view: :wide),
|
||||
class: 'link', onclick: 'this.blur();',
|
||||
data: {turbo_stream: true} %>
|
||||
<% else %>
|
||||
<%= format("%.10g", readout.value) %>
|
||||
<% end %>
|
||||
<%= readout.unit.symbol %>
|
||||
<% if current_user.at_least(:active) %>
|
||||
<%= image_button_to '', 'delete-outline', measurement_path(readout),
|
||||
method: :delete, data: {turbo_stream: true} %>
|
||||
<% end %>
|
||||
</span>
|
||||
<% end %>
|
||||
</td>
|
||||
<% end %>
|
||||
<td><%= l(readouts.first.created_at) %></td>
|
||||
</tr>
|
||||
<% end %>
|
||||
</tbody>
|
||||
</table>
|
||||
13
app/views/measurements/create.turbo_stream.erb
Normal file
13
app/views/measurements/create.turbo_stream.erb
Normal file
@@ -0,0 +1,13 @@
|
||||
<% if @readouts.present? && @readouts.all?(&:persisted?) %>
|
||||
<%= turbo_stream.update :flashes %>
|
||||
<%= turbo_stream.remove :measurement_form %>
|
||||
<%= turbo_stream.enable :new_measurement_link %>
|
||||
<%= turbo_stream.remove :no_items %>
|
||||
<% @readouts.each do |readout| %>
|
||||
<%= turbo_stream.prepend :measurements, partial: 'readout', locals: {readout: readout} %>
|
||||
<% end %>
|
||||
<%= turbo_stream.update 'measurements-wide', partial: 'wide_table',
|
||||
locals: {wide_groups: @wide_groups, wide_quantities: @wide_quantities} %>
|
||||
<% else %>
|
||||
<%= turbo_stream.update :flashes %>
|
||||
<% end %>
|
||||
5
app/views/measurements/destroy.turbo_stream.erb
Normal file
5
app/views/measurements/destroy.turbo_stream.erb
Normal file
@@ -0,0 +1,5 @@
|
||||
<%= turbo_stream.update :flashes %>
|
||||
<%= turbo_stream.remove @readout %>
|
||||
<%= turbo_stream.append(:measurements, render_no_items) if current_user.readouts.empty? %>
|
||||
<%= turbo_stream.update 'measurements-wide', partial: 'wide_table',
|
||||
locals: {wide_groups: @wide_groups, wide_quantities: @wide_quantities} %>
|
||||
@@ -33,6 +33,8 @@
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<div id="measurements-wide" class="measurements-wide"></div>
|
||||
<div id="measurements-wide" class="measurements-wide">
|
||||
<%= render 'wide_table', wide_groups: @wide_groups, wide_quantities: @wide_quantities %>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
@@ -1,2 +1,4 @@
|
||||
<%= turbo_stream.close_form dom_id(@readout, :edit) %>
|
||||
<%= turbo_stream.replace @readout, partial: 'measurements/readout', locals: {readout: @readout} %>
|
||||
<%= turbo_stream.update 'measurements-wide', partial: 'wide_table',
|
||||
locals: {wide_groups: @wide_groups, wide_quantities: @wide_quantities} %>
|
||||
|
||||
Reference in New Issue
Block a user