1
0
This repository has been archived on 2023-12-07. You can view files and clone it, but cannot push or open issues or pull requests.
body_tracking/app/controllers/measurements_controller.rb

83 lines
1.7 KiB
Ruby

class MeasurementsController < ApplicationController
menu_item :body_trackers
before_action :find_project_by_project_id, only: [:index, :create]
before_action :find_measurement, only: [:edit, :update, :destroy, :retake]
before_action :authorize
def index
@measurement = @project.measurements.new
@measurement.readouts.new
prepare_measurements
@measurements << @measurement
end
def create
@measurement = @project.measurements.new(measurement_params)
if @measurement.save
flash[:notice] = 'Created new measurement'
redirect_to :back
else
prepare_measurements
@measurement.readouts.new if @measurement.readouts.empty?
render :index
end
end
def edit
prepare_measurements
render :index
end
def update
if @measurement.update(measurement_params)
flash[:notice] = 'Updated measurement'
end
prepare_measurements
render :index
end
def destroy
if @measurement.destroy
flash[:notice] = 'Deleted measurement'
end
prepare_measurements
render :index
end
def retake
prepare_measurements
end
private
def measurement_params
params.require(:measurement).permit(
:name,
:source_id,
readouts_attributes:
[
:id,
:quantity_id,
:value,
:unit_id,
:_destroy
]
)
end
# :find_* methods are called before :authorize,
# @project is required for :authorize to succeed
def find_measurement
@measurement = Measurement.find(params[:id])
@project = @measurement.project
rescue ActiveRecord::RecordNotFound
render_404
end
def prepare_measurements
@measurements = @project.measurements.includes(:source)
end
end