debitcredit 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f5c9ca7c15a9a0033ccdf7dc8ba48be7c2235f09
4
- data.tar.gz: 639c675e7fa7448a3dae30abd464249af7dffafc
3
+ metadata.gz: 15bf18b62ec5961c9bc1c4fef7717b386b5e9c4c
4
+ data.tar.gz: bd563f7e9ced320e24ec88182013e30159584796
5
5
  SHA512:
6
- metadata.gz: 9eabc70957e8da1b80fd373f3b9626af95a56b35f4c9245695c7558ea18d2b9318a1a8a7eb9c4c488573124d83cf32e6d3c4635be4354615a384128d2a98c818
7
- data.tar.gz: 1fbde6b1603f9e3768d316636c249d78aa5795e39131164007a71270c32434d03a588c2216a0559321479cee70c5244ff459a0c686cfca2e4414f5facaa5e337
6
+ metadata.gz: 15eaadb79408a7768a7ce639d300dfb6eb4a811a7b6e367e77facc8d5053ea829c3e3409ce32143e19e93afa447c01b264d7c991295757753bb96439ab78434b
7
+ data.tar.gz: e2c2685de2c3cd0b7aa92415fb5195fc3783bea859b3f1dd1991c0ad679658cfb99391f7e4fe4de61e49b42c684a9f2f4e8934ab25e9996d22503cf46ac7f5d9
@@ -4,8 +4,7 @@ module Debitcredit
4
4
  has_many :items, dependent: :destroy
5
5
 
6
6
  validates :name, :balance, presence: true
7
-
8
- validates :balance, numericality: {greater_than_or_equal_to: 0}, unless: :overdraft_enabled?
7
+ validate :prevent_overdraft, unless: :overdraft_enabled?
9
8
 
10
9
  scope :asset, ->{where(type: AssetAccount.name)}
11
10
  scope :equity, ->{where(type: EquityAccount.name)}
@@ -36,9 +35,26 @@ module Debitcredit
36
35
  end
37
36
  end
38
37
 
39
- def update_balance!(item)
38
+ attr_accessor :check_overdraft
39
+ def update_balance!(item, check_overdraft = true)
40
40
  item.balance = send(item.kind, item.amount)
41
+ self.check_overdraft = check_overdraft
41
42
  save!
42
43
  end
44
+
45
+ def overdraft?
46
+ balance < 0
47
+ end
48
+
49
+ protected
50
+
51
+ def prevent_overdraft
52
+ return unless balance_changed?
53
+ return unless check_overdraft
54
+ return unless balance < 0
55
+ return unless balance_was > balance
56
+
57
+ errors.add(:balance, :overdraft)
58
+ end
43
59
  end
44
60
  end
@@ -20,5 +20,9 @@ module Debitcredit
20
20
  def kind
21
21
  debit?? :debit : :credit
22
22
  end
23
+
24
+ def inverse
25
+ self.class.new account: account, transaction: transaction, amount: amount, debit: credit?
26
+ end
23
27
  end
24
28
  end
@@ -10,32 +10,36 @@ module Debitcredit
10
10
 
11
11
  before_create :lock_and_update_balances
12
12
 
13
- # XXX
14
- # prevent items and balance changes on update
15
- # prevent update at all?
16
-
17
13
  def self.prepare(opts = {}, &block)
18
14
  new(opts).tap do |t|
19
15
  Docile.dsl_eval(DSL.new(t), &block)
20
16
  end
21
17
  end
22
18
 
19
+ attr_accessor :ignore_overdraft
20
+ def inverse(opts = {})
21
+ self.class.new({ignore_overdraft: true}.merge(opts)) do |res|
22
+ res.items = items.map(&:inverse)
23
+ end
24
+ end
25
+
23
26
  def items_balance
24
27
  items.map(&:value_for_balance).sum
25
28
  end
26
29
 
27
30
  def balanced?
28
- 0 == items_balance
31
+ items_balance.zero?
29
32
  end
30
33
 
31
34
  protected
35
+
32
36
  def ensure_balanced
33
37
  errors.add(:base, :unbalanced) unless balanced?
34
38
  end
35
39
 
36
40
  def lock_and_update_balances
37
41
  items.sort_by(&:account_id).each do |item|
38
- item.account.lock!.update_balance!(item)
42
+ item.account.lock!.update_balance!(item, !ignore_overdraft)
39
43
  end
40
44
  end
41
45
  end
@@ -5,4 +5,6 @@ en:
5
5
  models:
6
6
  debitcredit/transaction:
7
7
  unbalanced: item amounts must balance to 0
8
+ debitcredit/account:
9
+ overdraft: overdraft is not allowed
8
10
 
@@ -0,0 +1,9 @@
1
+ class NoOverdraftByDefault < ActiveRecord::Migration
2
+ def up
3
+ change_column_default :debitcredit_accounts, :overdraft_enabled, false
4
+ end
5
+
6
+ def down
7
+ change_column_default :debitcredit_accounts, :overdraft_enabled, true
8
+ end
9
+ end
@@ -1,3 +1,3 @@
1
1
  module Debitcredit
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: debitcredit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vitaly Kushner
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-01-28 00:00:00.000000000 Z
11
+ date: 2014-01-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -203,6 +203,7 @@ files:
203
203
  - db/migrate/20140128131859_add_transaction_parent.rb
204
204
  - db/migrate/20140128134203_longer_account_name.rb
205
205
  - db/migrate/20140128153104_add_account_overdraft.rb
206
+ - db/migrate/20140130095257_no_overdraft_by_default.rb
206
207
  - lib/debitcredit/engine.rb
207
208
  - lib/debitcredit/version.rb
208
209
  - lib/debitcredit.rb