Validate loan subtype values

Agent-Logs-Url: https://github.com/we-promise/sure/sessions/54bc6874-2cc0-43aa-ac44-9acd50316be3

Co-authored-by: jjmata <187772+jjmata@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot]
2026-04-17 21:47:14 +00:00
committed by GitHub
parent 00ed231c51
commit 988682e721
3 changed files with 14 additions and 0 deletions

View File

@@ -8,6 +8,8 @@ class Loan < ApplicationRecord
"other" => { short: "Other Loan", long: "Other Loan" }
}.freeze
validates :subtype, inclusion: { in: SUBTYPES.keys }, allow_blank: true
def monthly_payment
return nil if term_months.nil? || interest_rate.nil? || rate_type.nil? || rate_type != "fixed"
return Money.new(0, account.currency) if account.loan.original_balance.amount.zero? || term_months.zero?

View File

@@ -23,6 +23,7 @@ class LoansControllerTest < ActionDispatch::IntegrationTest
notes: "Mortgage notes",
accountable_type: "Loan",
accountable_attributes: {
subtype: "mortgage",
interest_rate: 5.5,
term_months: 60,
rate_type: "fixed",
@@ -40,6 +41,7 @@ class LoansControllerTest < ActionDispatch::IntegrationTest
assert_equal "Local Bank", created_account[:institution_name]
assert_equal "localbank.example", created_account[:institution_domain]
assert_equal "Mortgage notes", created_account[:notes]
assert_equal "mortgage", created_account.accountable.subtype
assert_equal 5.5, created_account.accountable.interest_rate
assert_equal 60, created_account.accountable.term_months
assert_equal "fixed", created_account.accountable.rate_type
@@ -63,6 +65,7 @@ class LoansControllerTest < ActionDispatch::IntegrationTest
accountable_type: "Loan",
accountable_attributes: {
id: @account.accountable_id,
subtype: "auto",
interest_rate: 4.5,
term_months: 48,
rate_type: "fixed",
@@ -79,6 +82,7 @@ class LoansControllerTest < ActionDispatch::IntegrationTest
assert_equal "Updated Bank", @account[:institution_name]
assert_equal "updatedbank.example", @account[:institution_domain]
assert_equal "Updated loan notes", @account[:notes]
assert_equal "auto", @account.accountable.subtype
assert_equal 4.5, @account.accountable.interest_rate
assert_equal 48, @account.accountable.term_months
assert_equal "fixed", @account.accountable.rate_type

View File

@@ -1,6 +1,13 @@
require "test_helper"
class LoanTest < ActiveSupport::TestCase
test "rejects invalid subtype" do
loan = Loan.new(subtype: "invalid")
assert_not loan.valid?
assert_includes loan.errors[:subtype], "is not included in the list"
end
test "calculates correct monthly payment for fixed rate loan" do
loan_account = Account.create! \
family: families(:dylan_family),
@@ -8,6 +15,7 @@ class LoanTest < ActiveSupport::TestCase
balance: 500000,
currency: "USD",
accountable: Loan.create!(
subtype: "mortgage",
interest_rate: 3.5,
term_months: 360,
rate_type: "fixed"