Allow binding goal saving w/o exposures and disallow destruction
This commit is contained in:
parent
8f0e718b4a
commit
cf0e14d87d
@ -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
|
||||||
|
@ -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
|
||||||
|
@ -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
|
||||||
|
@ -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
|
||||||
|
4
test/fixtures/goals.yml
vendored
4
test/fixtures/goals.yml
vendored
@ -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"
|
||||||
|
@ -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,23 +33,23 @@ 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,
|
||||||
visit project_targets_path(@project1)
|
'@project1.targets.reload.count' => 1, 'Threshold.count' => 1 do
|
||||||
click_link t('targets.contextual.link_new_target')
|
visit project_targets_path(@project1)
|
||||||
within 'form#new-target-form' do
|
click_link t('targets.contextual.link_new_target')
|
||||||
assert has_select?(t(:field_goal), selected: t('targets.form.binding_goal'))
|
within 'form#new-target-form' do
|
||||||
assert has_field?(t(:field_effective_from), with: Date.current.strftime)
|
assert has_select?(t(:field_goal), selected: t('targets.form.binding_goal'))
|
||||||
within 'p.target' do
|
assert has_field?(t(:field_effective_from), with: Date.current.strftime)
|
||||||
select quantities(:quantities_energy).name
|
within 'p.target' do
|
||||||
select '=='
|
select quantities(:quantities_energy).name
|
||||||
fill_in with: '1750'
|
select '=='
|
||||||
select units(:units_kcal).shortname
|
fill_in with: '1750'
|
||||||
end
|
select units(:units_kcal).shortname
|
||||||
assert_difference 'Target.count' => 1, '@project1.targets.reload.count' => 1,
|
end
|
||||||
'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?)
|
||||||
visit project_targets_path(@project1)
|
assert_difference ['Goal.count', '@project1.goals.reload.count(&:is_binding?)',
|
||||||
click_link t('targets.contextual.link_new_target')
|
'@project1.targets.reload.count'], 1 do
|
||||||
within 'form#new-target-form' do
|
visit project_targets_path(@project1)
|
||||||
# Assume binding Goal is initialized (not saved) and selected by default
|
click_link t('targets.contextual.link_new_target')
|
||||||
within 'p.target' do
|
within 'form#new-target-form' do
|
||||||
select quantities(:quantities_energy).name
|
# Assume binding Goal is selected by default
|
||||||
select '=='
|
within 'p.target' do
|
||||||
fill_in with: '1750'
|
select quantities(:quantities_energy).name
|
||||||
select units(:units_kcal).shortname
|
select '=='
|
||||||
end
|
fill_in with: '1750'
|
||||||
assert_difference ['Goal.count', '@project1.goals.reload.count(&:is_binding?)'], 1 do
|
select units(:units_kcal).shortname
|
||||||
|
end
|
||||||
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
|
||||||
|
Reference in New Issue
Block a user