diff --git a/app/controllers/targets_controller.rb b/app/controllers/targets_controller.rb index 0b8d2d7..3ae4f29 100644 --- a/app/controllers/targets_controller.rb +++ b/app/controllers/targets_controller.rb @@ -6,7 +6,7 @@ class TargetsController < ApplicationController include Concerns::Finders before_action :find_goal_by_goal_id, - only: [:index, :new, :create, :edit, :update, :toggle_exposure] + only: [:index, :new, :create, :edit, :update, :destroy, :toggle_exposure] before_action :find_quantity_by_quantity_id, only: [:toggle_exposure, :subthresholds] before_action :authorize #before_action :set_view_params @@ -54,17 +54,27 @@ class TargetsController < ApplicationController params[:goal][:targets_attributes].each { |ta| ta[:effective_from] = @effective_from } if @goal.update(targets_params) - flash.now[:notice] = t('.success') + count = @goal.targets.target.count { |t| t.previous_changes.present? } + flash.now[:notice] = t('.success', count: count) prepare_targets + render :index else @targets = @goal.targets.where(id: targets_params[:targets_attributes].pluck(:id)) - @targets += @goal.targets.select(&:changed_for_autosave?) + @targets += @goal.targets.target.select(&:changed_for_autosave?) .each { |t| t.thresholds.new unless t.thresholds.present? } render :edit end end def destroy + @effective_from = params[:date] + @targets = @goal.targets.where(effective_from: @effective_from) + count = @targets.destroy_all.length + if @targets.all?(&:destroyed?) + flash.now[:notice] = t('.success', count: count) + else + flash.now[:error] = t('.failure') + end end def reapply diff --git a/app/models/validations/nested_uniqueness.rb b/app/models/validations/nested_uniqueness.rb index dfa1cc2..2311f96 100644 --- a/app/models/validations/nested_uniqueness.rb +++ b/app/models/validations/nested_uniqueness.rb @@ -63,6 +63,7 @@ module Validations::NestedUniqueness end end if records + # TODO: reset collction, not proxy collection.proxy_association.reset records.each { |r| collection.proxy_association.add_to_target(r) } end diff --git a/app/views/targets/destroy.js.erb b/app/views/targets/destroy.js.erb new file mode 100644 index 0000000..677f752 --- /dev/null +++ b/app/views/targets/destroy.js.erb @@ -0,0 +1,3 @@ +<% if @targets.all?(&:destroyed?) %> + $('tr[id=date-<%= @effective_from %>]').nextUntil('tr.primary').addBack().remove(); +<% end %> diff --git a/config/locales/en.yml b/config/locales/en.yml index eeaac3c..11694a8 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -122,6 +122,15 @@ en: zero: "No targets specified" one: "Created 1 target" other: "Created %{count} targets" + update: + success: + one: "Successfully updated target" + other: "Successfully updated targets" + destroy: + success: + one: "Deleted 1 target" + other: "Deleted %{count} targets" + failure: "Some targets have not been deleted" meals: contextual: link_new_meal: 'New meal' diff --git a/test/system/targets_test.rb b/test/system/targets_test.rb index e39f3fe..61b98d0 100644 --- a/test/system/targets_test.rb +++ b/test/system/targets_test.rb @@ -13,15 +13,22 @@ class TargetsTest < BodyTrackingSystemTestCase def test_index_binding_goal goal = @project.goals.binding assert_not_equal 0, goal.targets.count + visit goal_targets_path(goal) - assert_selector 'table#targets tbody tr', - count: goal.targets.distinct.pluck(:effective_from).count + within 'table#targets tbody' do + dates = goal.targets.distinct.pluck(:effective_from) + assert_selector 'tr', count: dates.count + dates.each do |date| + assert_selector 'td', text: date + end + end end def test_index_binding_goal_without_targets goal = @project.goals.binding goal.targets.delete_all assert_equal 0, goal.targets.count + visit goal_targets_path(goal) assert_current_path goal_targets_path(goal) assert_selector 'div#targets', visible: :yes, exact_text: t(:label_no_data) @@ -58,6 +65,10 @@ class TargetsTest < BodyTrackingSystemTestCase assert_selector 'table#targets thead th' end + def test_show + # TODO + end + def test_new_binding_target visit goal_targets_path(@project.goals.binding) assert_no_selector 'form#new-target-form' @@ -112,8 +123,11 @@ class TargetsTest < BodyTrackingSystemTestCase assert_equal threshold_unit, t.thresholds.first.unit assert_no_selector 'form#new-target-form' - assert_selector 'table#targets tbody tr', - count: goal.targets.distinct.pluck(:effective_from).count + assert_selector 'div.flash.notice' + within 'table#targets tbody' do + assert_selector 'tr', count: goal.targets.distinct.pluck(:effective_from).count + assert_selector 'td', text: date + end end def test_create_binding_target_when_binding_goal_does_not_exist @@ -336,9 +350,9 @@ class TargetsTest < BodyTrackingSystemTestCase end click_on t(:button_save) - assert_no_selector 'div#errorExplanation' end end + assert_no_selector 'div#errorExplanation' target.reload assert_equal date, target.effective_from @@ -350,6 +364,12 @@ class TargetsTest < BodyTrackingSystemTestCase assert_equal t_value, target.thresholds[index].value assert_equal t_unit, target.thresholds[index].unit end + + assert_selector 'div.flash.notice' + within 'table#targets tbody' do + assert_no_selector 'td', text: target_date + assert_selector 'td', text: date + end end def test_update_swap_targets @@ -373,9 +393,9 @@ class TargetsTest < BodyTrackingSystemTestCase select2.select quantity1 click_on t(:button_save) - assert_no_selector 'div#errorExplanation' end end + assert_no_selector 'div#errorExplanation' target1.reload target2.reload @@ -405,15 +425,38 @@ class TargetsTest < BodyTrackingSystemTestCase end click_on t(:button_save) - assert_no_selector 'div#errorExplanation' end end + assert_no_selector 'div#errorExplanation' new_target = Target.last assert new_target.quantity, target.quantity assert_raises(ActiveRecord::RecordNotFound) { target.reload } end + def test_destroy + goal = @project.goals.binding + date = goal.targets.distinct.pluck(:effective_from).sample + targets = goal.targets.where(effective_from: date) + + visit goal_targets_path(goal) + assert_difference 'Goal.count' => 0, 'Target.count' => - targets.length, + 'Threshold.count' => - targets.sum { |t| t.thresholds.length } do + find('td', text: date).ancestor('tr').click_link t(:button_delete) + end + assert_empty goal.targets.reload.where(effective_from: date) + + assert_selector 'div.flash.notice' + within 'table#targets tbody' do + assert_selector 'tr', count: goal.targets.distinct.pluck(:effective_from).count + assert_no_selector 'td', text: date + end + end + + def test_reapply + # TODO + end + def fill_thresholds(thresholds) thresholds.each do |threshold| t_quantity, t_value, t_unit = threshold