Skip to content

Commit

Permalink
feat(accumulative balance chart): Add accumulative balance chart for …
Browse files Browse the repository at this point in the history
…account (#33)

* feat(accumulative balance chart): Add accumulative balance chart for account

* feat(accumulative balance chart): fixup! Add accumulative balance chart for account

---------

Co-authored-by: VladislavSokov <[email protected]>
  • Loading branch information
VladislavSokov and VladislavSokov authored Aug 28, 2023
1 parent d2dc01f commit c36c0a8
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ gem 'devise'
gem "bulma-rails", "~> 0.9.2"
gem 'slim'
gem "font-awesome-rails"
gem 'chartkick'
gem 'groupdate'

# Use Active Storage variant
# gem 'image_processing', '~> 1.2'
Expand Down
5 changes: 5 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ GEM
rack-test (>= 0.6.3)
regexp_parser (>= 1.5, < 3.0)
xpath (~> 3.2)
chartkick (5.0.4)
childprocess (4.1.0)
coderay (1.1.3)
concurrent-ruby (1.1.9)
Expand Down Expand Up @@ -110,6 +111,8 @@ GEM
railties (>= 3.2, < 7)
globalid (0.5.2)
activesupport (>= 5.0)
groupdate (6.2.1)
activesupport (>= 5.2)
i18n (1.8.10)
concurrent-ruby (~> 1.0)
jbuilder (2.11.2)
Expand Down Expand Up @@ -299,12 +302,14 @@ DEPENDENCIES
bulma-rails (~> 0.9.2)
byebug
capybara (>= 2.15)
chartkick
database_cleaner
devise
dry-initializer
factory_bot_rails (~> 6.2)
ffaker
font-awesome-rails
groupdate
jbuilder (~> 2.7)
letter_opener
listen (>= 3.0.5, < 3.2)
Expand Down
2 changes: 2 additions & 0 deletions app/javascript/packs/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// a relevant structure within app/javascript and only use these pack files to reference
// that code so it'll be compiled.

import "chartkick/chart.js"

require("@rails/ujs").start()
require("turbolinks").start()
require("@rails/activestorage").start()
Expand Down
4 changes: 4 additions & 0 deletions app/models/account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,8 @@ class Account < ApplicationRecord
def transactions
Transaction.where(to_account: self).or(Transaction.where(from_account: self))
end

def accumulative_balance_data
transactions.group_by_day(:created_at).sum(:amount)
end
end
2 changes: 2 additions & 0 deletions app/views/accounts/show.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,5 @@

.card-footer
= link_to '+ Add', new_account_account_automatic_topup_config_path(account), class: 'card-footer-item'
= line_chart account.accumulative_balance_data, xtitle: 'Date', ytitle: 'Accumulative Balance'

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
"@rails/ujs": "^6.0.0",
"@rails/webpacker": "^5.4.3",
"babel-loader": "^8.2.2",
"chart.js": "^4.4.0",
"chartkick": "^5.0.1",
"turbolinks": "^5.2.0"
},
"version": "0.1.0",
Expand Down
16 changes: 16 additions & 0 deletions spec/models/account_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe Account, type: :model do
describe '#accumulative_balance_data' do
let(:parent) { create(:account, :parent) }
let(:child) { create(:account, :children, parent: parent) }
let!(:first_transaction) { create(:transaction, to_account: child, from_account: parent, amount: 100, created_at: 1.day.ago) }
let!(:second_transaction) { create(:transaction, to_account: child, from_account: parent, amount: 50, created_at: Time.current) }

it 'calculates accumulative balance data' do
expect(parent.accumulative_balance_data).to eq({ first_transaction.created_at.to_date => 100, second_transaction.created_at.to_date => 50 })
end
end
end
45 changes: 45 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -848,6 +848,13 @@
dependencies:
regenerator-runtime "^0.13.4"

"@babel/runtime@^7.21.0":
version "7.22.11"
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.22.11.tgz#7a9ba3bbe406ad6f9e8dd4da2ece453eb23a77a4"
integrity sha512-ee7jVNlWN09+KftVOu9n7S8gQzD/Z6hN/I8VBRXW4P1+Xe7kJGXMwu8vds4aGIMHZnNbdpSWCfZZtinytpcAvA==
dependencies:
regenerator-runtime "^0.14.0"

"@babel/template@^7.15.4":
version "7.15.4"
resolved "https://registry.yarnpkg.com/@babel/template/-/template-7.15.4.tgz#51898d35dcf3faa670c4ee6afcfd517ee139f194"
Expand Down Expand Up @@ -895,6 +902,11 @@
resolved "https://registry.yarnpkg.com/@gar/promisify/-/promisify-1.1.2.tgz#30aa825f11d438671d585bd44e7fd564535fc210"
integrity sha512-82cpyJyKRoQoRi+14ibCeGPu0CwypgtBAdBhq1WfvagpCZNKqwXbKwXllYSMG91DhmG4jt9gN8eP6lGOtozuaw==

"@kurkle/color@^0.3.0":
version "0.3.2"
resolved "https://registry.yarnpkg.com/@kurkle/color/-/color-0.3.2.tgz#5acd38242e8bde4f9986e7913c8fdf49d3aa199f"
integrity sha512-fuscdXJ9G1qb7W8VdHi+IwRqij3lBkosAm4ydQtEmbY58OzHXqQhvlxqEkoz0yssNVn38bcpRWgA9PP+OGoisw==

"@nodelib/[email protected]":
version "2.1.5"
resolved "https://registry.yarnpkg.com/@nodelib/fs.scandir/-/fs.scandir-2.1.5.tgz#7619c2eb21b25483f6d167548b4cfd5a7488c3d5"
Expand Down Expand Up @@ -1954,6 +1966,27 @@ chalk@^2.0, chalk@^2.0.0, chalk@^2.4.1, chalk@^2.4.2:
escape-string-regexp "^1.0.5"
supports-color "^5.3.0"

chart.js@4, chart.js@^4.4.0:
version "4.4.0"
resolved "https://registry.yarnpkg.com/chart.js/-/chart.js-4.4.0.tgz#df843fdd9ec6bd88d7f07e2b95348d221bd2698c"
integrity sha512-vQEj6d+z0dcsKLlQvbKIMYFHd3t8W/7L2vfJIbYcfyPcRx92CsHqECpueN8qVGNlKyDcr5wBrYAYKnfu/9Q1hQ==
dependencies:
"@kurkle/color" "^0.3.0"

chartjs-adapter-date-fns@>=3:
version "3.0.0"
resolved "https://registry.yarnpkg.com/chartjs-adapter-date-fns/-/chartjs-adapter-date-fns-3.0.0.tgz#c25f63c7f317c1f96f9a7c44bd45eeedb8a478e5"
integrity sha512-Rs3iEB3Q5pJ973J93OBTpnP7qoGwvq3nUnoMdtxO+9aoJof7UFcRbWcIDteXuYd1fgAvct/32T9qaLyLuZVwCg==

chartkick@^5.0.1:
version "5.0.1"
resolved "https://registry.yarnpkg.com/chartkick/-/chartkick-5.0.1.tgz#f557ff8560f974343dc65c7fc34ce1e8326d8ee7"
integrity sha512-4F3tWI3eBQgnjCYZIZ+fHOaJuNyxeyhDE2Tm+voOWB19hDjSJceys/spzN52DOn8bWepNESGXvPVTGU1jeFsbA==
optionalDependencies:
chart.js "4"
chartjs-adapter-date-fns ">=3"
date-fns ">=2"

"chokidar@>=3.0.0 <4.0.0", chokidar@^3.4.1, chokidar@^3.5.1:
version "3.5.2"
resolved "https://registry.yarnpkg.com/chokidar/-/chokidar-3.5.2.tgz#dba3976fcadb016f66fd365021d91600d01c1e75"
Expand Down Expand Up @@ -2518,6 +2551,13 @@ cyclist@^1.0.1:
resolved "https://registry.yarnpkg.com/cyclist/-/cyclist-1.0.1.tgz#596e9698fd0c80e12038c2b82d6eb1b35b6224d9"
integrity sha1-WW6WmP0MgOEgOMK4LW6xs1tiJNk=

date-fns@>=2:
version "2.30.0"
resolved "https://registry.yarnpkg.com/date-fns/-/date-fns-2.30.0.tgz#f367e644839ff57894ec6ac480de40cae4b0f4d0"
integrity sha512-fnULvOpxnC5/Vg3NCiWelDsLiUc9bRwAPs/+LfTLNvetFCtCTN+yQz15C/fs4AwX1R9K5GLtLfn8QW+dWisaAw==
dependencies:
"@babel/runtime" "^7.21.0"

[email protected], debug@^2.2.0, debug@^2.3.3:
version "2.6.9"
resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.9.tgz#5d128515df134ff327e90a4c93f4e077a536341f"
Expand Down Expand Up @@ -5962,6 +6002,11 @@ regenerator-runtime@^0.13.4, regenerator-runtime@^0.13.9:
resolved "https://registry.yarnpkg.com/regenerator-runtime/-/regenerator-runtime-0.13.9.tgz#8925742a98ffd90814988d7566ad30ca3b263b52"
integrity sha512-p3VT+cOEgxFsRRA9X4lkI1E+k2/CtnKtU4gcxyaCUreilL/vqI6CdZ3wxVUx3UOUg+gnUOQQcRI7BmSI656MYA==

regenerator-runtime@^0.14.0:
version "0.14.0"
resolved "https://registry.yarnpkg.com/regenerator-runtime/-/regenerator-runtime-0.14.0.tgz#5e19d68eb12d486f797e15a3c6a918f7cec5eb45"
integrity sha512-srw17NI0TUWHuGa5CFGGmhfNIeja30WMBfbslPNhf6JrqQlLN5gcrvig1oqPxiVaXb0oW0XRKtH6Nngs5lKCIA==

regenerator-transform@^0.14.2:
version "0.14.5"
resolved "https://registry.yarnpkg.com/regenerator-transform/-/regenerator-transform-0.14.5.tgz#c98da154683671c9c4dcb16ece736517e1b7feb4"
Expand Down

0 comments on commit c36c0a8

Please sign in to comment.