forked from maybe-finance/maybe
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add loan and credit card views (maybe-finance#1268)
* Add loan and credit card views * Lint fix * Clean up overview card markup * Lint fix * Test fix
- Loading branch information
Showing
34 changed files
with
564 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
class CreditCardsController < ApplicationController | ||
before_action :set_account, only: :update | ||
|
||
def create | ||
account = Current.family | ||
.accounts | ||
.create_with_optional_start_balance! \ | ||
attributes: account_params.except(:start_date, :start_balance), | ||
start_date: account_params[:start_date], | ||
start_balance: account_params[:start_balance] | ||
|
||
account.sync_later | ||
redirect_to account, notice: t(".success") | ||
end | ||
|
||
def update | ||
@account.update_with_sync!(account_params) | ||
redirect_to @account, notice: t(".success") | ||
end | ||
|
||
private | ||
|
||
def set_account | ||
@account = Current.family.accounts.find(params[:id]) | ||
end | ||
|
||
def account_params | ||
params.require(:account) | ||
.permit( | ||
:name, :balance, :start_date, :start_balance, :currency, :accountable_type, | ||
accountable_attributes: [ | ||
:id, | ||
:available_credit, | ||
:minimum_payment, | ||
:apr, | ||
:annual_fee, | ||
:expiration_date | ||
] | ||
) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
class LoansController < ApplicationController | ||
before_action :set_account, only: :update | ||
|
||
def create | ||
account = Current.family | ||
.accounts | ||
.create_with_optional_start_balance! \ | ||
attributes: account_params.except(:start_date, :start_balance), | ||
start_date: account_params[:start_date], | ||
start_balance: account_params[:start_balance] | ||
|
||
account.sync_later | ||
redirect_to account, notice: t(".success") | ||
end | ||
|
||
def update | ||
@account.update_with_sync!(account_params) | ||
redirect_to @account, notice: t(".success") | ||
end | ||
|
||
private | ||
|
||
def set_account | ||
@account = Current.family.accounts.find(params[:id]) | ||
end | ||
|
||
def account_params | ||
params.require(:account) | ||
.permit( | ||
:name, :balance, :start_date, :start_balance, :currency, :accountable_type, | ||
accountable_attributes: [ | ||
:id, | ||
:rate_type, | ||
:interest_rate, | ||
:term_months | ||
] | ||
) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,15 @@ | ||
class Loan < ApplicationRecord | ||
include Accountable | ||
|
||
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.original_balance.zero? || term_months.zero? | ||
|
||
annual_rate = interest_rate / 100.0 | ||
monthly_rate = annual_rate / 12.0 | ||
|
||
payment = (account.original_balance * monthly_rate * (1 + monthly_rate)**term_months) / ((1 + monthly_rate)**term_months - 1) | ||
|
||
Money.new(payment.round, account.currency) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
<%# locals: (account:) %> | ||
|
||
<%= render partial: "accounts/accountables/#{account.accountable_type.downcase}/overview", locals: { account: account } %> | ||
<%= render partial: "accounts/accountables/#{account.accountable_type.underscore}/overview", locals: { account: account } %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<%# locals: (title:, content:) %> | ||
|
||
<div class="rounded-xl bg-white shadow-xs border border-alpha-black-25 p-4"> | ||
<h4 class="text-gray-500 text-sm"><%= title %></h4> | ||
<p class="text-xl font-medium text-gray-900"> | ||
<%= content %> | ||
</p> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<div> | ||
<hr class="my-4"> | ||
|
||
<div class="space-y-2"> | ||
<%= f.fields_for :accountable do |credit_card_form| %> | ||
<div class="flex items-center gap-2"> | ||
<%= credit_card_form.text_field :available_credit, label: t(".available_credit"), placeholder: t(".available_credit_placeholder") %> | ||
</div> | ||
|
||
<div class="flex items-center gap-2"> | ||
<%= credit_card_form.text_field :minimum_payment, label: t(".minimum_payment"), placeholder: t(".minimum_payment_placeholder") %> | ||
<%= credit_card_form.text_field :apr, label: t(".apr"), placeholder: t(".apr_placeholder") %> | ||
</div> | ||
|
||
<div class="flex items-center gap-2"> | ||
<%= credit_card_form.date_field :expiration_date, label: t(".expiration_date") %> | ||
<%= credit_card_form.text_field :annual_fee, label: t(".annual_fee"), placeholder: t(".annual_fee_placeholder") %> | ||
</div> | ||
<% end %> | ||
</div> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<div> | ||
<hr class="my-4"> | ||
|
||
<div class="space-y-2"> | ||
<%= f.fields_for :accountable do |loan_form| %> | ||
<div class="flex items-center gap-2"> | ||
<%= loan_form.text_field :interest_rate, label: t(".interest_rate"), placeholder: t(".interest_rate_placeholder") %> | ||
<%= loan_form.select :rate_type, options_for_select([["Fixed", "fixed"], ["Variable", "variable"], ["Adjustable", "adjustable"]]), { label: t(".rate_type") } %> | ||
</div> | ||
|
||
<div class="flex items-center gap-2"> | ||
<%= loan_form.number_field :term_months, label: t(".term_months"), placeholder: t(".term_months_placeholder") %> | ||
</div> | ||
<% end %> | ||
</div> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
app/views/accounts/accountables/credit_card/_overview.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<%# locals: (account:) %> | ||
|
||
<div class="grid grid-cols-3 gap-2"> | ||
<%= summary_card title: t(".amount_owed") do %> | ||
<%= format_money(account.balance) %> | ||
<% end %> | ||
|
||
<%= summary_card title: t(".available_credit") do %> | ||
<%= format_money(account.credit_card.available_credit) || t(".unknown") %> | ||
<% end %> | ||
|
||
<%= summary_card title: t(".minimum_payment") do %> | ||
<%= format_money(account.credit_card.minimum_payment) || t(".unknown") %> | ||
<% end %> | ||
|
||
<%= summary_card title: t(".apr") do %> | ||
<%= account.credit_card.apr ? number_to_percentage(account.credit_card.apr, precision: 2) : t(".unknown") %> | ||
<% end %> | ||
|
||
<%= summary_card title: t(".expiration_date") do %> | ||
<%= account.credit_card.expiration_date ? l(account.credit_card.expiration_date, format: :long) : t(".unknown") %> | ||
<% end %> | ||
|
||
<%= summary_card title: t(".annual_fee") do %> | ||
<%= format_money(account.credit_card.annual_fee) || t(".unknown") %> | ||
<% end %> | ||
</div> |
Oops, something went wrong.