Track account balance dynamically in AccountActivity

This commit is contained in:
Karl Erik Õunapuu 2021-02-15 14:41:50 +02:00
parent 416390a3c4
commit 27e441bc24
No known key found for this signature in database
GPG key ID: C9DD647298A34764
4 changed files with 19 additions and 3 deletions

View file

@ -13,6 +13,9 @@ class AccountActivity < ApplicationRecord
def update_balance def update_balance
account.balance += sum account.balance += sum
account.save account.save
self.new_balance = account.balance
save
end end
class << self class << self

View file

@ -14,7 +14,7 @@
<table class="table table-hover table-condensed"> <table class="table table-hover table-condensed">
<thead> <thead>
<tr> <tr>
<th class="col-xs-5"> <th class="col-xs-3">
<%= sort_link(@q, 'description') %> <%= sort_link(@q, 'description') %>
</th> </th>
<th class="col-xs-2"> <th class="col-xs-2">
@ -26,6 +26,9 @@
<th class="col-xs-2"> <th class="col-xs-2">
<%= sort_link(@q, 'sum') %> <%= sort_link(@q, 'sum') %>
</th> </th>
<th class="col-xs-2">
<%= sort_link(@q, 'new_balance', 'New balance') %>
</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
@ -45,6 +48,9 @@
<td class="<%= c %>"> <td class="<%= c %>">
<%= s %> <%= s %>
</td> </td>
<td>
<%= x.new_balance.present? ? "#{currency(x.new_balance)} EUR" : 'N/A' %>
</td>
</tr> </tr>
<% end %> <% end %>
</tbody> </tbody>

View file

@ -0,0 +1,5 @@
class AddNewBalanceToAccountActivity < ActiveRecord::Migration[6.0]
def change
add_column :account_activities, :new_balance, :decimal, precision: 10, scale: 2, null: true
end
end

View file

@ -226,7 +226,8 @@ CREATE TABLE public.account_activities (
creator_str character varying, creator_str character varying,
updator_str character varying, updator_str character varying,
activity_type character varying, activity_type character varying,
price_id integer price_id integer,
new_balance numeric(10,2)
); );
@ -4959,6 +4960,7 @@ INSERT INTO "schema_migrations" (version) VALUES
('20200908131554'), ('20200908131554'),
('20200910085157'), ('20200910085157'),
('20200910102028'), ('20200910102028'),
('20200916125326'); ('20200916125326'),
('20210215101019');