1
0

Allow binding goal saving w/o exposures and disallow destruction

This commit is contained in:
cryptogopher 2020-08-22 23:17:24 +02:00
parent 8f0e718b4a
commit cf0e14d87d
6 changed files with 51 additions and 33 deletions

View File

@ -1,6 +1,13 @@
module Concerns::Finders module Concerns::Finders
private private
def find_goal(id = params[:id])
@goal = Goal.find(id)
@project = @goal.project
rescue ActiveRecord::RecordNotFound
render_404
end
def find_meal def find_meal
@meal = Meal.find(params[:id]) @meal = Meal.find(params[:id])
@project = @meal.project @project = @meal.project

View File

@ -22,9 +22,9 @@ class TargetsController < ApplicationController
end end
def create def create
goal = @project.goals.binding if params[:goal][:id].blank? goal_id = params[:goal][:id]
goal ||= @project.goals.find_by(id: params[:goal][:id]) goal = goal_id.present? ? @project.goals.find(goal_id) : @project.goals.new
goal ||= @project.goals.build(goal_params) goal.attributes = goal_params unless goal.is_binding?
@targets = goal.targets.build(targets_params[:targets]) do |target| @targets = goal.targets.build(targets_params[:targets]) do |target|
target.effective_from = params[:target][:effective_from] target.effective_from = params[:target][:effective_from]
@ -70,7 +70,7 @@ class TargetsController < ApplicationController
private private
def goal_params def goal_params
params.require(:goal).permit(:id, :name, :description) params.require(:goal).permit(:name, :description)
end end
def targets_params def targets_params

View File

@ -6,7 +6,7 @@ class Goal < ActiveRecord::Base
class_name: 'Exposure', extend: BodyTracking::TogglableExposures class_name: 'Exposure', extend: BodyTracking::TogglableExposures
has_many :quantities, -> { order "lft" }, through: :target_exposures has_many :quantities, -> { order "lft" }, through: :target_exposures
validates :target_exposures, presence: true validates :target_exposures, presence: true, unless: :is_binding?
validates :is_binding, uniqueness: {scope: :project_id}, if: :is_binding? validates :is_binding, uniqueness: {scope: :project_id}, if: :is_binding?
validates :name, presence: true, uniqueness: {scope: :project_id}, validates :name, presence: true, uniqueness: {scope: :project_id},
exclusion: {in: [I18n.t('targets.form.binding_goal')], unless: :is_binding?} exclusion: {in: [I18n.t('targets.form.binding_goal')], unless: :is_binding?}
@ -16,4 +16,7 @@ class Goal < ActiveRecord::Base
self.is_binding = false if self.is_binding.nil? self.is_binding = false if self.is_binding.nil?
end end
end end
before_destroy prepend: true do
!is_binding?
end
end end

View File

@ -31,7 +31,7 @@ module BodyTracking::ProjectPatch
has_many :goals, dependent: :destroy do has_many :goals, dependent: :destroy do
def binding def binding
find_or_initialize_by(is_binding: true) do |goal| find_or_create_by(is_binding: true) do |goal|
goal.name = I18n.t('targets.form.binding_goal') goal.name = I18n.t('targets.form.binding_goal')
end end
end end

View File

@ -2,3 +2,7 @@ goals_binding:
project_id: 1 project_id: 1
is_binding: true is_binding: true
name: "<%= I18n.t 'targets.form.binding_goal' %>" name: "<%= I18n.t 'targets.form.binding_goal' %>"
goals_non_binding:
project_id: 1
is_binding: false
name: "Non-binding goal"

View File

@ -23,7 +23,7 @@ class TargetsTest < BodyTrackingSystemTestCase
assert_selector 'div#targets', visible: :yes, exact_text: t(:label_no_data) assert_selector 'div#targets', visible: :yes, exact_text: t(:label_no_data)
end end
def test_index_shows_and_hides_new_target_form def test_index_show_and_hide_new_target_form
visit project_targets_path(@project1) visit project_targets_path(@project1)
assert_no_selector 'form#new-target-form' assert_no_selector 'form#new-target-form'
click_link t('targets.contextual.link_new_target') click_link t('targets.contextual.link_new_target')
@ -33,7 +33,8 @@ class TargetsTest < BodyTrackingSystemTestCase
end end
def test_create_binding_target def test_create_binding_target
assert @project1.goals.exists?(is_binding: true) assert_difference 'Goal.count' => 0, 'Target.count' => 1,
'@project1.targets.reload.count' => 1, 'Threshold.count' => 1 do
visit project_targets_path(@project1) visit project_targets_path(@project1)
click_link t('targets.contextual.link_new_target') click_link t('targets.contextual.link_new_target')
within 'form#new-target-form' do within 'form#new-target-form' do
@ -45,11 +46,10 @@ class TargetsTest < BodyTrackingSystemTestCase
fill_in with: '1750' fill_in with: '1750'
select units(:units_kcal).shortname select units(:units_kcal).shortname
end end
assert_difference 'Target.count' => 1, '@project1.targets.reload.count' => 1,
'Threshold.count' => 1, 'Goal.count' => 0 do
click_on t(:button_create) click_on t(:button_create)
end end
end end
assert_equal @project1.goals.binding, Target.last.goal
assert_no_selector 'form#new-target-form' assert_no_selector 'form#new-target-form'
assert_selector 'table#targets tbody tr', count: @project1.targets.count assert_selector 'table#targets tbody tr', count: @project1.targets.count
end end
@ -57,20 +57,24 @@ class TargetsTest < BodyTrackingSystemTestCase
def test_create_binding_target_when_binding_goal_does_not_exist def test_create_binding_target_when_binding_goal_does_not_exist
@project1.goals.where(is_binding: true).delete_all @project1.goals.where(is_binding: true).delete_all
assert_equal 0, @project1.goals.count(&:is_binding?) assert_equal 0, @project1.goals.count(&:is_binding?)
assert_difference ['Goal.count', '@project1.goals.reload.count(&:is_binding?)',
'@project1.targets.reload.count'], 1 do
visit project_targets_path(@project1) visit project_targets_path(@project1)
click_link t('targets.contextual.link_new_target') click_link t('targets.contextual.link_new_target')
within 'form#new-target-form' do within 'form#new-target-form' do
# Assume binding Goal is initialized (not saved) and selected by default # Assume binding Goal is selected by default
within 'p.target' do within 'p.target' do
select quantities(:quantities_energy).name select quantities(:quantities_energy).name
select '==' select '=='
fill_in with: '1750' fill_in with: '1750'
select units(:units_kcal).shortname select units(:units_kcal).shortname
end end
assert_difference ['Goal.count', '@project1.goals.reload.count(&:is_binding?)'], 1 do
click_on t(:button_create) click_on t(:button_create)
end end
end end
assert_selector 'table#targets tbody tr', count: 1 assert_equal @project1.goals.binding, Target.last.goal
end
def test_edit
end end
end end