double_double 0.2.6 → 0.2.7
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +3 -0
- data/lib/double_double/account.rb +1 -0
- data/lib/double_double/amount.rb +5 -3
- data/lib/double_double/transaction.rb +3 -2
- data/lib/double_double/version.rb +1 -1
- data/spec/models/credit_amount_spec.rb +32 -11
- data/spec/models/debit_amount_spec.rb +31 -11
- data/spec/spec_helper.rb +3 -0
- metadata +2 -2
data/README.md
CHANGED
@@ -60,6 +60,7 @@ class CreateDoubleDouble < ActiveRecord::Migration
|
|
60
60
|
t.references :account
|
61
61
|
t.references :transaction
|
62
62
|
t.references :context, polymorphic: true
|
63
|
+
t.references :subcontext, polymorphic: true
|
63
64
|
t.references :accountee, polymorphic: true
|
64
65
|
|
65
66
|
t.integer :amount_cents, limit: 8, default: 0, null: false
|
@@ -67,6 +68,8 @@ class CreateDoubleDouble < ActiveRecord::Migration
|
|
67
68
|
end
|
68
69
|
add_index :double_double_amounts, :context_id
|
69
70
|
add_index :double_double_amounts, :context_type
|
71
|
+
add_index :double_double_amounts, :subcontext_id
|
72
|
+
add_index :double_double_amounts, :subcontext_type
|
70
73
|
add_index :double_double_amounts, :accountee_id
|
71
74
|
add_index :double_double_amounts, :accountee_type
|
72
75
|
add_index :double_double_amounts, :type
|
@@ -80,6 +80,7 @@ module DoubleDouble
|
|
80
80
|
a = is_debit ? DoubleDouble::DebitAmount.scoped : DoubleDouble::CreditAmount.scoped
|
81
81
|
a = a.where(account_id: self.id)
|
82
82
|
a = a.by_context(hash[:context]) if hash.has_key? :context
|
83
|
+
a = a.by_subcontext(hash[:subcontext]) if hash.has_key? :subcontext
|
83
84
|
a = a.by_accountee(hash[:accountee]) if hash.has_key? :accountee
|
84
85
|
a = a.by_transaction_type(hash[:transaction_type]) if hash.has_key? :transaction_type
|
85
86
|
Money.new(a.sum(:amount_cents))
|
data/lib/double_double/amount.rb
CHANGED
@@ -7,15 +7,17 @@ module DoubleDouble
|
|
7
7
|
class Amount < ActiveRecord::Base
|
8
8
|
self.table_name = 'double_double_amounts'
|
9
9
|
|
10
|
-
attr_accessible :account, :amount, :transaction, :context, :accountee, as: :transation_builder
|
10
|
+
attr_accessible :account, :amount, :transaction, :context, :subcontext, :accountee, as: :transation_builder
|
11
11
|
|
12
12
|
belongs_to :transaction
|
13
13
|
belongs_to :account
|
14
14
|
belongs_to :accountee, polymorphic: true
|
15
15
|
belongs_to :context, polymorphic: true
|
16
|
+
belongs_to :subcontext, polymorphic: true
|
16
17
|
|
17
|
-
scope :by_accountee, ->(a) { where(accountee_id:
|
18
|
-
scope :by_context, ->(c) { where(context_id:
|
18
|
+
scope :by_accountee, ->(a) { where(accountee_id: a.id, accountee_type: a.class.base_class) }
|
19
|
+
scope :by_context, ->(c) { where(context_id: c.id, context_type: c.class.base_class) }
|
20
|
+
scope :by_subcontext, ->(s) { where(subcontext_id: s.id, subcontext_type: s.class.base_class) }
|
19
21
|
scope :by_transaction_type, ->(t) { joins(:transaction).where(double_double_transactions: {transaction_type_id: t}) }
|
20
22
|
|
21
23
|
validates_presence_of :type, :transaction, :account
|
@@ -110,8 +110,9 @@ module DoubleDouble
|
|
110
110
|
|
111
111
|
def self.prepare_amount_parameters args
|
112
112
|
prepared_params = { account: Account.named(args[:account]), transaction: args[:transaction], amount: args[:amount]}
|
113
|
-
prepared_params.merge!({accountee:
|
114
|
-
prepared_params.merge!({context:
|
113
|
+
prepared_params.merge!({accountee: args[:accountee]}) if args.has_key? :accountee
|
114
|
+
prepared_params.merge!({context: args[:context]}) if args.has_key? :context
|
115
|
+
prepared_params.merge!({subcontext: args[:subcontext]}) if args.has_key? :subcontext
|
115
116
|
prepared_params
|
116
117
|
end
|
117
118
|
end
|
@@ -7,6 +7,8 @@ module DoubleDouble
|
|
7
7
|
@dummy_transaction = DoubleDouble::Transaction.new
|
8
8
|
@job = DoubleDouble::Expense.create!(name: 'stand-in job', number: 999)
|
9
9
|
@po = DoubleDouble::Expense.create!(name: 'stand-in purchase order', number: 333)
|
10
|
+
@item_foo = DoubleDouble::Expense.create!(name: 'stand-in item_foo', number: 1000)
|
11
|
+
@item_bar = DoubleDouble::Expense.create!(name: 'stand-in item_bar', number: 1001)
|
10
12
|
end
|
11
13
|
|
12
14
|
it "should not be valid without an amount" do
|
@@ -46,23 +48,42 @@ module DoubleDouble
|
|
46
48
|
it "should be sensitive to 'context' when calculating balances, if supplied" do
|
47
49
|
Transaction.create!(
|
48
50
|
description: 'Foobar1',
|
49
|
-
debits: [{account: 'Cash', amount:
|
50
|
-
credits: [{account: 'Loan', amount:
|
51
|
+
debits: [{account: 'Cash', amount: 123}],
|
52
|
+
credits: [{account: 'Loan', amount: 123, context: @job}])
|
51
53
|
Transaction.create!(
|
52
54
|
description: 'Foobar2',
|
53
|
-
debits: [{account: 'Cash', amount:
|
54
|
-
credits: [{account: 'Loan', amount:
|
55
|
+
debits: [{account: 'Cash', amount: 321}],
|
56
|
+
credits: [{account: 'Loan', amount: 321, context: @job}])
|
55
57
|
Transaction.create!(
|
56
58
|
description: 'Foobar3',
|
57
|
-
debits: [{account: 'Cash', amount:
|
58
|
-
credits: [{account: 'Loan', amount:
|
59
|
+
debits: [{account: 'Cash', amount: 275}],
|
60
|
+
credits: [{account: 'Loan', amount: 275, context: @po}])
|
59
61
|
Transaction.create!(
|
60
62
|
description: 'Foobar4',
|
61
|
-
debits: [{account: 'Cash', amount:
|
62
|
-
credits: [{account: 'Loan', amount:
|
63
|
-
@loan.credits_balance({context: @job}).should ==
|
64
|
-
@loan.credits_balance({context: @po}).should ==
|
65
|
-
@loan.credits_balance.should ==
|
63
|
+
debits: [{account: 'Cash', amount: 999}],
|
64
|
+
credits: [{account: 'Loan', amount: 999}])
|
65
|
+
@loan.credits_balance({context: @job}).should == 123 + 321
|
66
|
+
@loan.credits_balance({context: @po}).should == 275
|
67
|
+
@loan.credits_balance.should == 123 + 321 + 275 + 999
|
68
|
+
Transaction.create!(
|
69
|
+
description: 'Foobar5',
|
70
|
+
debits: [{account: 'Cash', amount: 9_999}],
|
71
|
+
credits: [{account: 'Loan', amount: 9_999, context: @job, subcontext: @item_foo}])
|
72
|
+
Transaction.create!(
|
73
|
+
description: 'Foobar5',
|
74
|
+
debits: [{account: 'Cash', amount: 123}],
|
75
|
+
credits: [{account: 'Loan', amount: 123, context: @po, subcontext: @item_foo}])
|
76
|
+
Transaction.create!(
|
77
|
+
description: 'Foobar6',
|
78
|
+
debits: [{account: 'Cash', amount: 222}],
|
79
|
+
credits: [{account: 'Loan', amount: 222, context: @po, subcontext: @item_foo}])
|
80
|
+
Transaction.create!(
|
81
|
+
description: 'Foobar7',
|
82
|
+
debits: [{account: 'Cash', amount: 1}],
|
83
|
+
credits: [{account: 'Loan', amount: 1, context: @po, subcontext: @item_bar}])
|
84
|
+
@loan.credits_balance({context: @po, subcontext: @item_foo}).should == 123 + 222
|
85
|
+
@loan.credits_balance({context: @po, subcontext: @item_bar}).should == 1
|
86
|
+
|
66
87
|
Account.trial_balance.should eq(0)
|
67
88
|
end
|
68
89
|
end
|
@@ -7,6 +7,8 @@ module DoubleDouble
|
|
7
7
|
@dummy_transaction = DoubleDouble::Transaction.new
|
8
8
|
@job = DoubleDouble::Expense.create!(name: 'stand-in job', number: 999)
|
9
9
|
@po = DoubleDouble::Expense.create!(name: 'stand-in purchase order', number: 333)
|
10
|
+
@item_foo = DoubleDouble::Expense.create!(name: 'stand-in item_foo', number: 1000)
|
11
|
+
@item_bar = DoubleDouble::Expense.create!(name: 'stand-in item_bar', number: 1001)
|
10
12
|
end
|
11
13
|
|
12
14
|
it "should not be valid without an amount" do
|
@@ -46,23 +48,41 @@ module DoubleDouble
|
|
46
48
|
it "should be sensitive to 'context' when calculating balances, if supplied" do
|
47
49
|
Transaction.create!(
|
48
50
|
description: 'Foobar1',
|
49
|
-
debits: [{account: 'Cash', amount:
|
50
|
-
credits: [{account: 'Loan', amount:
|
51
|
+
debits: [{account: 'Cash', amount: 123, context: @job}],
|
52
|
+
credits: [{account: 'Loan', amount: 123}])
|
51
53
|
Transaction.create!(
|
52
54
|
description: 'Foobar2',
|
53
|
-
debits: [{account: 'Cash', amount:
|
54
|
-
credits: [{account: 'Loan', amount:
|
55
|
+
debits: [{account: 'Cash', amount: 321, context: @job}],
|
56
|
+
credits: [{account: 'Loan', amount: 321}])
|
55
57
|
Transaction.create!(
|
56
58
|
description: 'Foobar3',
|
57
|
-
debits: [{account: 'Cash', amount:
|
58
|
-
credits: [{account: 'Loan', amount:
|
59
|
+
debits: [{account: 'Cash', amount: 275, context: @po}],
|
60
|
+
credits: [{account: 'Loan', amount: 275}])
|
59
61
|
Transaction.create!(
|
60
62
|
description: 'Foobar4',
|
61
|
-
debits: [{account: 'Cash', amount:
|
62
|
-
credits: [{account: 'Loan', amount:
|
63
|
-
@cash.debits_balance({context: @job}).should ==
|
64
|
-
@cash.debits_balance({context: @po}).should ==
|
65
|
-
@cash.debits_balance.should ==
|
63
|
+
debits: [{account: 'Cash', amount: 999}],
|
64
|
+
credits: [{account: 'Loan', amount: 999}])
|
65
|
+
@cash.debits_balance({context: @job}).should == 123 + 321
|
66
|
+
@cash.debits_balance({context: @po}).should == 275
|
67
|
+
@cash.debits_balance.should == 123 + 321 + 275 + 999
|
68
|
+
Transaction.create!(
|
69
|
+
description: 'Foobar5',
|
70
|
+
debits: [{account: 'Cash', amount: 9_999, context: @job, subcontext: @item_foo}],
|
71
|
+
credits: [{account: 'Loan', amount: 9_999}])
|
72
|
+
Transaction.create!(
|
73
|
+
description: 'Foobar5',
|
74
|
+
debits: [{account: 'Cash', amount: 123, context: @po, subcontext: @item_foo}],
|
75
|
+
credits: [{account: 'Loan', amount: 123}])
|
76
|
+
Transaction.create!(
|
77
|
+
description: 'Foobar6',
|
78
|
+
debits: [{account: 'Cash', amount: 222, context: @po, subcontext: @item_foo}],
|
79
|
+
credits: [{account: 'Loan', amount: 222}])
|
80
|
+
Transaction.create!(
|
81
|
+
description: 'Foobar7',
|
82
|
+
debits: [{account: 'Cash', amount: 1, context: @po, subcontext: @item_bar}],
|
83
|
+
credits: [{account: 'Loan', amount: 1}])
|
84
|
+
@cash.debits_balance({context: @po, subcontext: @item_foo}).should == 123 + 222
|
85
|
+
@cash.debits_balance({context: @po, subcontext: @item_bar}).should == 1
|
66
86
|
Account.trial_balance.should eq(0)
|
67
87
|
end
|
68
88
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -37,6 +37,7 @@ ActiveRecord::Migration.verbose = false
|
|
37
37
|
t.references :account
|
38
38
|
t.references :transaction
|
39
39
|
t.references :context, polymorphic: true
|
40
|
+
t.references :subcontext, polymorphic: true
|
40
41
|
t.references :accountee, polymorphic: true
|
41
42
|
|
42
43
|
t.integer :amount_cents, limit: 8, default: 0, null: false
|
@@ -44,6 +45,8 @@ ActiveRecord::Migration.verbose = false
|
|
44
45
|
end
|
45
46
|
add_index :double_double_amounts, :context_id
|
46
47
|
add_index :double_double_amounts, :context_type
|
48
|
+
add_index :double_double_amounts, :subcontext_id
|
49
|
+
add_index :double_double_amounts, :subcontext_type
|
47
50
|
add_index :double_double_amounts, :accountee_id
|
48
51
|
add_index :double_double_amounts, :accountee_type
|
49
52
|
add_index :double_double_amounts, :type
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: double_double
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.7
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-02-
|
12
|
+
date: 2013-02-13 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: money
|