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 +4 -4
- data/app/models/debitcredit/account.rb +19 -3
- data/app/models/debitcredit/item.rb +4 -0
- data/app/models/debitcredit/transaction.rb +10 -6
- data/config/locales/en.yml +2 -0
- data/db/migrate/20140130095257_no_overdraft_by_default.rb +9 -0
- data/lib/debitcredit/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 15bf18b62ec5961c9bc1c4fef7717b386b5e9c4c
|
4
|
+
data.tar.gz: bd563f7e9ced320e24ec88182013e30159584796
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
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
|
@@ -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
|
-
|
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
|
data/config/locales/en.yml
CHANGED
data/lib/debitcredit/version.rb
CHANGED
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.
|
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-
|
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
|