Adding MeasurementRoutine selection/editing support
This commit is contained in:
@@ -10,7 +10,8 @@ module Concerns::Finders
|
||||
|
||||
def find_measurement
|
||||
@measurement = Measurement.find(params[:id])
|
||||
@project = @measurement.routine.project
|
||||
@routine = @measurement.routine
|
||||
@project = @routine.project
|
||||
rescue ActiveRecord::RecordNotFound
|
||||
render_404
|
||||
end
|
||||
@@ -22,15 +23,22 @@ module Concerns::Finders
|
||||
render_404
|
||||
end
|
||||
|
||||
def find_quantity(id = :id)
|
||||
@quantity = Quantity.find(params[id])
|
||||
def find_measurement_routine(id = params[:id])
|
||||
@routine = MeasurementRoutine.find(id)
|
||||
@project = @routine.project
|
||||
rescue ActiveRecord::RecordNotFound
|
||||
render_404
|
||||
end
|
||||
|
||||
def find_quantity(id = params[:id])
|
||||
@quantity = Quantity.find(id)
|
||||
@project = @quantity.project
|
||||
rescue ActiveRecord::RecordNotFound
|
||||
render_404
|
||||
end
|
||||
|
||||
def find_quantity_by_quantity_id
|
||||
find_quantity(:quantity_id)
|
||||
find_quantity(params[:quantity_id])
|
||||
end
|
||||
|
||||
def find_unit
|
||||
|
||||
@@ -23,6 +23,7 @@ class IngredientsController < ApplicationController
|
||||
def new
|
||||
@ingredient = @project.ingredients.new
|
||||
# passing attr for Nutrient after_initialize
|
||||
# FIXME: is this necessary when creating through association?
|
||||
@ingredient.nutrients.new(ingredient: @ingredient)
|
||||
end
|
||||
|
||||
|
||||
12
app/controllers/measurement_routines_controller.rb
Normal file
12
app/controllers/measurement_routines_controller.rb
Normal file
@@ -0,0 +1,12 @@
|
||||
class MeasurementRoutinesController < ApplicationController
|
||||
include Concerns::Finders
|
||||
|
||||
before_action :find_measurement_routine, only: [:show, :edit]
|
||||
before_action :authorize
|
||||
|
||||
def show
|
||||
end
|
||||
|
||||
def edit
|
||||
end
|
||||
end
|
||||
@@ -18,16 +18,25 @@ class MeasurementsController < ApplicationController
|
||||
|
||||
def new
|
||||
@measurement = @project.measurements.new
|
||||
@measurement.build_routine
|
||||
@routine = @measurement.build_routine
|
||||
@measurement.readouts.new
|
||||
end
|
||||
|
||||
def create
|
||||
@measurement = @project.measurements.new(measurement_params)
|
||||
# Nested attributes cannot create outer object (Measurement) and at the same time edit
|
||||
# existing nested object (MeasurementRoutine) if it's not associated with outer object
|
||||
# https://stackoverflow.com/questions/6346134/
|
||||
# That's why routine needs to be found and associated before measurement initialization
|
||||
@measurement = @project.measurements.new do |m|
|
||||
routine_id = params[:measurement][:routine_attributes][:id]
|
||||
m.routine = @project.measurement_routines.find_by(id: routine_id) if routine_id
|
||||
end
|
||||
@measurement.attributes = measurement_params
|
||||
@measurement.routine.project = @project
|
||||
@routine = @measurement.routine
|
||||
if @measurement.save
|
||||
if @measurement.routine.columns.empty?
|
||||
@measurement.routine.quantities << @measurement.readouts.map(&:quantity).first(6)
|
||||
if @routine.columns.empty?
|
||||
@routine.quantities << @measurement.readouts.map(&:quantity).first(6)
|
||||
end
|
||||
|
||||
flash[:notice] = 'Created new measurement'
|
||||
@@ -93,6 +102,7 @@ class MeasurementsController < ApplicationController
|
||||
:source_id,
|
||||
routine_attributes:
|
||||
[
|
||||
:id,
|
||||
:name,
|
||||
:description
|
||||
],
|
||||
@@ -118,7 +128,7 @@ class MeasurementsController < ApplicationController
|
||||
end
|
||||
|
||||
def prepare_readouts
|
||||
@quantities = @measurement.routine.quantities.includes(:formula)
|
||||
@quantities = @routine.quantities.includes(:formula)
|
||||
@measurements, @requested_r, @extra_r, @formula_q = @routine.measurements
|
||||
.includes(:routine, :source)
|
||||
.filter(session[:m_filters], @quantities)
|
||||
|
||||
Reference in New Issue
Block a user