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
|
||||
before_action :find_readout, only: :destroy
|
||||
|
||||
before_action except: :index do
|
||||
raise AccessForbidden unless current_user.at_least(:active)
|
||||
end
|
||||
|
||||
def index
|
||||
@measurements = []
|
||||
#@measurements = current_user.units.ordered.includes(:base, :subunits)
|
||||
@measurements = current_user.readouts.includes(:quantity, :unit).order(created_at: :desc)
|
||||
end
|
||||
|
||||
def new
|
||||
@@ -9,8 +14,26 @@ class MeasurementsController < ApplicationController
|
||||
end
|
||||
|
||||
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
|
||||
|
||||
def destroy
|
||||
@readout.destroy!
|
||||
flash.now[:notice] = t('.success')
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def find_readout
|
||||
@readout = current_user.readouts.find(params[:id])
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user