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/quantities_controller.rb
cryptogopher 3ce5d5c940 Fix migration of default quantities
Allow Quantity formula to be nil but not blank
Add computed energy quantity
Reverse default quantities deletion order
2019-11-15 00:04:34 +01:00

73 lines
1.7 KiB
Ruby

class QuantitiesController < ApplicationController
before_action :find_project_by_project_id, only: [:index, :create]
before_action :find_quantity, only: [:destroy, :toggle, :up, :down, :left, :right]
before_action :authorize
def index
@quantity = @project.quantities.new
@quantities = @project.quantities
end
def create
@quantity = @project.quantities.new(quantity_params)
if @quantity.save
flash[:notice] = 'Created new quantity'
redirect_to project_quantities_url(@project)
else
@quantities = @project.quantities
render :index
end
end
def destroy
if @quantity.destroy
flash[:notice] = 'Deleted quantity'
end
@quantities = @project.quantities
render :toggle
end
def toggle
@quantity.toggle_primary!
@quantities = @project.quantities
end
def up
@quantity.move_left if @quantity.left_sibling.present?
@quantities = @project.quantities
render :toggle
end
def down
@quantity.move_right if @quantity.right_sibling.present?
@quantities = @project.quantities
render :toggle
end
def left
@quantity.move_to_right_of(@quantity.parent) if @quantity.parent.present?
@quantities = @project.quantities
render :toggle
end
def right
@quantity.move_to_child_of(@quantity.left_sibling) if @quantity.left_sibling.present?
@quantities = @project.quantities
render :toggle
end
private
def quantity_params
params[:quantity].delete(:formula) if params[:quantity][:formula].blank?
params.require(:quantity).permit(
:domain,
:parent_id,
:name,
:description,
:formula,
:primary
)
end
end