1
0

Added: test_create_binding_target_when_binding_goal_does_not_exist

This commit is contained in:
cryptogopher
2020-08-22 16:11:43 +02:00
parent d8a8d9f75e
commit 8b43f22e66
3 changed files with 26 additions and 7 deletions

View File

@@ -22,19 +22,19 @@ class TargetsController < ApplicationController
end
def create
goal = @project.goals.find_by(id: params[:goal][:id]) || @project.goals.build(goal_params)
goal = @project.goals.binding if params[:goal][:id].blank?
goal ||= @project.goals.find_by(id: params[:goal][:id])
goal ||= @project.goals.build(goal_params)
@targets = goal.targets.build(targets_params[:targets]) do |target|
target.effective_from = params[:target][:effective_from]
end
# FIXME: add goal exposures before save and require (in model) goal.target_exposures to
# be present (same for measurement/food?)
if goal.target_exposures.empty?
goal.quantities << @targets.map { |t| t.thresholds.first.quantity }.first(6)
end
# :save only after build, to re-display values in case records are invalid
if goal.save && Target.transaction { @targets.all?(&:save) }
if goal.target_exposures.empty?
goal.quantities << @targets.map { |t| t.thresholds.first.quantity }.first(6)
end
flash[:notice] = 'Created new target(s)'
prepare_targets
else

View File

@@ -7,5 +7,6 @@ class MeasurementRoutine < ActiveRecord::Base
class_name: 'Exposure', extend: BodyTracking::TogglableExposures
has_many :quantities, -> { order "lft" }, through: :readout_exposures
# TODO: require readout_exposures to be present
validates :name, presence: true, uniqueness: {scope: :project_id}
end