simple_wallet 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2c3400c19f3e0b718cc3af58b3bacb0ecb45e6c2749a454ff0d4482648533370
4
- data.tar.gz: da498f98223ef9dc2d3f32ff4f43937163c5fd06cf40d33d0b59416d2f82761e
3
+ metadata.gz: ecb26e24cf18bdc51ca2d483ee5570a91418108e4aa42da8138c38d7de099704
4
+ data.tar.gz: a20a7b3eed5ce08422a739b1419f73da1d1ed7fbd08800ccf5ca329daef09aa7
5
5
  SHA512:
6
- metadata.gz: 360e49d1e3b7896c4d2694b9dab53d54c8d39f4a73f48445d33f190807e8b4ccc685fba25ab595d9eed74c3760b81e72b50b4ead1f7d82cf2a1b2d078d9dce2c
7
- data.tar.gz: 88c4b352059950f58ab5cf5a4061e4a868dae88210fc3ae2d497701349392bdc3c6e6878d4df233ce473281b438409974c2317d56c797be472133addd062845c
6
+ metadata.gz: c4c60183ac0922910a4de5fed4defba64d56126ff2809ab9b20a02996dbbd38dcc641213787ccfda7afe6c234372674737fc5b60578832bc5dcdc36685ac5def
7
+ data.tar.gz: bc129abbe862eba61e6714d1ee6665839f4f2c95665b005978479b882977dc00e36d559e7109362d3978786696daa861ec030793365af4546407cf9e6dd0a4b7
@@ -0,0 +1,35 @@
1
+ module SimpleWallet
2
+ class Account < ::ApplicationRecord
3
+ self.table_name = "simple_wallet_accounts"
4
+
5
+ has_many :transactions, class_name: "::SimpleWallet::Transaction", foreign_key: :account_id, dependent: :destroy
6
+
7
+ validates :balance, presence: true, numericality: { only_integer: true, greater_than_or_equal_to: 0 }
8
+ validates :income, presence: true, numericality: { only_integer: true, greater_than_or_equal_to: 0 }
9
+ validates :outcome, presence: true, numericality: { only_integer: true, less_than_or_equal_to: 0 }
10
+
11
+ def recalculate_balance!
12
+ SimpleWallet::Account.where(id: id).update_all(<<~SQL)
13
+ balance = (
14
+ SELECT COALESCE(SUM(amount), 0)
15
+ FROM simple_wallet_transactions
16
+ WHERE simple_wallet_transactions.account_id = simple_wallet_accounts.id
17
+ ),
18
+ income = (
19
+ SELECT COALESCE(SUM(amount), 0)
20
+ FROM simple_wallet_transactions
21
+ WHERE
22
+ simple_wallet_transactions.type = 'SimpleWallet::Transaction::Credit'
23
+ AND simple_wallet_transactions.account_id = simple_wallet_accounts.id
24
+ ),
25
+ outcome = (
26
+ SELECT COALESCE(SUM(amount), 0)
27
+ FROM simple_wallet_transactions
28
+ WHERE
29
+ simple_wallet_transactions.type = 'SimpleWallet::Transaction::Debit'
30
+ AND simple_wallet_transactions.account_id = simple_wallet_accounts.id
31
+ )
32
+ SQL
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,7 @@
1
+ module SimpleWallet
2
+ class Transaction < ::ApplicationRecord
3
+ class Credit < ::SimpleWallet::Transaction
4
+ validates :amount, numericality: { only_integer: true, greater_than_or_equal_to: 0 }
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ module SimpleWallet
2
+ class Transaction < ::ApplicationRecord
3
+ class Debit < ::SimpleWallet::Transaction
4
+ validates :amount, numericality: { only_integer: true, less_than_or_equal_to: 0 }
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,11 @@
1
+ module SimpleWallet
2
+ class Transaction < ::ApplicationRecord
3
+ self.table_name = "simple_wallet_transactions"
4
+
5
+ belongs_to :account, class_name: "::SimpleWallet::Account"
6
+ belongs_to :source, polymorphic: true, optional: true
7
+
8
+ validates :amount, presence: true
9
+ validates :note, presence: { unless: :source_id? }
10
+ end
11
+ end
@@ -0,0 +1,14 @@
1
+ class CreateSimpleWalletAccount < ActiveRecord::Migration[8.1]
2
+ def up
3
+ create_table :simple_wallet_accounts do |t|
4
+ t.integer :balance, null: false, default: 0
5
+ t.integer :income, null: false, default: 0
6
+ t.integer :outcome, null: false, default: 0
7
+ t.timestamps
8
+ end
9
+
10
+ add_check_constraint :simple_wallet_accounts, "balance >= 0", name: :simple_wallet_balance_positive_chk
11
+ add_check_constraint :simple_wallet_accounts, "income >= 0", name: :simple_wallet_income_positive_chk
12
+ add_check_constraint :simple_wallet_accounts, "outcome <= 0", name: :simple_wallet_outcome_positive_chk
13
+ end
14
+ end
@@ -0,0 +1,17 @@
1
+ class CreateSimpleWalletTransactions < ActiveRecord::Migration[8.1]
2
+ def change
3
+ create_table :simple_wallet_transactions do |t|
4
+ t.string :type, null: false, index: true
5
+ t.references :account, null: false, foreign_key: { to_table: :simple_wallet_accounts }, index: true
6
+ t.references :source, polymorphic: true
7
+ t.integer :pre_account_balance, null: false, default: 0
8
+ t.integer :amount, null: false
9
+ t.text :note
10
+ t.timestamps
11
+ end
12
+
13
+ add_check_constraint :simple_wallet_transactions,
14
+ "((type = 'SimpleWallet::Transaction::Credit') AND (amount >= 0)) OR ((type = 'SimpleWallet::Transaction::Debit') AND (amount <= 0))",
15
+ name: :simple_wallet_transaction_amount_chk
16
+ end
17
+ end
@@ -1,3 +1,3 @@
1
1
  module SimpleWallet
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -0,0 +1,45 @@
1
+ # This file is auto-generated from the current state of the database. Instead
2
+ # of editing this file, please use the migrations feature of Active Record to
3
+ # incrementally modify your database, and then regenerate this schema definition.
4
+ #
5
+ # This file is the source Rails uses to define your schema when running `bin/rails
6
+ # db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to
7
+ # be faster and is potentially less error prone than running all of your
8
+ # migrations from scratch. Old migrations may fail to apply correctly if those
9
+ # migrations use external dependencies or application code.
10
+ #
11
+ # It's strongly recommended that you check this file into your version control system.
12
+
13
+ ActiveRecord::Schema[8.1].define(version: 2026_03_29_181402) do
14
+ # These are extensions that must be enabled in order to support this database
15
+ enable_extension "pg_catalog.plpgsql"
16
+
17
+ create_table "simple_wallet_accounts", force: :cascade do |t|
18
+ t.integer "balance", default: 0, null: false
19
+ t.datetime "created_at", null: false
20
+ t.integer "income", default: 0, null: false
21
+ t.integer "outcome", default: 0, null: false
22
+ t.datetime "updated_at", null: false
23
+ t.check_constraint "balance >= 0", name: "simple_wallet_balance_positive_chk"
24
+ t.check_constraint "income >= 0", name: "simple_wallet_income_positive_chk"
25
+ t.check_constraint "outcome <= 0", name: "simple_wallet_outcome_positive_chk"
26
+ end
27
+
28
+ create_table "simple_wallet_transactions", force: :cascade do |t|
29
+ t.bigint "account_id", null: false
30
+ t.integer "amount", null: false
31
+ t.datetime "created_at", null: false
32
+ t.text "note"
33
+ t.integer "pre_account_balance", default: 0, null: false
34
+ t.bigint "source_id"
35
+ t.string "source_type"
36
+ t.string "type", null: false
37
+ t.datetime "updated_at", null: false
38
+ t.index ["account_id"], name: "index_simple_wallet_transactions_on_account_id"
39
+ t.index ["source_type", "source_id"], name: "index_simple_wallet_transactions_on_source"
40
+ t.index ["type"], name: "index_simple_wallet_transactions_on_type"
41
+ t.check_constraint "type::text = 'SimpleWallet::Transaction::Credit'::text AND amount >= 0 OR type::text = 'SimpleWallet::Transaction::Debit'::text AND amount <= 0", name: "simple_wallet_transaction_amount_chk"
42
+ end
43
+
44
+ add_foreign_key "simple_wallet_transactions", "simple_wallet_accounts", column: "account_id"
45
+ end