Added MeasurementRoutine as a nested Measurement model
Updated ItemsWithQuantities to work with MeasurementRoutine Replaced ColumnViews HABTM with polymorphic HMT Added Measurement notes Added destroy restrictions on Quantity Replaced BodyTrackingPluginController with Finders concern Removed 'body_trackers' prefix from paths Unified styling for textarea
This commit is contained in:
@@ -1,4 +1,7 @@
|
||||
class BodyTrackersController < BodyTrackingPluginController
|
||||
class BodyTrackersController < ApplicationController
|
||||
layout 'body_tracking'
|
||||
menu_item :body_trackers
|
||||
|
||||
before_action :find_project_by_project_id, only: [:index, :defaults]
|
||||
before_action :authorize
|
||||
|
||||
@@ -43,10 +46,8 @@ class BodyTrackersController < BodyTrackingPluginController
|
||||
flash[:notice] += ", #{new_quantities_count > 0 ? new_quantities_count : "no" } new" \
|
||||
" #{'quantity'.pluralize(new_quantities_count)}"
|
||||
|
||||
ncv = @project.nutrients_column_view
|
||||
if ncv.quantities.count == 0
|
||||
ncv.quantities.append(@project.quantities.roots.first(6))
|
||||
ncv.save!
|
||||
if @project.nutrient_quantities.empty?
|
||||
@project.nutrient_quantities << @project.quantities.diet.roots.first(6)
|
||||
end
|
||||
|
||||
# Sources
|
||||
|
||||
@@ -1,17 +0,0 @@
|
||||
class BodyTrackingPluginController < ApplicationController
|
||||
menu_item :body_trackers
|
||||
layout 'body_tracking'
|
||||
|
||||
private
|
||||
|
||||
def find_quantity(id = :id)
|
||||
@quantity = Quantity.find(params[id])
|
||||
@project = @quantity.project
|
||||
rescue ActiveRecord::RecordNotFound
|
||||
render_404
|
||||
end
|
||||
|
||||
def find_quantity_by_quantity_id
|
||||
find_quantity(:quantity_id)
|
||||
end
|
||||
end
|
||||
42
app/controllers/concerns/finders.rb
Normal file
42
app/controllers/concerns/finders.rb
Normal file
@@ -0,0 +1,42 @@
|
||||
module Concerns::Finders
|
||||
private
|
||||
|
||||
def find_ingredient
|
||||
@ingredient = Ingredient.find(params[:id])
|
||||
@project = @ingredient.project
|
||||
rescue ActiveRecord::RecordNotFound
|
||||
render_404
|
||||
end
|
||||
|
||||
def find_measurement
|
||||
@measurement = Measurement.find(params[:id])
|
||||
@project = @measurement.routine.project
|
||||
rescue ActiveRecord::RecordNotFound
|
||||
render_404
|
||||
end
|
||||
|
||||
def find_measurement_routine
|
||||
@routine = MeasurementRoutine.find(params[:id])
|
||||
@project = @routine.project
|
||||
rescue ActiveRecord::RecordNotFound
|
||||
render_404
|
||||
end
|
||||
|
||||
def find_quantity(id = :id)
|
||||
@quantity = Quantity.find(params[id])
|
||||
@project = @quantity.project
|
||||
rescue ActiveRecord::RecordNotFound
|
||||
render_404
|
||||
end
|
||||
|
||||
def find_quantity_by_quantity_id
|
||||
find_quantity(:quantity_id)
|
||||
end
|
||||
|
||||
def find_unit
|
||||
@unit = Unit.find(params[:id])
|
||||
@project = @unit.project
|
||||
rescue ActiveRecord::RecordNotFound
|
||||
render_404
|
||||
end
|
||||
end
|
||||
@@ -1,9 +1,13 @@
|
||||
class IngredientsController < BodyTrackingPluginController
|
||||
class IngredientsController < ApplicationController
|
||||
require 'csv'
|
||||
|
||||
layout 'body_tracking'
|
||||
menu_item :body_trackers
|
||||
helper :body_trackers
|
||||
helper_method :current_view
|
||||
|
||||
include Concerns::Finders
|
||||
|
||||
before_action :init_session_filters
|
||||
before_action :find_project_by_project_id,
|
||||
only: [:index, :new, :create, :nutrients, :filter, :import]
|
||||
@@ -63,7 +67,7 @@ class IngredientsController < BodyTrackingPluginController
|
||||
end
|
||||
|
||||
def toggle_column
|
||||
@project.nutrients_column_view.toggle_column!(@quantity)
|
||||
@project.nutrient_columns.toggle!(@quantity)
|
||||
prepare_items
|
||||
render :index
|
||||
end
|
||||
@@ -201,15 +205,6 @@ class IngredientsController < BodyTrackingPluginController
|
||||
)
|
||||
end
|
||||
|
||||
# :find_* methods are called before :authorize,
|
||||
# @project is required for :authorize to succeed
|
||||
def find_ingredient
|
||||
@ingredient = Ingredient.find(params[:id])
|
||||
@project = @ingredient.project
|
||||
rescue ActiveRecord::RecordNotFound
|
||||
render_404
|
||||
end
|
||||
|
||||
def prepare_ingredients
|
||||
@ingredients, @formula_q = @project.ingredients
|
||||
.includes(:ref_unit, :source)
|
||||
@@ -217,7 +212,7 @@ class IngredientsController < BodyTrackingPluginController
|
||||
end
|
||||
|
||||
def prepare_nutrients
|
||||
@quantities = @project.nutrients_column_view.quantities.includes(:formula)
|
||||
@quantities = @project.nutrient_quantities.includes(:formula)
|
||||
@ingredients, @requested_n, @extra_n, @formula_q = @project.ingredients
|
||||
.filter(session[:i_filters], @quantities)
|
||||
end
|
||||
|
||||
@@ -1,11 +1,15 @@
|
||||
class MeasurementsController < BodyTrackingPluginController
|
||||
class MeasurementsController < ApplicationController
|
||||
layout 'body_tracking'
|
||||
menu_item :body_trackers
|
||||
helper :body_trackers
|
||||
|
||||
include Concerns::Finders
|
||||
|
||||
before_action :init_session_filters
|
||||
before_action :find_project_by_project_id, only: [:index, :new, :create, :filter]
|
||||
before_action :find_quantity_by_quantity_id, only: [:toggle_column]
|
||||
before_action :find_measurement,
|
||||
only: [:edit, :update, :destroy, :retake, :readouts, :toggle_column]
|
||||
before_action :find_measurement, only: [:edit, :update, :destroy, :retake]
|
||||
before_action :find_measurement_routine, only: [:readouts, :toggle_column]
|
||||
before_action :authorize
|
||||
|
||||
def index
|
||||
@@ -15,12 +19,18 @@ class MeasurementsController < BodyTrackingPluginController
|
||||
|
||||
def new
|
||||
@measurement = @project.measurements.new
|
||||
@measurement.build_routine
|
||||
@measurement.readouts.new
|
||||
end
|
||||
|
||||
def create
|
||||
@measurement = @project.measurements.new(measurement_params)
|
||||
@measurement.routine.project = @project
|
||||
if @measurement.save
|
||||
if @measurement.routine.columns.empty?
|
||||
@measurement.routine.quantities << @measurement.readouts.map(&:quantity).first(6)
|
||||
end
|
||||
|
||||
flash[:notice] = 'Created new measurement'
|
||||
readouts_view? ? prepare_readouts : prepare_measurements
|
||||
else
|
||||
@@ -58,12 +68,12 @@ class MeasurementsController < BodyTrackingPluginController
|
||||
end
|
||||
|
||||
def readouts
|
||||
session[:m_filters][:scope] = {name: @measurement.name}
|
||||
#session[:m_filters][:scope] = {routine: @routine}
|
||||
prepare_readouts
|
||||
end
|
||||
|
||||
def toggle_column
|
||||
@measurement.column_view.toggle_column!(@quantity)
|
||||
@routine.columns.toggle!(@quantity)
|
||||
prepare_readouts
|
||||
render :index
|
||||
end
|
||||
@@ -82,8 +92,13 @@ class MeasurementsController < BodyTrackingPluginController
|
||||
|
||||
def measurement_params
|
||||
params.require(:measurement).permit(
|
||||
:name,
|
||||
:notes,
|
||||
:source_id,
|
||||
routine_attributes:
|
||||
[
|
||||
:name,
|
||||
:description
|
||||
],
|
||||
readouts_attributes:
|
||||
[
|
||||
:id,
|
||||
@@ -95,30 +110,17 @@ class MeasurementsController < BodyTrackingPluginController
|
||||
)
|
||||
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, @formula_q = @project.measurements
|
||||
.includes(:source, :readouts)
|
||||
.includes(:routine, :source, :readouts)
|
||||
.filter(session[:m_filters])
|
||||
end
|
||||
|
||||
def prepare_readouts
|
||||
@scoping_measurement = @project.measurements.where(session[:m_filters][:scope]).first!
|
||||
@quantities = @scoping_measurement.column_view.quantities.includes(:formula)
|
||||
@measurements, @requested_r, @extra_r, @formula_q = @project.measurements
|
||||
.includes(:source)
|
||||
@quantities = @routine.quantities.includes(:formula)
|
||||
@measurements, @requested_r, @extra_r, @formula_q = @routine.measurements
|
||||
.includes(:routine, :source)
|
||||
.filter(session[:m_filters], @quantities)
|
||||
rescue ActiveRecord::RecordNotFound
|
||||
session[:m_filters][:scope] = {}
|
||||
render_404
|
||||
end
|
||||
|
||||
def readouts_view?
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
class QuantitiesController < BodyTrackingPluginController
|
||||
class QuantitiesController < ApplicationController
|
||||
layout 'body_tracking'
|
||||
menu_item :body_trackers
|
||||
helper :body_trackers
|
||||
|
||||
include Concerns::Finders
|
||||
|
||||
before_action :init_session_filters
|
||||
before_action :find_project_by_project_id, only: [:index, :new, :create, :filter, :parents]
|
||||
before_action :find_quantity, only: [:edit, :update, :destroy, :move,
|
||||
@@ -118,6 +122,6 @@ class QuantitiesController < BodyTrackingPluginController
|
||||
|
||||
def prepare_quantities
|
||||
@quantities = @project.quantities.filter(@project, session[:q_filters])
|
||||
.includes(:column_views, :formula, :parent)
|
||||
.includes(:columns, :formula, :parent)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
class SourcesController < BodyTrackingPluginController
|
||||
class SourcesController < ApplicationController
|
||||
layout 'body_tracking'
|
||||
menu_item :body_trackers
|
||||
|
||||
before_action :find_project_by_project_id, only: [:index, :create]
|
||||
before_action :find_source, only: [:destroy]
|
||||
before_action :authorize
|
||||
|
||||
@@ -1,4 +1,9 @@
|
||||
class UnitsController < BodyTrackingPluginController
|
||||
class UnitsController < ApplicationController
|
||||
layout 'body_tracking'
|
||||
menu_item :body_trackers
|
||||
|
||||
include Concerns::Finders
|
||||
|
||||
before_action :find_project_by_project_id, only: [:index, :create]
|
||||
before_action :find_unit, only: [:destroy]
|
||||
before_action :authorize
|
||||
@@ -34,13 +39,4 @@ class UnitsController < BodyTrackingPluginController
|
||||
:shortname
|
||||
)
|
||||
end
|
||||
|
||||
# :find_* methods are called before :authorize,
|
||||
# @project is required for :authorize to succeed
|
||||
def find_unit
|
||||
@unit = Unit.find(params[:id])
|
||||
@project = @unit.project
|
||||
rescue ActiveRecord::RecordNotFound
|
||||
render_404
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user