mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-07-19 05:09:38 +02:00
Add accounts management list (#522)
* Add accounts management * Normalize i18n file * Get turbo streams working * Ignore disabled accounts in calculations * Add empty state
This commit is contained in:
parent
0e77bab00b
commit
ad7136cb63
12 changed files with 164 additions and 25 deletions
|
@ -49,6 +49,10 @@
|
||||||
.form-field__submit {
|
.form-field__submit {
|
||||||
@apply w-full p-3 text-center text-white bg-black rounded-lg hover:bg-gray-700;
|
@apply w-full p-3 text-center text-white bg-black rounded-lg hover:bg-gray-700;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
input:checked + label + .toggle-switch-dot {
|
||||||
|
transform: translateX(100%);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Small, single purpose classes that should take precedence over other styles */
|
/* Small, single purpose classes that should take precedence over other styles */
|
||||||
|
|
|
@ -15,6 +15,30 @@ class AccountsController < ApplicationController
|
||||||
@valuation_series = @account.valuations.to_series(@account)
|
@valuation_series = @account.valuations.to_series(@account)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def edit
|
||||||
|
end
|
||||||
|
|
||||||
|
def update
|
||||||
|
@account = Current.family.accounts.find(params[:id])
|
||||||
|
|
||||||
|
if @account.update(account_params.except(:accountable_type))
|
||||||
|
|
||||||
|
@account.sync_later if account_params[:is_active] == "1"
|
||||||
|
|
||||||
|
respond_to do |format|
|
||||||
|
format.html { redirect_to accounts_path, notice: t(".success") }
|
||||||
|
format.turbo_stream do
|
||||||
|
render turbo_stream: [
|
||||||
|
turbo_stream.append("notification-tray", partial: "shared/notification", locals: { type: "success", content: t(".success") }),
|
||||||
|
turbo_stream.replace("account_#{@account.id}", partial: "accounts/account", locals: { account: @account })
|
||||||
|
]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
else
|
||||||
|
render "edit", status: :unprocessable_entity
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def create
|
def create
|
||||||
@account = Current.family.accounts.build(account_params.except(:accountable_type))
|
@account = Current.family.accounts.build(account_params.except(:accountable_type))
|
||||||
@account.accountable = Accountable.from_type(account_params[:accountable_type])&.new
|
@account.accountable = Accountable.from_type(account_params[:accountable_type])&.new
|
||||||
|
@ -29,6 +53,6 @@ class AccountsController < ApplicationController
|
||||||
private
|
private
|
||||||
|
|
||||||
def account_params
|
def account_params
|
||||||
params.require(:account).permit(:name, :accountable_type, :balance, :currency, :subtype)
|
params.require(:account).permit(:name, :accountable_type, :balance, :currency, :subtype, :is_active)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -7,6 +7,8 @@ class Account < ApplicationRecord
|
||||||
has_many :valuations
|
has_many :valuations
|
||||||
has_many :transactions
|
has_many :transactions
|
||||||
|
|
||||||
|
scope :active, -> { where(is_active: true) }
|
||||||
|
|
||||||
delegated_type :accountable, types: Accountable::TYPES, dependent: :destroy
|
delegated_type :accountable, types: Accountable::TYPES, dependent: :destroy
|
||||||
|
|
||||||
before_create :check_currency
|
before_create :check_currency
|
||||||
|
@ -17,10 +19,15 @@ class Account < ApplicationRecord
|
||||||
Trend.new(current: last.balance, previous: first.balance, type: classification)
|
Trend.new(current: last.balance, previous: first.balance, type: classification)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.by_provider
|
||||||
|
# TODO: When 3rd party providers are supported, dynamically load all providers and their accounts
|
||||||
|
[ { name: "Manual accounts", accounts: all.order(balance: :desc).group_by(&:accountable_type) } ]
|
||||||
|
end
|
||||||
|
|
||||||
# TODO: We will need a better way to encapsulate large queries & transformation logic, but leaving all in one spot until
|
# TODO: We will need a better way to encapsulate large queries & transformation logic, but leaving all in one spot until
|
||||||
# we have a better understanding of the requirements
|
# we have a better understanding of the requirements
|
||||||
def self.by_group(period = Period.all)
|
def self.by_group(period = Period.all)
|
||||||
ranked_balances_cte = joins(:balances)
|
ranked_balances_cte = active.joins(:balances)
|
||||||
.select("
|
.select("
|
||||||
account_balances.account_id,
|
account_balances.account_id,
|
||||||
account_balances.balance,
|
account_balances.balance,
|
||||||
|
|
|
@ -4,15 +4,15 @@ class Family < ApplicationRecord
|
||||||
has_many :transactions, through: :accounts
|
has_many :transactions, through: :accounts
|
||||||
|
|
||||||
def net_worth
|
def net_worth
|
||||||
accounts.sum("CASE WHEN classification = 'asset' THEN balance ELSE -balance END")
|
accounts.active.sum("CASE WHEN classification = 'asset' THEN balance ELSE -balance END")
|
||||||
end
|
end
|
||||||
|
|
||||||
def assets
|
def assets
|
||||||
accounts.where(classification: "asset").sum(:balance)
|
accounts.active.where(classification: "asset").sum(:balance)
|
||||||
end
|
end
|
||||||
|
|
||||||
def liabilities
|
def liabilities
|
||||||
accounts.where(classification: "liability").sum(:balance)
|
accounts.active.where(classification: "liability").sum(:balance)
|
||||||
end
|
end
|
||||||
|
|
||||||
def net_worth_series(period = nil)
|
def net_worth_series(period = nil)
|
||||||
|
|
23
app/views/accounts/_account.html.erb
Normal file
23
app/views/accounts/_account.html.erb
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
<%= turbo_frame_tag dom_id(account) do %>
|
||||||
|
<div class="p-4 flex items-center justify-between gap-3">
|
||||||
|
<div class="flex items-center gap-3">
|
||||||
|
<div class="w-8 h-8 flex items-center justify-center rounded-full text-xs font-medium <%= account.is_active ? "bg-blue-500/10 text-blue-500" : "bg-gray-500/10 text-gray-500" %>">
|
||||||
|
<%= account.name[0].upcase %>
|
||||||
|
</div>
|
||||||
|
<p class="text-sm font-medium <%= account.is_active ? "text-gray-900" : "text-gray-400" %>">
|
||||||
|
<%= account.name %>
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
<div class="flex items-center gap-8">
|
||||||
|
<p class="text-sm font-medium <%= account.is_active ? "text-gray-900" : "text-gray-400" %>">
|
||||||
|
<%= format_currency account.balance %>
|
||||||
|
</p>
|
||||||
|
<%= form_with model: account, method: :patch, html: { class: "flex items-center", data: { turbo_frame: "_top" } } do |form| %>
|
||||||
|
<div class="relative inline-block select-none">
|
||||||
|
<%= form.check_box :is_active, class: "sr-only peer", id: "is_active_#{account.id}", onchange: "this.form.requestSubmit();" %>
|
||||||
|
<label for="is_active_<%= account.id %>" class="block bg-gray-100 w-9 h-5 rounded-full cursor-pointer after:content-[''] after:block after:absolute after:top-0.5 after:left-0.5 after:bg-white after:w-4 after:h-4 after:rounded-full after:transition-transform after:duration-300 after:ease-in-out peer-checked:bg-green-600 peer-checked:after:translate-x-4"></label>
|
||||||
|
</div>
|
||||||
|
<% end %>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<% end%>
|
|
@ -1,17 +1,57 @@
|
||||||
|
<div class="space-y-4">
|
||||||
<h2 class="text-2xl font-semibold font-display"><%= t('.title')%></h2>
|
<div class="flex items-center justify-between">
|
||||||
<h3 class="mt-1 mb-4 text-sm text-gray-500"><%#= number_to_currency Current.family.cash_balance %></h3>
|
<h1 class="text-xl font-medium text-gray-900">Accounts</h1>
|
||||||
|
<%= link_to new_account_path, class: "flex text-white text-sm font-medium items-center gap-1 bg-gray-900 rounded-lg p-2", data: { turbo_frame: "modal" } do %>
|
||||||
<% Current.family.accounts.each do |account| %>
|
<%= lucide_icon("plus", class: "w-5 h-5") %>
|
||||||
<div class="flex flex-row items-center justify-between px-3 py-3 mb-2 bg-white shadow-sm rounded-xl">
|
<span><%= t('.new_account') %></span>
|
||||||
<div class="flex items-center w-1/3 text-sm">
|
<% end %>
|
||||||
<%= account.name %>
|
</div>
|
||||||
|
<% if Current.family.accounts.empty? %>
|
||||||
|
<div class="flex justify-center items-center h-[800px] text-sm">
|
||||||
|
<div class="text-center flex flex-col items-center max-w-[300px]">
|
||||||
|
<p class="text-gray-900 mb-1 font-medium">No accounts yet</p>
|
||||||
|
<p class="text-gray-500 mb-4">Add an account either via connection, importing or entering manually.</p>
|
||||||
|
<%= link_to new_account_path, class: "w-fit flex text-white text-sm font-medium items-center gap-1 bg-gray-900 rounded-lg p-2", data: { turbo_frame: "modal" } do %>
|
||||||
|
<%= lucide_icon("plus", class: "w-5 h-5") %>
|
||||||
|
<span><%= t('.new_account') %></span>
|
||||||
|
<% end %>
|
||||||
</div>
|
</div>
|
||||||
<div class="flex items-center w-1/3 text-sm">
|
|
||||||
<%= to_accountable_title(account.accountable) %>
|
|
||||||
</div>
|
|
||||||
<p class="flex w-1/3 text-sm text-right">
|
|
||||||
<span class="block mb-1"><%= format_currency account.converted_balance %></span>
|
|
||||||
</p>
|
|
||||||
</div>
|
</div>
|
||||||
<% end %>
|
<% else %>
|
||||||
|
<div>
|
||||||
|
<% Current.family.accounts.by_provider.each do |item| %>
|
||||||
|
<details class="bg-white group p-4 border border-alpha-black-25 shadow-xs rounded-xl">
|
||||||
|
<summary class="flex items-center gap-2">
|
||||||
|
<%= lucide_icon("chevron-down", class: "hidden group-open:block w-5 h-5 text-gray-500") %>
|
||||||
|
<%= lucide_icon("chevron-right", class: "group-open:hidden w-5 h-5 text-gray-500") %>
|
||||||
|
<% if item[:name] == "Manual accounts" %>
|
||||||
|
<div class="flex items-center justify-center h-8 w-8 rounded-full bg-black/5">
|
||||||
|
<%= lucide_icon("folder-pen", class: "w-5 h-5 text-gray-500") %>
|
||||||
|
</div>
|
||||||
|
<% end %>
|
||||||
|
<span class="text-sm font-medium text-gray-900">
|
||||||
|
<%= item[:name] %>
|
||||||
|
</span>
|
||||||
|
</summary>
|
||||||
|
<div class="space-y-4 mt-4">
|
||||||
|
<% item[:accounts].each do |group, accounts| %>
|
||||||
|
<div class="bg-gray-25 p-1 rounded-xl">
|
||||||
|
<div class="flex items-center px-4 py-2 text-xs font-medium text-gray-500">
|
||||||
|
<p><%= to_accountable_title(Accountable.from_type(group)) %></p>
|
||||||
|
<span class="text-gray-400 mx-2">·</span>
|
||||||
|
<p><%= accounts.count %></p>
|
||||||
|
<p class="ml-auto"><%= format_currency accounts.sum(&:balance) %></p>
|
||||||
|
</div>
|
||||||
|
<div class="bg-white">
|
||||||
|
<% accounts.each do |account| %>
|
||||||
|
<%= render account %>
|
||||||
|
<% end %>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<% end %>
|
||||||
|
</div>
|
||||||
|
</details>
|
||||||
|
<% end %>
|
||||||
|
</div>
|
||||||
|
<% end %>
|
||||||
|
</div>
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
<%= turbo_stream.replace Valuation.new, body: turbo_frame_tag(dom_id(Valuation.new)) %>
|
<%= turbo_stream.replace Valuation.new, body: turbo_frame_tag(dom_id(Valuation.new)) %>
|
||||||
<%= turbo_stream.append "notification-tray", partial: "shared/notification", locals: { type: "success", content: "Valuation created" } %>
|
<%= turbo_stream.append "notification-tray", partial: "shared/notification", locals: { type: "success", content: "Valuation created" } %>
|
||||||
<%= turbo_stream.replace "valuations_list", partial: "accounts/account_valuation_list", locals: { valuation_series: @account.valuation_series, classification: @account.classification } %>
|
<%= turbo_stream.replace "valuations_list", partial: "accounts/account_valuation_list", locals: { valuation_series: @account.valuations.to_series(@account) } %>
|
||||||
<%= turbo_stream.replace "sync_message", partial: "accounts/sync_message", locals: { is_syncing: true } %>
|
<%= turbo_stream.replace "sync_message", partial: "accounts/sync_message", locals: { is_syncing: true } %>
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
<%= turbo_stream.remove @valuation %>
|
<%= turbo_stream.remove @valuation %>
|
||||||
<%= turbo_stream.append "notification-tray", partial: "shared/notification", locals: { type: "success", content: "Valuation deleted" } %>
|
<%= turbo_stream.append "notification-tray", partial: "shared/notification", locals: { type: "success", content: "Valuation deleted" } %>
|
||||||
<%= turbo_stream.replace "valuations_list", partial: "accounts/account_valuation_list", locals: { valuation_series: @account.valuation_series, classification: @account.classification } %>
|
<%= turbo_stream.replace "valuations_list", partial: "accounts/account_valuation_list", locals: { valuation_series: @account.valuations.to_series(@account) } %>
|
||||||
<%= turbo_stream.replace "sync_message", partial: "accounts/sync_message", locals: { is_syncing: true } %>
|
<%= turbo_stream.replace "sync_message", partial: "accounts/sync_message", locals: { is_syncing: true } %>
|
||||||
|
|
|
@ -4,10 +4,12 @@ en:
|
||||||
create:
|
create:
|
||||||
success: New account created successfully
|
success: New account created successfully
|
||||||
index:
|
index:
|
||||||
title: Cash
|
new_account: New account
|
||||||
new:
|
new:
|
||||||
name:
|
name:
|
||||||
label: Account name
|
label: Account name
|
||||||
placeholder: Example account name
|
placeholder: Example account name
|
||||||
select_accountable_type: What would you like to add?
|
select_accountable_type: What would you like to add?
|
||||||
title: Add an account
|
title: Add an account
|
||||||
|
update:
|
||||||
|
success: Account updated successfully
|
||||||
|
|
5
db/migrate/20240306193345_add_is_active_to_account.rb
Normal file
5
db/migrate/20240306193345_add_is_active_to_account.rb
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
class AddIsActiveToAccount < ActiveRecord::Migration[7.2]
|
||||||
|
def change
|
||||||
|
add_column :accounts, :is_active, :boolean, default: true, null: false
|
||||||
|
end
|
||||||
|
end
|
5
db/schema.rb
generated
5
db/schema.rb
generated
|
@ -10,7 +10,7 @@
|
||||||
#
|
#
|
||||||
# It's strongly recommended that you check this file into your version control system.
|
# It's strongly recommended that you check this file into your version control system.
|
||||||
|
|
||||||
ActiveRecord::Schema[7.2].define(version: 2024_03_02_145715) do
|
ActiveRecord::Schema[7.2].define(version: 2024_03_06_193345) do
|
||||||
# These are extensions that must be enabled in order to support this database
|
# These are extensions that must be enabled in order to support this database
|
||||||
enable_extension "pgcrypto"
|
enable_extension "pgcrypto"
|
||||||
enable_extension "plpgsql"
|
enable_extension "plpgsql"
|
||||||
|
@ -79,7 +79,8 @@ ActiveRecord::Schema[7.2].define(version: 2024_03_02_145715) do
|
||||||
t.decimal "converted_balance", precision: 19, scale: 4, default: "0.0"
|
t.decimal "converted_balance", precision: 19, scale: 4, default: "0.0"
|
||||||
t.string "converted_currency", default: "USD"
|
t.string "converted_currency", default: "USD"
|
||||||
t.string "status", default: "OK"
|
t.string "status", default: "OK"
|
||||||
t.virtual "classification", type: :string, as: "\nCASE\n WHEN ((accountable_type)::text = ANY ((ARRAY['Account::Loan'::character varying, 'Account::Credit'::character varying, 'Account::OtherLiability'::character varying])::text[])) THEN 'liability'::text\n ELSE 'asset'::text\nEND", stored: true
|
t.virtual "classification", type: :string, as: "\nCASE\n WHEN ((accountable_type)::text = ANY (ARRAY[('Account::Loan'::character varying)::text, ('Account::Credit'::character varying)::text, ('Account::OtherLiability'::character varying)::text])) THEN 'liability'::text\n ELSE 'asset'::text\nEND", stored: true
|
||||||
|
t.boolean "is_active", default: true, null: false
|
||||||
t.index ["accountable_type"], name: "index_accounts_on_accountable_type"
|
t.index ["accountable_type"], name: "index_accounts_on_accountable_type"
|
||||||
t.index ["family_id"], name: "index_accounts_on_family_id"
|
t.index ["family_id"], name: "index_accounts_on_family_id"
|
||||||
end
|
end
|
||||||
|
|
|
@ -42,6 +42,22 @@ class FamilyTest < ActiveSupport::TestCase
|
||||||
assert_equal BigDecimal("24550"), @family.net_worth
|
assert_equal BigDecimal("24550"), @family.net_worth
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "should exclude disabled accounts from calculations" do
|
||||||
|
assets_before = @family.assets
|
||||||
|
liabilities_before = @family.liabilities
|
||||||
|
net_worth_before = @family.net_worth
|
||||||
|
|
||||||
|
disabled_checking = accounts(:checking)
|
||||||
|
disabled_cc = accounts(:credit_card)
|
||||||
|
|
||||||
|
disabled_checking.update!(is_active: false)
|
||||||
|
disabled_cc.update!(is_active: false)
|
||||||
|
|
||||||
|
assert_equal assets_before - disabled_checking.balance, @family.assets
|
||||||
|
assert_equal liabilities_before - disabled_cc.balance, @family.liabilities
|
||||||
|
assert_equal net_worth_before - disabled_checking.balance + disabled_cc.balance, @family.net_worth
|
||||||
|
end
|
||||||
|
|
||||||
test "calculates asset series" do
|
test "calculates asset series" do
|
||||||
# Sum of expected balances for all asset accounts in balance_calculator_test.rb
|
# Sum of expected balances for all asset accounts in balance_calculator_test.rb
|
||||||
expected_balances = [
|
expected_balances = [
|
||||||
|
@ -108,6 +124,23 @@ class FamilyTest < ActiveSupport::TestCase
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "calculates balances by type with disabled account" do
|
||||||
|
disabled_checking = accounts(:checking).update!(is_active: false)
|
||||||
|
|
||||||
|
verify_balances_by_type(
|
||||||
|
period: Period.all,
|
||||||
|
expected_asset_total: BigDecimal("20550"),
|
||||||
|
expected_liability_total: BigDecimal("1000"),
|
||||||
|
expected_asset_groups: {
|
||||||
|
"Account::OtherAsset" => { end_balance: BigDecimal("550"), start_balance: BigDecimal("400"), allocation: 2.68 },
|
||||||
|
"Account::Depository" => { end_balance: BigDecimal("20000"), start_balance: BigDecimal("21250"), allocation: 97.32 }
|
||||||
|
},
|
||||||
|
expected_liability_groups: {
|
||||||
|
"Account::Credit" => { end_balance: BigDecimal("1000"), start_balance: BigDecimal("1040"), allocation: 100 }
|
||||||
|
}
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def verify_balances_by_type(period:, expected_asset_total:, expected_liability_total:, expected_asset_groups:, expected_liability_groups:)
|
def verify_balances_by_type(period:, expected_asset_total:, expected_liability_total:, expected_asset_groups:, expected_liability_groups:)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue