The personal finance app for everyone (by everyone)
Ruby
Leave trades out of the Reports breakdown when filtering by category (#5025)
arnaurodondev14a11a44415e
diff --git a/app/controllers/reports_controller.rb b/app/controllers/reports_controller.rbindex cd63bcb285..7f60dce175 100644--- a/app/controllers/reports_controller.rb+++ b/app/controllers/reports_controller.rb@@ -794,7 +794,8 @@ class ReportsController < ApplicationController# Scope to family's categories to prevent cross-family data accesssubcategory_ids = Current.family.categories.where(parent_id: category_id).pluck(:id)all_category_ids = [ category_id ] + subcategory_ids- scope = scope.where(category_id: all_category_ids)+ # Trades have no category, so a category filter leaves them out.+ scope = scope.model == Trade ? scope.none : scope.where(category_id: all_category_ids)end# Filter by accountdiff --git a/test/controllers/reports_controller_test.rb b/test/controllers/reports_controller_test.rbindex 93308ed5b4..47cfc6121e 100644--- a/test/controllers/reports_controller_test.rb+++ b/test/controllers/reports_controller_test.rb@@ -980,6 +980,18 @@ class ReportsControllerTest < ActionDispatch::IntegrationTestassert_select "a[href=?]", reports_path(period_type: :ytd, start_date: prev_start, end_date: prev_end)end+ test "the category filter lists the category's transactions and leaves trades out" do+ category = @family.categories.create!(name: "Filtered", color: "#123456")+ create_transaction(account: accounts(:depository), name: "Filtered spend", amount: 42, category: category)+ create_trade(securities(:aapl), account: accounts(:investment), qty: 1, date: Date.current, price: 100)++ get reports_path(period_type: :monthly, filter_category_id: category.id)++ assert_response :ok+ assert_select "tr[data-category='category-#{category.id}']", text: /\$42\.00/+ assert_select "tr[data-category='category-other_investments']", count: 0+ end+test "ytd period navigation disables next arrow on current year" doget reports_path(period_type: :ytd)assert_response :ok