diff --git a/app/controllers/goal_contributions_controller.rb b/app/controllers/goal_contributions_controller.rb index 6e6226823..da3a0353d 100644 --- a/app/controllers/goal_contributions_controller.rb +++ b/app/controllers/goal_contributions_controller.rb @@ -1,6 +1,7 @@ class GoalContributionsController < ApplicationController before_action :set_goal before_action :set_contribution, only: :destroy + rescue_from ActiveRecord::RecordNotFound, with: :record_not_found def new @contribution = @goal.goal_contributions.new( @@ -55,4 +56,8 @@ class GoalContributionsController < ApplicationController return nil if id.blank? @goal.linked_accounts.find_by(id: id) end + + def record_not_found + redirect_to goals_path, alert: t("goals.errors.not_found") + end end diff --git a/app/controllers/goals_controller.rb b/app/controllers/goals_controller.rb index 6f9c2984c..76461af72 100644 --- a/app/controllers/goals_controller.rb +++ b/app/controllers/goals_controller.rb @@ -1,12 +1,14 @@ class GoalsController < ApplicationController before_action :set_goal, only: %i[show edit update destroy pause resume complete archive unarchive] + rescue_from ActiveRecord::RecordNotFound, with: :goal_not_found STATE_FILTERS = %w[all active paused completed archived].freeze ACTIVE_STATUS_RANK = { behind: 0, on_track: 1, no_target_date: 2 }.freeze def index + state_counts = Current.family.goals.group(:state).count @counts = STATE_FILTERS.each_with_object({}) do |state, h| - h[state] = state == "all" ? Current.family.goals.count : Current.family.goals.where(state: state).count + h[state] = state == "all" ? state_counts.values.sum : (state_counts[state] || 0) end all_goals = Current.family.goals.with_current_balance.alphabetically.includes(:goal_contributions, :linked_accounts).to_a @@ -123,6 +125,10 @@ class GoalsController < ApplicationController .find(params[:id]) end + def goal_not_found + redirect_to goals_path, alert: t("goals.errors.not_found") + end + def goal_params params.require(:goal).permit(:name, :target_amount, :target_date, :color, :notes) end diff --git a/config/locales/views/goals/en.yml b/config/locales/views/goals/en.yml index d0e609538..261a73a14 100644 --- a/config/locales/views/goals/en.yml +++ b/config/locales/views/goals/en.yml @@ -163,6 +163,8 @@ en: monthly_pace: Monthly pace target_of: "target %{amount}/mo" behind_by: "Behind by %{amount}/mo" + errors: + not_found: This goal couldn't be found. It may have been deleted. states: active: Active paused: Paused diff --git a/test/controllers/goals_controller_test.rb b/test/controllers/goals_controller_test.rb index 2f62aeda5..7d905d4af 100644 --- a/test/controllers/goals_controller_test.rb +++ b/test/controllers/goals_controller_test.rb @@ -141,6 +141,7 @@ class GoalsControllerTest < ActionDispatch::IntegrationTest other_goal.save! get goal_url(other_goal) - assert_response :not_found + assert_redirected_to goals_path + assert_equal I18n.t("goals.errors.not_found"), flash[:alert] end end