forked from fixin.me/fixin.me
- 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>
63 lines
1.7 KiB
Ruby
63 lines
1.7 KiB
Ruby
class MeasurementsController < ApplicationController
|
|
before_action :find_readout, only: [:destroy, :edit, :update]
|
|
|
|
before_action except: :index do
|
|
raise AccessForbidden unless current_user.at_least(:active)
|
|
end
|
|
|
|
def index
|
|
load_measurements
|
|
end
|
|
|
|
def new
|
|
@quantities = current_user.quantities.ordered
|
|
end
|
|
|
|
def create
|
|
taken_at = params.permit(:taken_at)[:taken_at]
|
|
readout_params = params.permit(readouts: Readout::ATTRIBUTES).fetch(:readouts, [])
|
|
@readouts = readout_params.map { |rp| current_user.readouts.build(rp.merge(taken_at: taken_at)) }
|
|
|
|
if @readouts.present? && @readouts.all?(&:valid?)
|
|
ActiveRecord::Base.transaction { @readouts.each(&:save!) }
|
|
load_measurements
|
|
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
|
|
|
|
def edit
|
|
@user_units = current_user.units.ordered
|
|
end
|
|
|
|
def update
|
|
if @readout.update(params.require(:readout).permit(:value, :unit_id, :taken_at))
|
|
load_measurements
|
|
flash.now[:notice] = t('.success')
|
|
else
|
|
@user_units = current_user.units.ordered
|
|
render :edit
|
|
end
|
|
end
|
|
|
|
def destroy
|
|
@readout.destroy!
|
|
load_measurements
|
|
flash.now[:notice] = t('.success')
|
|
end
|
|
|
|
private
|
|
|
|
def find_readout
|
|
@readout = current_user.readouts.find(params[:id])
|
|
end
|
|
|
|
def load_measurements
|
|
@measurements = current_user.readouts.includes(:quantity, :unit).order(taken_at: :desc, id: :desc)
|
|
@wide_groups = @measurements.group_by(&:taken_at)
|
|
@wide_quantities = @measurements.map(&:quantity).uniq.sort_by(&:name)
|
|
end
|
|
end
|