forked from fixin.me/fixin.me
Implement measurements create/destroy and display existing readouts
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,7 +1,12 @@
|
|||||||
class MeasurementsController < ApplicationController
|
class MeasurementsController < ApplicationController
|
||||||
|
before_action :find_readout, only: :destroy
|
||||||
|
|
||||||
|
before_action except: :index do
|
||||||
|
raise AccessForbidden unless current_user.at_least(:active)
|
||||||
|
end
|
||||||
|
|
||||||
def index
|
def index
|
||||||
@measurements = []
|
@measurements = current_user.readouts.includes(:quantity, :unit).order(created_at: :desc)
|
||||||
#@measurements = current_user.units.ordered.includes(:base, :subunits)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def new
|
def new
|
||||||
@@ -9,8 +14,26 @@ class MeasurementsController < ApplicationController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def create
|
def create
|
||||||
|
readout_params = params.permit(readouts: Readout::ATTRIBUTES).fetch(:readouts, [])
|
||||||
|
@readouts = readout_params.map { |rp| current_user.readouts.build(rp) }
|
||||||
|
|
||||||
|
if @readouts.present? && @readouts.all?(&:valid?)
|
||||||
|
ActiveRecord::Base.transaction { @readouts.each(&:save!) }
|
||||||
|
flash.now[:notice] = t('.success', count: @readouts.size)
|
||||||
|
else
|
||||||
|
errors = @readouts.flat_map { |r| r.errors.full_messages }
|
||||||
|
flash.now[:alert] = errors.present? ? errors.first : t('.no_readouts')
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def destroy
|
def destroy
|
||||||
|
@readout.destroy!
|
||||||
|
flash.now[:notice] = t('.success')
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def find_readout
|
||||||
|
@readout = current_user.readouts.find(params[:id])
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
12
app/views/measurements/_readout.html.erb
Normal file
12
app/views/measurements/_readout.html.erb
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
<%= tag.tr id: dom_id(readout) do %>
|
||||||
|
<td><%= readout.quantity %></td>
|
||||||
|
<td class="ralign"><%= readout.value %></td>
|
||||||
|
<td><%= readout.unit %></td>
|
||||||
|
<td><%= l(readout.created_at) %></td>
|
||||||
|
<% if current_user.at_least(:active) %>
|
||||||
|
<td class="flex">
|
||||||
|
<%= image_button_to t('.destroy'), 'delete-outline', measurement_path(readout),
|
||||||
|
method: :delete, data: {turbo_stream: true} %>
|
||||||
|
</td>
|
||||||
|
<% end %>
|
||||||
|
<% end %>
|
||||||
11
app/views/measurements/create.turbo_stream.erb
Normal file
11
app/views/measurements/create.turbo_stream.erb
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
<% if @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, readout %>
|
||||||
|
<% end %>
|
||||||
|
<% else %>
|
||||||
|
<%= turbo_stream.update :flashes %>
|
||||||
|
<% end %>
|
||||||
3
app/views/measurements/destroy.turbo_stream.erb
Normal file
3
app/views/measurements/destroy.turbo_stream.erb
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
<%= turbo_stream.update :flashes %>
|
||||||
|
<%= turbo_stream.remove @readout %>
|
||||||
|
<%= turbo_stream.append(:measurements, render_no_items) if current_user.readouts.empty? %>
|
||||||
@@ -7,7 +7,18 @@
|
|||||||
<% end %>
|
<% end %>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<table class="main-area">
|
<table class="main-area items-table">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th><%= Quantity.model_name.human %></th>
|
||||||
|
<th><%= Readout.human_attribute_name(:value) %></th>
|
||||||
|
<th><%= Unit.model_name.human %></th>
|
||||||
|
<th><%= Readout.human_attribute_name(:created_at) %></th>
|
||||||
|
<% if current_user.at_least(:active) %>
|
||||||
|
<th></th>
|
||||||
|
<% end %>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
<tbody id="measurements">
|
<tbody id="measurements">
|
||||||
<%= render(@measurements) || render_no_items %>
|
<%= render(@measurements) || render_no_items %>
|
||||||
</tbody>
|
</tbody>
|
||||||
|
|||||||
@@ -13,6 +13,9 @@ en:
|
|||||||
quantity:
|
quantity:
|
||||||
description: Description
|
description: Description
|
||||||
name: Name
|
name: Name
|
||||||
|
readout:
|
||||||
|
created_at: Recorded at
|
||||||
|
value: Value
|
||||||
unit:
|
unit:
|
||||||
base: Base unit
|
base: Base unit
|
||||||
description: Description
|
description: Description
|
||||||
@@ -89,6 +92,15 @@ en:
|
|||||||
taken_at_html: Measurement taken at 
|
taken_at_html: Measurement taken at 
|
||||||
index:
|
index:
|
||||||
new_measurement: Add measurement
|
new_measurement: Add measurement
|
||||||
|
readout:
|
||||||
|
destroy: Delete
|
||||||
|
create:
|
||||||
|
success:
|
||||||
|
one: Recorded 1 measurement.
|
||||||
|
other: Recorded %{count} measurements.
|
||||||
|
no_readouts: No readouts selected.
|
||||||
|
destroy:
|
||||||
|
success: Measurement deleted.
|
||||||
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.
|
||||||
|
|||||||
Reference in New Issue
Block a user