Skip to content

Commit

Permalink
in process.
Browse files Browse the repository at this point in the history
added config
renamed ledger_person_id to person_id
added tenancy in schema
added generator and tests
readme edit

yet to do
- add associations and validations of them in models
- reconfigure the tests to work with new structure
  • Loading branch information
merof-code committed Aug 7, 2024
1 parent 715b45f commit 411aa0a
Show file tree
Hide file tree
Showing 19 changed files with 304 additions and 158 deletions.
33 changes: 18 additions & 15 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,24 @@ source "https://rubygems.org"
gemspec

# Development and testing dependencies
gem "factory_bot"
gem "mysql2"
gem "pg"
gem "rake", "~> 13.2.1"
gem "rspec", "~> 3.0"
gem "rubocop", "~> 1.21"
gem "rubocop-performance", require: false
gem "rubocop-rspec", require: false
gem "shoulda-matchers"
gem "sorbet"
gem "sorbet-rails"
gem "sorbet-runtime"
gem "sqlite3"
gem "tapioca", require: false
gem "yard"
group :development do
gem "factory_bot"
gem "mysql2"
gem "pg"
gem "rake", "~> 13.2.1"
gem "rspec", "~> 3.0"
gem "rubocop", "~> 1.21"
gem "rubocop-performance", require: false
gem "rubocop-rspec", require: false
gem "shoulda-matchers"
gem "sorbet"
gem "sorbet-rails"
gem "sorbet-runtime"
gem "sqlite3"
gem "tapioca", require: false
gem "yard"
end
group :test do
gem "database_cleaner-active_record"
gem "generator_spec"
end
4 changes: 4 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ GEM
erubi (1.13.0)
factory_bot (6.4.6)
activesupport (>= 5.0.0)
generator_spec (0.10.0)
activesupport (>= 3.0.0)
railties (>= 3.0.0)
highline (3.0.1)
i18n (1.14.5)
concurrent-ruby (~> 1.0)
Expand Down Expand Up @@ -245,6 +248,7 @@ PLATFORMS
DEPENDENCIES
database_cleaner-active_record
factory_bot
generator_spec
ledger!
mysql2
pg
Expand Down
51 changes: 43 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Ledger

[![Ruby](https://github.com/merof-code/double_entry_ledger/actions/workflows/main.yml/badge.svg)](https://github.com/merof-code/double_entry_ledger/actions/workflows/main.yml)
## Introduction

The Ledger gem is a comprehensive implementation of a double-entry bookkeeping system, following the principles of [Double-Entry Bookkeeping](http://en.wikipedia.org/wiki/Double-entry_bookkeeping_system). Key features include:
Expand All @@ -15,15 +15,37 @@ The Ledger gem is a comprehensive implementation of a double-entry bookkeeping s
- Provides a base table for documents, connectable to business models via a polymorphic association
- Includes a chart of accounts table where the `id` represents the account number (e.g., `ledger_accounts.id = 331`)

### Non-Rails Usage

If not using Rails, initialize with:
## Installation

### Before you install

GEM IS STILL IN ALPHA MODE, NOT DEPLOYED YET
### Steps to install

1. `bundle add double_entry_ledger` or `gem install double_entry_ledger` if you are not in rails and also [check out additional steps](#non-rails)
2. `rails g ledger::install`
3. In the migration, update `tenant_table_name` and `user_accounts_table_name` to your tables, for all the required FKs.
```ruby
MoneyRails::Hooks.init
tenant_table_name = "tenants"
user_accounts_table_name = "accounts"
```

## Installation
4. In the created ``
5. Add the following code to your `Tenant` and `Person` models:
```ruby
# models\tenant.rb
class Tenant < ApplicationRecord
has_many :transfers, class_name: "Ledger::Transfer", foreign_key: "tenant_id"
end

# models\person.rb # This is an example of a name.
class Person < ApplicationRecord
has_many :account_balances, class_name: "Ledger::AccountBalance", foreign_key: "person_id"
has_many :entries, class_name: "Ledger::Entry", foreign_key: "person_id", inverse_of: :person
has_many :transfers, through: :entries, source: :ledger_transfer
has_many :documents, through: :transfers, source: :ledger_document
end
```

Replace `UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG` with your gem name after releasing it to RubyGems.org. If not releasing to RubyGems.org, replace this section with installation instructions from git.

Expand All @@ -39,6 +61,19 @@ If not using Bundler, install the gem with:
$ gem install UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
```

In order to use this gem you need to have `tenant` and `person` models in your app already.
When running
`rails g ledger::install`

### Configuration
### Non-Rails

If not using Rails, initialize with:

```ruby
MoneyRails::Hooks.init
```

## Usage

```mermaid
Expand All @@ -64,7 +99,7 @@ erDiagram
ledger_entries {
bigint ledger_transfer_id
bigint ledger_account_id
bigint ledger_person_id
bigint account_id
boolean is_debit
integer amount_cents
string amount_currency
Expand All @@ -78,7 +113,7 @@ erDiagram
ledger_account_balances {
bigint ledger_person_id
bigint account_id
integer balance_cents
string balance_currency
bigint ledger_account_id
Expand Down
45 changes: 29 additions & 16 deletions config/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

ActiveRecord::Schema.define(version: 20_240_701_000_001) do # rubocop:disable Metrics/BlockLength
self.verbose = false
# tenant_table_name = "YOUR TENANT TABLE NAME FOR THE LEDGER"
tenant_table_name = "tenants"
user_accounts_table_name = "accounts"

create_table "ledger_documents", force: :cascade do |t|
t.date "date", null: false
Expand Down Expand Up @@ -42,7 +45,7 @@
create_table "ledger_entries", force: :cascade do |t|
t.bigint "ledger_transfer_id", null: false
t.bigint "ledger_account_id", null: false
t.bigint "ledger_person_id"
t.bigint "person_id"
t.boolean "is_debit", default: false, null: false
t.integer "amount_cents", default: 0, null: false
t.string "amount_currency", limit: 3, default: "USD", null: false
Expand All @@ -51,28 +54,22 @@
t.index ["is_debit"], name: "index_ledger_entries_on_is_debit"
t.index ["ledger_account_id"], name: "index_ledger_entries_on_ledger_account_id"
t.index ["ledger_transfer_id"], name: "index_ledger_entries_on_ledger_transfer_id"
t.index ["ledger_person_id"], name: "index_ledger_entries_on_ledger_person_id"
t.index ["person_id"], name: "index_ledger_entries_on_person_id"
end

create_table "ledger_transfers", force: :cascade do |t|
t.date "date", null: false
t.bigint "tenant_id", null: false
t.bigint "ledger_document_id", null: false
t.string "description", limit: 255, default: "", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["tenant_id"], name: "index_ledger_transfers_on_tenant_id"
t.index ["date"], name: "index_ledger_transfers_on_date"
end

create_table "ledger_people", force: :cascade do |t|
t.string "personable_type", null: false
t.bigint "personable_id", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index %w[personable_type personable_id], name: "index_ledger_people_on_personable"
end

create_table "ledger_account_balances", force: :cascade do |t|
t.bigint "ledger_person_id", null: false
t.bigint "person_id", null: false
t.integer "balance_cents", default: 0, null: false
t.string "balance_currency", default: "USD", null: false
t.bigint "ledger_account_id", null: false
Expand All @@ -81,8 +78,8 @@
t.datetime "updated_at", null: false
t.index ["date"], name: "index_ledger_account_balances_on_date"
t.index ["ledger_account_id"], name: "index_ledger_account_balances_on_ledger_account_id"
t.index ["ledger_person_id"], name: "index_ledger_account_balances_on_ledger_person_id"
t.index %w[ledger_account_id ledger_person_id date balance_currency],
t.index ["person_id"], name: "index_ledger_account_balances_on_person_id"
t.index %w[ledger_account_id person_id date balance_currency],
unique: true, name: "index_ledger_account_balances_on_account_person_date"
column_name = "date"
constraint_command =
Expand All @@ -103,15 +100,31 @@
add_foreign_key "ledger_transfers", "ledger_documents"
add_foreign_key "ledger_entries", "ledger_accounts"
add_foreign_key "ledger_entries", "ledger_transfers"
add_foreign_key "ledger_account_balances", "ledger_people", column: "ledger_person_id"
add_foreign_key "ledger_account_balances", "ledger_accounts"
add_foreign_key "ledger_entries", "ledger_people", column: "ledger_person_id"
add_foreign_key "ledger_account_balances", user_accounts_table_name, column: "person_id"
add_foreign_key "ledger_entries", user_accounts_table_name, column: "person_id"
add_foreign_key "ledger_transfers", tenant_table_name

# test table only
# Tables needed to proper function of the gem, that are user-provided
create_table "users", force: :cascade do |t|
t.string "username", null: false
t.timestamps null: false
end

add_index "users", ["username"], name: "index_users_on_username", unique: true

create_table user_accounts_table_name, force: :cascade do |t|
t.string "personable_type", null: false
t.bigint "personable_id", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index %w[personable_type personable_id], name: "index_people_on_personable"
end

create_table tenant_table_name, force: :cascade do |t|
t.string "name", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index "name", name: "tenants_on_name", unique: true
end
end
38 changes: 0 additions & 38 deletions lib/generators/ledger/install/install_generator.rb

This file was deleted.

Loading

0 comments on commit 411aa0a

Please sign in to comment.