The personal finance app for everyone (by everyone)
Ruby
Fix loan account subtype not persisting on create (#1491)
Sure Admin (bot)ae37c2495f2f
diff --git a/app/controllers/loans_controller.rb b/app/controllers/loans_controller.rbindex 961c5acf0d..e6066f4ade 100644--- a/app/controllers/loans_controller.rb+++ b/app/controllers/loans_controller.rb@@ -2,6 +2,6 @@ class LoansController < ApplicationControllerinclude AccountableResourcepermitted_accountable_attributes(- :id, :rate_type, :interest_rate, :term_months, :initial_balance+ :id, :subtype, :rate_type, :interest_rate, :term_months, :initial_balance)enddiff --git a/app/models/loan.rb b/app/models/loan.rbindex ec56d65b84..980c198852 100644--- a/app/models/loan.rb+++ b/app/models/loan.rb@@ -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_paymentreturn 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?diff --git a/test/controllers/loans_controller_test.rb b/test/controllers/loans_controller_test.rbindex 0949336422..67fff1859d 100644--- a/test/controllers/loans_controller_test.rb+++ b/test/controllers/loans_controller_test.rb@@ -23,6 +23,7 @@ class LoansControllerTest < ActionDispatch::IntegrationTestnotes: "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::IntegrationTestassert_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.subtypeassert_equal 5.5, created_account.accountable.interest_rateassert_equal 60, created_account.accountable.term_monthsassert_equal "fixed", created_account.accountable.rate_type@@ -63,6 +65,7 @@ class LoansControllerTest < ActionDispatch::IntegrationTestaccountable_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::IntegrationTestassert_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.subtypeassert_equal 4.5, @account.accountable.interest_rateassert_equal 48, @account.accountable.term_monthsassert_equal "fixed", @account.accountable.rate_typediff --git a/test/models/loan_test.rb b/test/models/loan_test.rbindex c39679bfbc..05ce9fa05a 100644--- a/test/models/loan_test.rb+++ b/test/models/loan_test.rb@@ -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" doloan_account = Account.create! \family: families(:dylan_family),@@ -8,6 +15,7 @@ class LoanTest < ActiveSupport::TestCasebalance: 500000,currency: "USD",accountable: Loan.create!(+ subtype: "mortgage",interest_rate: 3.5,term_months: 360,rate_type: "fixed"