The personal finance app for everyone (by everyone)
Ruby
Refactor `Investment Contributions` category handling to use a centralized constant and streamline related logic.
luckyPipewrench4727a391a746
diff --git a/app/components/UI/account/activity_feed.html.erb b/app/components/UI/account/activity_feed.html.erbindex 38c179552d..fe5041f13e 100644--- a/app/components/UI/account/activity_feed.html.erb+++ b/app/components/UI/account/activity_feed.html.erb@@ -37,7 +37,7 @@variant: "link",text: t("accounts.show.activity.new_transfer"),icon: "arrow-right-left",- href: new_transfer_path,+ href: new_transfer_path(from_account_id: account.id),data: { turbo_frame: :modal }) %><% end %></div>diff --git a/app/controllers/transfers_controller.rb b/app/controllers/transfers_controller.rbindex 7342414bb3..db43435845 100644--- a/app/controllers/transfers_controller.rb+++ b/app/controllers/transfers_controller.rb@@ -5,6 +5,7 @@ class TransfersController < ApplicationControllerdef new@transfer = Transfer.new+ @from_account_id = params[:from_account_id]enddef showdiff --git a/app/models/account/provider_import_adapter.rb b/app/models/account/provider_import_adapter.rbindex 3c28db52a8..28bf61cf90 100644--- a/app/models/account/provider_import_adapter.rb+++ b/app/models/account/provider_import_adapter.rb@@ -155,7 +155,7 @@ class Account::ProviderImportAdapterauto_kind = "funds_movement"elsif detected_label == "Contribution"auto_kind = "investment_contribution"- auto_category = account.family.categories.find_by(name: "Investment Contributions")+ auto_category = account.family.categories.find_by(name: Category::DEFAULT_INVESTMENT_CONTRIBUTIONS_NAME)end# Set investment activity label, kind, and category if detecteddiff --git a/app/models/category.rb b/app/models/category.rbindex b6586492e9..3dfb8a8093 100644--- a/app/models/category.rb+++ b/app/models/category.rb@@ -39,6 +39,10 @@ class Category < ApplicationRecordUNCATEGORIZED_NAME_KEY = "models.category.uncategorized"OTHER_INVESTMENTS_NAME_KEY = "models.category.other_investments"+ # Default category name for investment contributions (bootstrapped category)+ # This is the canonical English name used when seeding categories via bootstrap!+ DEFAULT_INVESTMENT_CONTRIBUTIONS_NAME = "Investment Contributions"+class Groupattr_reader :category, :subcategoriesdiff --git a/app/models/income_statement/totals.rb b/app/models/income_statement/totals.rbindex 1de3e081c0..7bb9f67dc8 100644--- a/app/models/income_statement/totals.rb+++ b/app/models/income_statement/totals.rb@@ -57,7 +57,7 @@ class IncomeStatement::Totalsc.id as category_id,c.parent_id as parent_category_id,CASE WHEN at.kind = 'investment_contribution' THEN 'expense' WHEN ae.amount < 0 THEN 'income' ELSE 'expense' END as classification,- ABS(SUM(ae.amount * COALESCE(er.rate, 1))) as total,+ ABS(SUM(CASE WHEN at.kind = 'investment_contribution' THEN ABS(ae.amount * COALESCE(er.rate, 1)) ELSE ae.amount * COALESCE(er.rate, 1) END)) as total,COUNT(ae.id) as transactions_count,false as is_uncategorized_investmentFROM (#{@transactions_scope.to_sql}) at@@ -83,7 +83,7 @@ class IncomeStatement::Totalsc.id as category_id,c.parent_id as parent_category_id,CASE WHEN at.kind = 'investment_contribution' THEN 'expense' WHEN ae.amount < 0 THEN 'income' ELSE 'expense' END as classification,- ABS(SUM(ae.amount * COALESCE(er.rate, 1))) as total,+ ABS(SUM(CASE WHEN at.kind = 'investment_contribution' THEN ABS(ae.amount * COALESCE(er.rate, 1)) ELSE ae.amount * COALESCE(er.rate, 1) END)) as total,COUNT(ae.id) as entry_count,false as is_uncategorized_investmentFROM (#{@transactions_scope.to_sql}) atdiff --git a/app/models/transfer/creator.rb b/app/models/transfer/creator.rbindex 61bbadeebd..70b143b24b 100644--- a/app/models/transfer/creator.rb+++ b/app/models/transfer/creator.rb@@ -43,7 +43,7 @@ class Transfer::Creatorenddef investment_contributions_category- source_account.family.categories.find_by(name: "Investment Contributions")+ source_account.family.categories.find_by(name: Category::DEFAULT_INVESTMENT_CONTRIBUTIONS_NAME)enddef inflow_transactiondiff --git a/app/views/transfers/_form.html.erb b/app/views/transfers/_form.html.erbindex b32e597075..4158303e42 100644--- a/app/views/transfers/_form.html.erb+++ b/app/views/transfers/_form.html.erb@@ -11,7 +11,7 @@</section><section class="space-y-2">- <%= f.collection_select :from_account_id, Current.family.accounts.alphabetically, :id, :name, { prompt: t(".select_account"), label: t(".from") }, required: true %>+ <%= f.collection_select :from_account_id, Current.family.accounts.alphabetically, :id, :name, { prompt: t(".select_account"), label: t(".from"), selected: @from_account_id }, required: true %><%= f.collection_select :to_account_id, Current.family.accounts.alphabetically, :id, :name, { prompt: t(".select_account"), label: t(".to") }, required: true %><%= f.number_field :amount, label: t(".amount"), required: true, min: 0, placeholder: "100", step: 0.00000001 %><%= f.date_field :date, value: transfer.inflow_transaction&.entry&.date || Date.current, label: t(".date"), required: true, max: Date.current %>diff --git a/test/models/transfer/creator_test.rb b/test/models/transfer/creator_test.rbindex ff168509f6..4f1a2a1630 100644--- a/test/models/transfer/creator_test.rb+++ b/test/models/transfer/creator_test.rb@@ -7,16 +7,11 @@ class Transfer::CreatorTest < ActiveSupport::TestCase@destination_account = accounts(:investment)@date = Date.current@amount = 100+ # Ensure the Investment Contributions category exists for transfer tests+ @investment_category = ensure_investment_contributions_category(@family)endtest "creates investment contribution when transferring from depository to investment" do- # Ensure the Investment Contributions category exists with all required attributes- investment_category = @family.categories.find_or_create_by!(name: "Investment Contributions") do |c|- c.color = "#0d9488"- c.lucide_icon = "trending-up"- c.classification = "expense"- end-creator = Transfer::Creator.new(family: @family,source_account_id: @source_account.id,@@ -36,7 +31,7 @@ class Transfer::CreatorTest < ActiveSupport::TestCaseassert_equal @amount, outflow.entry.amountassert_equal @source_account.currency, outflow.entry.currencyassert_equal "Transfer to #{@destination_account.name}", outflow.entry.name- assert_equal investment_category, outflow.category, "Should auto-assign Investment Contributions category"+ assert_equal @investment_category, outflow.category, "Should auto-assign Investment Contributions category"# Verify inflow transaction (always funds_movement)inflow = transfer.inflow_transaction@@ -75,13 +70,7 @@ class Transfer::CreatorTest < ActiveSupport::TestCaseendtest "creates investment contribution when transferring from depository to crypto" do- # Use crypto account - transfer from depository should be investment_contributioncrypto_account = accounts(:crypto)- investment_category = @family.categories.find_or_create_by!(name: "Investment Contributions") do |c|- c.color = "#0d9488"- c.lucide_icon = "trending-up"- c.classification = "expense"- endcreator = Transfer::Creator.new(family: @family,@@ -99,7 +88,7 @@ class Transfer::CreatorTest < ActiveSupport::TestCaseoutflow = transfer.outflow_transactionassert_equal "investment_contribution", outflow.kindassert_equal "Transfer to #{crypto_account.name}", outflow.entry.name- assert_equal investment_category, outflow.category+ assert_equal @investment_category, outflow.category# Verify inflow transaction with currency handlinginflow = transfer.inflow_transactiondiff --git a/test/test_helper.rb b/test/test_helper.rbindex 16bab4495d..b44fcdeb33 100644--- a/test/test_helper.rb+++ b/test/test_helper.rb@@ -78,6 +78,16 @@ module ActiveSupportdef user_password_test"maybetestpassword817983172"end++ # Ensures the Investment Contributions category exists for a family+ # Used in transfer tests where this bootstrapped category is required+ def ensure_investment_contributions_category(family)+ family.categories.find_or_create_by!(name: Category::DEFAULT_INVESTMENT_CONTRIBUTIONS_NAME) do |c|+ c.color = "#0d9488"+ c.lucide_icon = "trending-up"+ c.classification = "expense"+ end+ endendend