Skip to content

Commit

Permalink
Merge pull request #1 from bmaclean/add-specs-and-improve-net-amount-…
Browse files Browse the repository at this point in the history
…computation

Support custom net amount computation
  • Loading branch information
bmaclean authored Jun 14, 2024
2 parents 59c0e1c + 9c355a0 commit 1e7d56b
Show file tree
Hide file tree
Showing 11 changed files with 417 additions and 144 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Ledger Accountable Test Workflow

on:
pull_request:
branches: [ main ]

jobs:
test:
runs-on: ubuntu-latest

steps:
- name: Check out code
uses: actions/checkout@v2

- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: 2.7.8

- name: Install dependencies
run: bundle install

- name: Run tests
run: rake test
194 changes: 78 additions & 116 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,105 +1,91 @@
PATH
remote: .
specs:
ledger_accountable (0.0.1)
activerecord (>= 6.0)
rails (>= 6.0)
ledger_accountable (0.0.5.pre)
activerecord (>= 6.0.0, < 7.1)
activesupport (>= 6.0.0, < 7.1)

GEM
remote: https://rubygems.org/
specs:
actioncable (7.1.3.4)
actionpack (= 7.1.3.4)
activesupport (= 7.1.3.4)
actioncable (7.0.8.4)
actionpack (= 7.0.8.4)
activesupport (= 7.0.8.4)
nio4r (~> 2.0)
websocket-driver (>= 0.6.1)
zeitwerk (~> 2.6)
actionmailbox (7.1.3.4)
actionpack (= 7.1.3.4)
activejob (= 7.1.3.4)
activerecord (= 7.1.3.4)
activestorage (= 7.1.3.4)
activesupport (= 7.1.3.4)
actionmailbox (7.0.8.4)
actionpack (= 7.0.8.4)
activejob (= 7.0.8.4)
activerecord (= 7.0.8.4)
activestorage (= 7.0.8.4)
activesupport (= 7.0.8.4)
mail (>= 2.7.1)
net-imap
net-pop
net-smtp
actionmailer (7.1.3.4)
actionpack (= 7.1.3.4)
actionview (= 7.1.3.4)
activejob (= 7.1.3.4)
activesupport (= 7.1.3.4)
actionmailer (7.0.8.4)
actionpack (= 7.0.8.4)
actionview (= 7.0.8.4)
activejob (= 7.0.8.4)
activesupport (= 7.0.8.4)
mail (~> 2.5, >= 2.5.4)
net-imap
net-pop
net-smtp
rails-dom-testing (~> 2.2)
actionpack (7.1.3.4)
actionview (= 7.1.3.4)
activesupport (= 7.1.3.4)
nokogiri (>= 1.8.5)
racc
rack (>= 2.2.4)
rack-session (>= 1.0.1)
rails-dom-testing (~> 2.0)
actionpack (7.0.8.4)
actionview (= 7.0.8.4)
activesupport (= 7.0.8.4)
rack (~> 2.0, >= 2.2.4)
rack-test (>= 0.6.3)
rails-dom-testing (~> 2.2)
rails-html-sanitizer (~> 1.6)
actiontext (7.1.3.4)
actionpack (= 7.1.3.4)
activerecord (= 7.1.3.4)
activestorage (= 7.1.3.4)
activesupport (= 7.1.3.4)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.0, >= 1.2.0)
actiontext (7.0.8.4)
actionpack (= 7.0.8.4)
activerecord (= 7.0.8.4)
activestorage (= 7.0.8.4)
activesupport (= 7.0.8.4)
globalid (>= 0.6.0)
nokogiri (>= 1.8.5)
actionview (7.1.3.4)
activesupport (= 7.1.3.4)
actionview (7.0.8.4)
activesupport (= 7.0.8.4)
builder (~> 3.1)
erubi (~> 1.11)
rails-dom-testing (~> 2.2)
rails-html-sanitizer (~> 1.6)
activejob (7.1.3.4)
activesupport (= 7.1.3.4)
erubi (~> 1.4)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.1, >= 1.2.0)
activejob (7.0.8.4)
activesupport (= 7.0.8.4)
globalid (>= 0.3.6)
activemodel (7.1.3.4)
activesupport (= 7.1.3.4)
activerecord (7.1.3.4)
activemodel (= 7.1.3.4)
activesupport (= 7.1.3.4)
timeout (>= 0.4.0)
activestorage (7.1.3.4)
actionpack (= 7.1.3.4)
activejob (= 7.1.3.4)
activerecord (= 7.1.3.4)
activesupport (= 7.1.3.4)
activemodel (7.0.8.4)
activesupport (= 7.0.8.4)
activerecord (7.0.8.4)
activemodel (= 7.0.8.4)
activesupport (= 7.0.8.4)
activestorage (7.0.8.4)
actionpack (= 7.0.8.4)
activejob (= 7.0.8.4)
activerecord (= 7.0.8.4)
activesupport (= 7.0.8.4)
marcel (~> 1.0)
activesupport (7.1.3.4)
base64
bigdecimal
mini_mime (>= 1.1.0)
activesupport (7.0.8.4)
concurrent-ruby (~> 1.0, >= 1.0.2)
connection_pool (>= 2.2.5)
drb
i18n (>= 1.6, < 2)
minitest (>= 5.1)
mutex_m
tzinfo (~> 2.0)
base64 (0.2.0)
bigdecimal (3.1.8)
builder (3.3.0)
concurrent-ruby (1.3.3)
connection_pool (2.4.1)
crass (1.0.6)
database_cleaner-active_record (2.1.0)
activerecord (>= 5.a)
database_cleaner-core (~> 2.0.0)
database_cleaner-core (2.0.1)
date (3.3.4)
diff-lcs (1.5.1)
drb (2.2.1)
erubi (1.12.0)
erubi (1.13.0)
globalid (1.2.1)
activesupport (>= 6.1)
i18n (1.14.5)
concurrent-ruby (~> 1.0)
io-console (0.7.2)
irb (1.13.1)
rdoc (>= 4.0.0)
reline (>= 0.4.2)
loofah (2.22.0)
crass (~> 1.0.2)
nokogiri (>= 1.12.0)
Expand All @@ -109,10 +95,10 @@ GEM
net-pop
net-smtp
marcel (1.0.4)
method_source (1.1.0)
mini_mime (1.1.5)
mini_portile2 (2.8.7)
minitest (5.23.1)
mutex_m (0.2.0)
net-imap (0.4.12)
date
net-protocol
Expand All @@ -126,70 +112,45 @@ GEM
nokogiri (1.15.6)
mini_portile2 (~> 2.8.2)
racc (~> 1.4)
psych (5.1.2)
stringio
racc (1.8.0)
rack (3.1.3)
rack-session (2.0.0)
rack (>= 3.0.0)
rack (2.2.9)
rack-test (2.1.0)
rack (>= 1.3)
rackup (2.1.0)
rack (>= 3)
webrick (~> 1.8)
rails (7.1.3.4)
actioncable (= 7.1.3.4)
actionmailbox (= 7.1.3.4)
actionmailer (= 7.1.3.4)
actionpack (= 7.1.3.4)
actiontext (= 7.1.3.4)
actionview (= 7.1.3.4)
activejob (= 7.1.3.4)
activemodel (= 7.1.3.4)
activerecord (= 7.1.3.4)
activestorage (= 7.1.3.4)
activesupport (= 7.1.3.4)
rails (7.0.8.4)
actioncable (= 7.0.8.4)
actionmailbox (= 7.0.8.4)
actionmailer (= 7.0.8.4)
actionpack (= 7.0.8.4)
actiontext (= 7.0.8.4)
actionview (= 7.0.8.4)
activejob (= 7.0.8.4)
activemodel (= 7.0.8.4)
activerecord (= 7.0.8.4)
activestorage (= 7.0.8.4)
activesupport (= 7.0.8.4)
bundler (>= 1.15.0)
railties (= 7.1.3.4)
railties (= 7.0.8.4)
rails-dom-testing (2.2.0)
activesupport (>= 5.0.0)
minitest
nokogiri (>= 1.6)
rails-html-sanitizer (1.6.0)
loofah (~> 2.21)
nokogiri (~> 1.14)
railties (7.1.3.4)
actionpack (= 7.1.3.4)
activesupport (= 7.1.3.4)
irb
rackup (>= 1.0.0)
railties (7.0.8.4)
actionpack (= 7.0.8.4)
activesupport (= 7.0.8.4)
method_source
rake (>= 12.2)
thor (~> 1.0, >= 1.2.2)
zeitwerk (~> 2.6)
thor (~> 1.0)
zeitwerk (~> 2.5)
rake (12.3.3)
rdoc (6.7.0)
psych (>= 4.0.0)
reline (0.5.9)
io-console (~> 0.5)
rspec (3.13.0)
rspec-core (~> 3.13.0)
rspec-expectations (~> 3.13.0)
rspec-mocks (~> 3.13.0)
rspec-core (3.13.0)
rspec-support (~> 3.13.0)
rspec-expectations (3.13.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.13.0)
rspec-mocks (3.13.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.13.0)
rspec-support (3.13.1)
stringio (3.1.0)
sqlite3 (1.6.9)
mini_portile2 (~> 2.8.0)
thor (1.3.1)
timeout (0.4.1)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
webrick (1.8.1)
websocket-driver (0.7.6)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.5)
Expand All @@ -199,10 +160,11 @@ PLATFORMS
ruby

DEPENDENCIES
database_cleaner-active_record (= 2.1.0)
ledger_accountable!
rails (> 6)
rake (~> 12.0)
rspec (~> 3.0)
sqlite3 (= 1.6.9)

BUNDLED WITH
2.1.4
38 changes: 34 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,41 @@ $ rails generate ledger_accountable
Include `LedgerAccountable` in any model to enable ledger accounting functionality.

```ruby
class YourModel < ApplicationRecord
class Order < ApplicationRecord
has_many :ledger_entries, as: :owner
has_many :order_items, dependent: :destroy
has_many :payments, dependent: :destroy
end

class OrderItem < ApplicationRecord
include LedgerAccountable

track_ledger :ledger_owner,
amount: :total,
type: :credit
belongs_to :order

# Track ledger changes on order with the cost method and provide a net_amount
# to dynamically compute net changes to its cost
track_ledger :order,
amount: :cost,
net_amount: :net_cost_change,
type: :debit
def cost
quantity * unit_price
end

private

def net_cost_change
cost - (quantity_was * unit_price_was)
end
end


class Payment < ApplicationRecord
include LedgerAccountable

belongs_to :order

# Track ledger changes on order with the amount attribute and mark it as a credit
track_ledger :order, amount: :amount, type: :credit
end
```
13 changes: 13 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

require 'bundler/gem_tasks'
require 'rake/testtask'
require 'rdoc/task'

desc 'Run Devise unit tests.'
Rake::TestTask.new(:test) do |t|
t.libs << 'lib'
t.libs << 'test'
t.pattern = 'test/**/*_test.rb'
t.verbose = true
t.warning = false
end
8 changes: 6 additions & 2 deletions ledger_accountable.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,19 @@ Gem::Specification.new do |spec|

spec.metadata['homepage_uri'] = spec.homepage
spec.metadata['source_code_uri'] = 'https://github.com/bmaclean/ledger-accountable'
spec.metadata['changelog_uri'] = 'https://github.com/bmaclean/ledger-accountable/CHANGELOG.md'
spec.metadata['changelog_uri'] = 'https://github.com/bmaclean/ledger-accountable/blob/main/CHANGELOG.md'

spec.files = Dir['{app,config,lib}/**/*', 'CHANGELOG.md', 'LICENSE', 'README.md']
spec.require_paths = ['lib']

spec.required_ruby_version = '>= 2.6.5'

# Runtime dependencies
spec.add_dependency 'activerecord', ">= 6.0.0", "< 7.1"
spec.add_dependency 'activesupport', ">= 6.0.0", "< 7.1"

# Development dependencies
spec.add_development_dependency 'rake', '~> 12.0'
spec.add_development_dependency 'rspec', '~> 3.0'
spec.add_development_dependency 'sqlite3', '= 1.6.9'
spec.add_development_dependency 'database_cleaner-active_record', '= 2.1.0'
end
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class LedgerEntry < ApplicationRecord
class LedgerEntry < ActiveRecord::Base
# used for internationalization via config/locales/ledger.en.yml
TRANSLATION_PREFIX = 'ledger'.freeze

Expand Down
Loading

0 comments on commit 1e7d56b

Please sign in to comment.