1
0

Add GoalController: #index, #new, #create

This commit is contained in:
cryptogopher
2021-02-21 18:10:15 +01:00
parent ea308a1e4a
commit 7f87b3bc84
20 changed files with 182 additions and 76 deletions

View File

@@ -1,12 +1,60 @@
class GoalsController < ApplicationController
layout 'body_tracking', except: :subthresholds
menu_item :body_trackers
helper :body_trackers
include Concerns::Finders
before_action :find_project_by_project_id, only: [:index, :new, :create]
before_action :find_goal, only: [:show, :edit]
before_action :authorize
def index
@goals = @project.goals
end
def new
@goal = @project.goals.new
@targets = @goal.targets
end
def create
@goal = @project.goals.new(goal_params)
if @goal.save
flash.now[:notice] = 'Created new goal'
@goals = @project.goals
else
@targets = @goal.targets
render :new
end
end
def show
end
def edit
end
private
def goal_params
params.require(:goal).permit(
:name,
:description,
targets_attributes:
[
:id,
:quantity_id,
:scope,
:destroy,
thresholds_attributes: [
:id,
:quantity_id,
:value,
:unit_id,
:_destroy
]
]
)
end
end

View File

@@ -33,10 +33,6 @@ class TargetsController < ApplicationController
target.effective_from = params[:target][:effective_from]
end
if @goal.target_exposures.empty?
@goal.quantities << @targets.map(&:quantity)[0..5]
end
# :save only after build, to re-display values in case records are invalid
if @goal.save
flash.now[:notice] = 'Created new target(s)'
@@ -85,13 +81,6 @@ class TargetsController < ApplicationController
private
def goal_params
params.require(:goal).permit(
:name,
:description
)
end
def targets_params
params.require(:goal).permit(
targets_attributes: