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

View File

@ -48,9 +48,27 @@ class TargetsTest < BodyTrackingSystemTestCase
click_on t(:button_create)
end
end
assert_no_selector 'form#new-target-form'
assert_selector 'table#targets tbody tr', count: @project1.targets.count
end
def test_create_binding_target_when_binding_goal_does_not_exist
@project1.goals.delete_all
assert_equal 0, @project1.goals.count
visit project_targets_path(@project1)
click_link t('targets.contextual.link_new_target')
within 'form#new-target-form' do
# Assume binding Goal is initialized (not saved) and selected by default
within 'p.target' do
select quantities(:quantities_energy).name
select '=='
fill_in with: '1750'
select units(:units_kcal).shortname
end
assert_difference ['Goal.count', '@project1.goals.reload.count(&:is_binding?)'], 1 do
click_on t(:button_create)
end
end
assert_selector 'table#targets tbody tr', count: 1
end
end