The personal finance app for everyone (by everyone)
Ruby
resloved issues raised by ai chatbot
Shibu M7e6d1123dc2b
diff --git a/app/models/transfer.rb b/app/models/transfer.rbindex 7d5c449470..3b60c1cfb9 100644--- a/app/models/transfer.rb+++ b/app/models/transfer.rb@@ -11,6 +11,7 @@ class Transfer < ApplicationRecordvalidate :transfer_has_opposite_amounts_or_feesvalidate :transfer_within_date_rangevalidate :transfer_has_same_family+ validate :fees_must_be_non_negativeclass << selfdef kind_for_account(account)@@ -148,14 +149,19 @@ class Transfer < ApplicationRecordinflow_amount = inflow_entry.amountoutflow_amount = outflow_entry.amount+ errors.add(:base, :opposite_amounts) unless inflow_amount.negative? && outflow_amount.positive?+if inflow_entry.currency == outflow_entry.currencytotal_fee = source_fee_amount.to_d + destination_fee_amount.to_derrors.add(:base, :opposite_amounts) if inflow_amount + outflow_amount != total_fee- else- errors.add(:base, :opposite_amounts) unless inflow_amount.negative? && outflow_amount.positive?endend+ def fees_must_be_non_negative+ errors.add(:source_fee_amount, :greater_than_or_equal_to, count: 0) if source_fee_amount.to_d.negative?+ errors.add(:destination_fee_amount, :greater_than_or_equal_to, count: 0) if destination_fee_amount.to_d.negative?+ end+def transfer_within_date_rangereturn unless inflow_transaction&.entry && outflow_transaction&.entrydiff --git a/app/views/transfers/show.html.erb b/app/views/transfers/show.html.erbindex 6b4a82afda..5b713b54a9 100644--- a/app/views/transfers/show.html.erb+++ b/app/views/transfers/show.html.erb@@ -35,13 +35,13 @@</dl><dl class="flex items-center gap-2 justify-between"><dt class="text-secondary"><%= t(".amount") %></dt>- <dd class="font-medium text-red-500 privacy-sensitive"><%= format_money @transfer.outflow_transaction.entry.amount_money * -1 %></dd>+ <dd class="font-medium text-destructive privacy-sensitive"><%= format_money @transfer.outflow_transaction.entry.amount_money * -1 %></dd></dl></div><% if @transfer.has_source_fee? %><dl class="flex items-center gap-2 justify-between"><dt class="text-secondary"><%= t(".source_fee") %></dt>- <dd class="font-medium text-red-500 privacy-sensitive">+ <dd class="font-medium text-destructive privacy-sensitive">-<%= format_money Money.new(@transfer.source_fee_amount, @transfer.from_account.currency) %></dd></dl>@@ -50,7 +50,7 @@<% if @transfer.has_destination_fee? %><dl class="flex items-center gap-2 justify-between"><dt class="text-secondary"><%= t(".destination_fee") %></dt>- <dd class="font-medium text-red-500 privacy-sensitive">+ <dd class="font-medium text-destructive privacy-sensitive">-<%= format_money Money.new(@transfer.destination_fee_amount, @transfer.to_account.currency) %></dd></dl>@@ -71,7 +71,7 @@</dl><dl class="flex items-center gap-2 justify-between"><dt class="text-secondary"><%= t(".amount") %></dt>- <dd class="font-medium text-green-500 privacy-sensitive">+<%= format_money @transfer.inflow_transaction.entry.amount_money * -1 %></dd>+ <dd class="font-medium text-success privacy-sensitive">+<%= format_money @transfer.inflow_transaction.entry.amount_money * -1 %></dd></dl></div></div>@@ -115,7 +115,7 @@<%= button_to t(".delete"),transfer_path(@transfer),method: :delete,- class: "rounded-lg px-3 py-2 whitespace-nowrap text-red-500 text-sm+ class: "rounded-lg px-3 py-2 whitespace-nowrap text-destructive text-smfont-medium border border-secondary",data: { turbo_confirm: true, turbo_frame: "_top" } %></div>diff --git a/test/models/transfer_test.rb b/test/models/transfer_test.rbindex f592930964..f2abc4df9c 100644--- a/test/models/transfer_test.rb+++ b/test/models/transfer_test.rb@@ -203,4 +203,47 @@ class TransferTest < ActiveSupport::TestCasetransfer.update!(source_fee_amount: 3, destination_fee_amount: 2)assert_equal 5, transfer.total_feeend++ test "destination fee larger than amount inverts inflow sign and fails validation" do+ outflow_entry = create_transaction(date: Date.current, account: accounts(:depository), amount: 100)+ inflow_entry = create_transaction(date: Date.current, account: accounts(:credit_card), amount: 50)++ transfer = Transfer.new(+ inflow_transaction: inflow_entry.transaction,+ outflow_transaction: outflow_entry.transaction,+ destination_fee_amount: 150+ )++ # inflow amount (50) is positive, which means destination is also outflowing+ assert transfer.invalid?+ assert_equal "Must have opposite amounts", transfer.errors.full_messages.first+ end++ test "negative source fee is rejected" do+ outflow_entry = create_transaction(date: Date.current, account: accounts(:depository), amount: 500)+ inflow_entry = create_transaction(date: Date.current, account: accounts(:credit_card), amount: -500)++ transfer = Transfer.new(+ inflow_transaction: inflow_entry.transaction,+ outflow_transaction: outflow_entry.transaction,+ source_fee_amount: -5+ )++ assert transfer.invalid?+ assert_equal [ "Source fee amount must be greater than or equal to 0" ], transfer.errors.full_messages+ end++ test "negative destination fee is rejected" do+ outflow_entry = create_transaction(date: Date.current, account: accounts(:depository), amount: 500)+ inflow_entry = create_transaction(date: Date.current, account: accounts(:credit_card), amount: -500)++ transfer = Transfer.new(+ inflow_transaction: inflow_entry.transaction,+ outflow_transaction: outflow_entry.transaction,+ destination_fee_amount: -5+ )++ assert transfer.invalid?+ assert_equal [ "Destination fee amount must be greater than or equal to 0" ], transfer.errors.full_messages+ endend