ledger_accountable 0.0.4.pre → 0.0.6.pre

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: 4794daeb706e70aad2fee44a919d9e3662a90626de32c800f83f50cca233f292
4
- data.tar.gz: 0b9b5328f240de5a0f981ef4e9377116270c266a0ebf902e414d7530635f3065
3
+ metadata.gz: b93a1058b37c11487cfe062e040ef27d0a5f903a3d5b16507183cdc43fbcfded
4
+ data.tar.gz: 1a865fd912346f3930c08ae7cae387fdaeb31fb29ebb511563f2f7215857f1b2
5
5
  SHA512:
6
- metadata.gz: b0b64654c3e52db811b11a06c4448e7f8bc6b99aab84c9f3614dfb773f1211bbf9ca6030eca0795d979bbdbaf5f7ecb8c0611d7afede41d3106fa76f9376ef7e
7
- data.tar.gz: 3e04be4d764af038e55173a9389efcec50eeded14641014a4ae50de728560eafdcfecad286fb2a2804872f3169fa54a7f44b998db632a9e1b699cb536da2892a
6
+ metadata.gz: bbba69d3d13c3af56660b8ed27ff5eff1ba7df2ad8a2d478bf89e6815e167c314e8521b26b13a662ced307ae868cdc63ceb237c60ea281865b1d37aa75a36a48
7
+ data.tar.gz: c5312763c5e588b7328a70d732a408464bb7aa8945a5c8a87e262e92b809ca7936beba3eacdf19d1c3d3621d0be53f00d752d1921857517c46438786d41b62fe
data/README.md CHANGED
@@ -29,11 +29,41 @@ $ rails generate ledger_accountable
29
29
  Include `LedgerAccountable` in any model to enable ledger accounting functionality.
30
30
 
31
31
  ```ruby
32
- class YourModel < ApplicationRecord
32
+ class Order < ApplicationRecord
33
+ has_many :ledger_entries, as: :owner
34
+ has_many :order_items, dependent: :destroy
35
+ has_many :payments, dependent: :destroy
36
+ end
37
+
38
+ class OrderItem < ApplicationRecord
33
39
  include LedgerAccountable
34
40
 
35
- track_ledger :ledger_owner,
36
- amount: :total,
37
- type: :credit
41
+ belongs_to :order
42
+
43
+ # Track ledger changes on order with the cost method and provide a net_amount
44
+ # to dynamically compute net changes to its cost
45
+ track_ledger :order,
46
+ amount: :cost,
47
+ net_amount: :net_cost_change,
48
+ type: :debit
49
+ def cost
50
+ quantity * unit_price
51
+ end
52
+
53
+ private
54
+
55
+ def net_cost_change
56
+ cost - (quantity_was * unit_price_was)
57
+ end
58
+ end
59
+
60
+
61
+ class Payment < ApplicationRecord
62
+ include LedgerAccountable
63
+
64
+ belongs_to :order
65
+
66
+ # Track ledger changes on order with the amount attribute and mark it as a credit
67
+ track_ledger :order, amount: :amount, type: :credit
38
68
  end
39
69
  ```
@@ -1,4 +1,4 @@
1
- class LedgerEntry < ApplicationRecord
1
+ class LedgerEntry < ActiveRecord::Base
2
2
  # used for internationalization via config/locales/ledger.en.yml
3
3
  TRANSLATION_PREFIX = 'ledger'.freeze
4
4
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module LedgerAccountable
4
- VERSION = "0.0.4.pre".freeze
4
+ VERSION = "0.0.6.pre".freeze
5
5
  end
@@ -24,12 +24,21 @@
24
24
  #
25
25
  # belongs_to :order
26
26
  #
27
- # # Track ledger changes on order with the cost method and mark it as a debit
28
- # track_ledger :order, amount: :cost, type: :debit
29
- #
27
+ # # Track ledger changes on order with the cost method and provide a net_amount
28
+ # # to dynamically compute net changes to its cost
29
+ # track_ledger :order,
30
+ # amount: :cost,
31
+ # net_amount: :net_cost_change,
32
+ # type: :debit
30
33
  # def cost
31
34
  # quantity * unit_price
32
35
  # end
36
+ #
37
+ # private
38
+ #
39
+ # def net_cost_change
40
+ # cost - (quantity_was * unit_price_was)
41
+ # end
33
42
  # end
34
43
  #
35
44
  #
@@ -75,6 +84,8 @@ module LedgerAccountable
75
84
  class_attribute :ledger_owner
76
85
  # the name of the attribute or method which determines the ledger amount
77
86
  class_attribute :ledger_amount_attribute
87
+ # the name of the attribute or method which determines the ledger amount
88
+ class_attribute :ledger_net_amount_method
78
89
  # the type of ledger entry to create - debit or credit
79
90
  class_attribute :ledger_type
80
91
  # attributes of the LedgerAccountable that should trigger a ledger entry when changed
@@ -95,7 +106,7 @@ module LedgerAccountable
95
106
  def validate_and_assign_ledger_owner(ledger_owner)
96
107
  # verify that an instance of the LedgerAccountable model can respond to ledger_owner
97
108
  unless instance_methods.include?(ledger_owner)
98
- raise "LedgerAccountable model must respond to the provided value for ledger_owner (#{ledger_owner})."
109
+ raise "LedgerAccountable model #{model_name} must respond to the provided value for ledger_owner (#{ledger_owner})."
99
110
  end
100
111
 
101
112
  self.ledger_owner = ledger_owner
@@ -112,20 +123,17 @@ module LedgerAccountable
112
123
  end
113
124
 
114
125
  def validate_and_assign_ledger_amount_attribute(options)
115
- if options[:amount].present?
116
- raise 'LedgerAccountable amount must be an attribute or a method' if instance_methods.include?(options[:amount])
126
+ raise "track_ledger :amount is required in #{model_name}" unless options[:amount].present?
127
+ raise 'track_ledger :amount must be a symbol' unless options[:amount].is_a?(Symbol)
117
128
 
118
- self.ledger_amount_attribute = options[:amount]
119
- elsif instance_methods.include?(:amount)
120
- raise "LedgerAccountable amount must be specified on #{self.class.name}"
121
- end
129
+ self.ledger_amount_attribute = options[:amount]
122
130
  end
123
131
 
124
132
  def validate_net_amount_method(options)
125
- # TODO: should we be able to provide a method to calculate the net amount?
126
- return unless options[:net_amount].present? && !instance_methods.include?(options[:net_amount])
133
+ return unless options[:net_amount].present?
134
+ raise 'track_ledger :net_amount must be a symbol' unless options[:net_amount].is_a?(Symbol)
127
135
 
128
- raise 'LedgerAccountable net_amount must be a method'
136
+ self.ledger_net_amount_method = options[:net_amount]
129
137
  end
130
138
 
131
139
  def validate_and_assign_ledger_attributes(options)
@@ -150,19 +158,31 @@ module LedgerAccountable
150
158
  # LedgerAccountable object
151
159
  def ledger_amount
152
160
  unless respond_to?(self.class.ledger_amount_attribute)
153
- raise NotImplementedError, "Implement #{self.class.ledger_amount_attribute} in #{self.class.name}"
161
+ raise NotImplementedError,
162
+ "LedgerAccountable model '#{model_name}' specified #{self.class.ledger_amount_attribute} for track_ledger :amount, but does not implement #{self.class.ledger_amount_attribute}"
154
163
  end
155
164
 
156
- ledger_amount_multiplier = self.class.ledger_type == :debit ? 1 : -1
157
- ledger_amount_multiplier * (send(self.class.ledger_amount_attribute) || 0.0)
165
+ ledger_amount_multiplier = self.class.ledger_type == :credit ? 1 : -1
166
+ ledger_amount_multiplier * (send(self.class.ledger_amount_attribute) || 0)
158
167
  end
159
168
 
160
169
  # the amount to be recorded in the ledger entry on update; typically a net change to the dollar amount
161
170
  # stored on the LedgerAccountable object
162
171
  def net_ledger_amount
163
- previous_ledger_amount = attribute_was(self.class.ledger_amount_attribute)
164
- ledger_amount_multiplier = self.class.ledger_type == :debit ? 1 : -1
165
- ledger_amount_multiplier * (ledger_amount - (previous_ledger_amount || 0.0))
172
+ if self.class.ledger_net_amount_method
173
+ send(self.class.ledger_net_amount_method)
174
+ else
175
+ unless attribute_method?(self.class.ledger_amount_attribute.to_s)
176
+ # if a method is provided to compute ledger_amount,
177
+ logger.warn "
178
+ LedgerAccountable model '#{model_name}' appears to use a method for track_ledger :amount, \
179
+ but did not provide an option for :net_amount. This can lead to unexpected ledger entry amounts when modifying #{model_name}.
180
+ "
181
+ end
182
+ previous_ledger_amount = attribute_was(self.class.ledger_amount_attribute)
183
+ ledger_amount_multiplier = self.class.ledger_type == :credit ? 1 : -1
184
+ ledger_amount_multiplier * (ledger_amount - (previous_ledger_amount || 0))
185
+ end
166
186
  end
167
187
 
168
188
  private
@@ -233,7 +253,7 @@ module LedgerAccountable
233
253
  end
234
254
 
235
255
  def would_change_ledger_balance?
236
- net_ledger_amount&.to_f != 0.00
256
+ net_ledger_amount != 0
237
257
  end
238
258
 
239
259
  # create an :addition ledger entry with the full ledger amount
@@ -341,7 +361,7 @@ module LedgerAccountable
341
361
  # warning log if the ledger owner is not set - the LedgerAccountable model must
342
362
  # include track_ledger
343
363
  if self.class.ledger_owner.blank?
344
- Rails.logger.warn "LedgerAccountable model #{self.class.name} must include track_ledger to use ledger functionality"
364
+ Rails.logger.warn "LedgerAccountable model #{model_name} must include track_ledger to use ledger functionality"
345
365
  end
346
366
 
347
367
  self.class.ledger_owner.present?
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ledger_accountable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4.pre
4
+ version: 0.0.6.pre
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brendan Maclean
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2024-06-13 00:00:00.000000000 Z
12
+ date: 2024-06-14 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
@@ -31,6 +31,26 @@ dependencies:
31
31
  - - "<"
32
32
  - !ruby/object:Gem::Version
33
33
  version: '7.1'
34
+ - !ruby/object:Gem::Dependency
35
+ name: activesupport
36
+ requirement: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 6.0.0
41
+ - - "<"
42
+ - !ruby/object:Gem::Version
43
+ version: '7.1'
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: 6.0.0
51
+ - - "<"
52
+ - !ruby/object:Gem::Version
53
+ version: '7.1'
34
54
  - !ruby/object:Gem::Dependency
35
55
  name: rake
36
56
  requirement: !ruby/object:Gem::Requirement
@@ -46,19 +66,33 @@ dependencies:
46
66
  - !ruby/object:Gem::Version
47
67
  version: '12.0'
48
68
  - !ruby/object:Gem::Dependency
49
- name: rspec
69
+ name: sqlite3
50
70
  requirement: !ruby/object:Gem::Requirement
51
71
  requirements:
52
- - - "~>"
72
+ - - '='
53
73
  - !ruby/object:Gem::Version
54
- version: '3.0'
74
+ version: 1.6.9
55
75
  type: :development
56
76
  prerelease: false
57
77
  version_requirements: !ruby/object:Gem::Requirement
58
78
  requirements:
59
- - - "~>"
79
+ - - '='
80
+ - !ruby/object:Gem::Version
81
+ version: 1.6.9
82
+ - !ruby/object:Gem::Dependency
83
+ name: database_cleaner-active_record
84
+ requirement: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - '='
87
+ - !ruby/object:Gem::Version
88
+ version: 2.1.0
89
+ type: :development
90
+ prerelease: false
91
+ version_requirements: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - '='
60
94
  - !ruby/object:Gem::Version
61
- version: '3.0'
95
+ version: 2.1.0
62
96
  description: LedgerAccountable is a gem for recording ledger entries to store an accounting
63
97
  history in your Rails models.
64
98
  email:
@@ -82,7 +116,7 @@ licenses: []
82
116
  metadata:
83
117
  homepage_uri: https://github.com/bmaclean/ledger-accountable
84
118
  source_code_uri: https://github.com/bmaclean/ledger-accountable
85
- changelog_uri: https://github.com/bmaclean/ledger-accountable/CHANGELOG.md
119
+ changelog_uri: https://github.com/bmaclean/ledger-accountable/blob/main/CHANGELOG.md
86
120
  post_install_message:
87
121
  rdoc_options: []
88
122
  require_paths: