diff --git a/app/controllers/concerns/finders.rb b/app/controllers/concerns/finders.rb index 2cc9732..a53efc9 100644 --- a/app/controllers/concerns/finders.rb +++ b/app/controllers/concerns/finders.rb @@ -1,6 +1,13 @@ module Concerns::Finders private + def find_goal(id = params[:id]) + @goal = Goal.find(id) + @project = @goal.project + rescue ActiveRecord::RecordNotFound + render_404 + end + def find_meal @meal = Meal.find(params[:id]) @project = @meal.project diff --git a/app/controllers/targets_controller.rb b/app/controllers/targets_controller.rb index f83e0e2..95e0194 100644 --- a/app/controllers/targets_controller.rb +++ b/app/controllers/targets_controller.rb @@ -22,9 +22,9 @@ class TargetsController < ApplicationController end def create - goal = @project.goals.binding if params[:goal][:id].blank? - goal ||= @project.goals.find_by(id: params[:goal][:id]) - goal ||= @project.goals.build(goal_params) + goal_id = params[:goal][:id] + goal = goal_id.present? ? @project.goals.find(goal_id) : @project.goals.new + goal.attributes = goal_params unless goal.is_binding? @targets = goal.targets.build(targets_params[:targets]) do |target| target.effective_from = params[:target][:effective_from] @@ -70,7 +70,7 @@ class TargetsController < ApplicationController private def goal_params - params.require(:goal).permit(:id, :name, :description) + params.require(:goal).permit(:name, :description) end def targets_params diff --git a/app/models/goal.rb b/app/models/goal.rb index 2078692..f888f3b 100644 --- a/app/models/goal.rb +++ b/app/models/goal.rb @@ -6,7 +6,7 @@ class Goal < ActiveRecord::Base class_name: 'Exposure', extend: BodyTracking::TogglableExposures 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 :name, presence: true, uniqueness: {scope: :project_id}, 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? end end + before_destroy prepend: true do + !is_binding? + end end diff --git a/lib/body_tracking/project_patch.rb b/lib/body_tracking/project_patch.rb index 9225fa2..c7ddd72 100644 --- a/lib/body_tracking/project_patch.rb +++ b/lib/body_tracking/project_patch.rb @@ -31,7 +31,7 @@ module BodyTracking::ProjectPatch has_many :goals, dependent: :destroy do 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') end end diff --git a/test/fixtures/goals.yml b/test/fixtures/goals.yml index 635ae23..2fc82e1 100644 --- a/test/fixtures/goals.yml +++ b/test/fixtures/goals.yml @@ -2,3 +2,7 @@ goals_binding: project_id: 1 is_binding: true name: "<%= I18n.t 'targets.form.binding_goal' %>" +goals_non_binding: + project_id: 1 + is_binding: false + name: "Non-binding goal" diff --git a/test/system/targets_test.rb b/test/system/targets_test.rb index 6bcf05d..ac7b1b0 100644 --- a/test/system/targets_test.rb +++ b/test/system/targets_test.rb @@ -23,7 +23,7 @@ class TargetsTest < BodyTrackingSystemTestCase assert_selector 'div#targets', visible: :yes, exact_text: t(:label_no_data) end - def test_index_shows_and_hides_new_target_form + def test_index_show_and_hide_new_target_form visit project_targets_path(@project1) assert_no_selector 'form#new-target-form' click_link t('targets.contextual.link_new_target') @@ -33,23 +33,23 @@ class TargetsTest < BodyTrackingSystemTestCase end def test_create_binding_target - assert @project1.goals.exists?(is_binding: true) - visit project_targets_path(@project1) - click_link t('targets.contextual.link_new_target') - within 'form#new-target-form' do - assert has_select?(t(:field_goal), selected: t('targets.form.binding_goal')) - assert has_field?(t(:field_effective_from), with: Date.current.strftime) - within 'p.target' do - select quantities(:quantities_energy).name - select '==' - fill_in with: '1750' - select units(:units_kcal).shortname - end - assert_difference 'Target.count' => 1, '@project1.targets.reload.count' => 1, - 'Threshold.count' => 1, 'Goal.count' => 0 do + assert_difference 'Goal.count' => 0, 'Target.count' => 1, + '@project1.targets.reload.count' => 1, 'Threshold.count' => 1 do + visit project_targets_path(@project1) + click_link t('targets.contextual.link_new_target') + within 'form#new-target-form' do + assert has_select?(t(:field_goal), selected: t('targets.form.binding_goal')) + assert has_field?(t(:field_effective_from), with: Date.current.strftime) + within 'p.target' do + select quantities(:quantities_energy).name + select '==' + fill_in with: '1750' + select units(:units_kcal).shortname + end click_on t(:button_create) end end + assert_equal @project1.goals.binding, Target.last.goal assert_no_selector 'form#new-target-form' assert_selector 'table#targets tbody tr', count: @project1.targets.count end @@ -57,20 +57,24 @@ class TargetsTest < BodyTrackingSystemTestCase def test_create_binding_target_when_binding_goal_does_not_exist @project1.goals.where(is_binding: true).delete_all assert_equal 0, @project1.goals.count(&:is_binding?) - 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 + assert_difference ['Goal.count', '@project1.goals.reload.count(&:is_binding?)', + '@project1.targets.reload.count'], 1 do + visit project_targets_path(@project1) + click_link t('targets.contextual.link_new_target') + within 'form#new-target-form' do + # Assume binding Goal is selected by default + within 'p.target' do + select quantities(:quantities_energy).name + select '==' + fill_in with: '1750' + select units(:units_kcal).shortname + end click_on t(:button_create) end end - assert_selector 'table#targets tbody tr', count: 1 + assert_equal @project1.goals.binding, Target.last.goal + end + + def test_edit end end