double_double 0.2.5 → 0.2.6
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.
- data/.travis.yml +1 -2
- data/lib/double_double/account.rb +3 -2
- data/lib/double_double/amount.rb +3 -2
- data/lib/double_double/version.rb +1 -1
- data/spec/models/transaction_spec.rb +39 -0
- metadata +2 -2
data/.travis.yml
CHANGED
@@ -79,8 +79,9 @@ module DoubleDouble
|
|
79
79
|
def side_balance(is_debit, hash)
|
80
80
|
a = is_debit ? DoubleDouble::DebitAmount.scoped : DoubleDouble::CreditAmount.scoped
|
81
81
|
a = a.where(account_id: self.id)
|
82
|
-
a = a.by_context(hash[:context])
|
83
|
-
a = a.by_accountee(hash[:accountee])
|
82
|
+
a = a.by_context(hash[:context]) if hash.has_key? :context
|
83
|
+
a = a.by_accountee(hash[:accountee]) if hash.has_key? :accountee
|
84
|
+
a = a.by_transaction_type(hash[:transaction_type]) if hash.has_key? :transaction_type
|
84
85
|
Money.new(a.sum(:amount_cents))
|
85
86
|
end
|
86
87
|
# The balance method that derived Accounts utilize.
|
data/lib/double_double/amount.rb
CHANGED
@@ -14,8 +14,9 @@ module DoubleDouble
|
|
14
14
|
belongs_to :accountee, polymorphic: true
|
15
15
|
belongs_to :context, polymorphic: true
|
16
16
|
|
17
|
-
scope :by_accountee,
|
18
|
-
scope :by_context,
|
17
|
+
scope :by_accountee, ->(a) { where(accountee_id: a.id, accountee_type: a.class.base_class) }
|
18
|
+
scope :by_context, ->(c) { where(context_id: c.id, context_type: c.class.base_class) }
|
19
|
+
scope :by_transaction_type, ->(t) { joins(:transaction).where(double_double_transactions: {transaction_type_id: t}) }
|
19
20
|
|
20
21
|
validates_presence_of :type, :transaction, :account
|
21
22
|
validates :amount_cents, numericality: {greater_than: 0}
|
@@ -114,6 +114,22 @@ module DoubleDouble
|
|
114
114
|
t.errors['base'].should == ['The credit and debit amounts are not equal']
|
115
115
|
end
|
116
116
|
|
117
|
+
describe 'transaction reversing' do
|
118
|
+
it "should negate the same non-reversed transaction" do
|
119
|
+
args_normal = {description: 'reverse test',
|
120
|
+
debits: [{account: 'Cash_11', amount: 10}],
|
121
|
+
credits: [{account: 'Loan_12', amount: 9},
|
122
|
+
{account: 'Loan_12', amount: 1}]}
|
123
|
+
args_reversed = args_normal.merge({reversed: true})
|
124
|
+
Transaction.create!(args_normal)
|
125
|
+
Account.named('Cash_11').balance.should eq(10)
|
126
|
+
Account.named('Loan_12').balance.should eq(10)
|
127
|
+
Transaction.create!(args_reversed)
|
128
|
+
Account.named('Cash_11').balance.should eq(0)
|
129
|
+
Account.named('Loan_12').balance.should eq(0)
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
117
133
|
describe 'transaction_types' do
|
118
134
|
it 'should create a Transaction with a TransactionType of Unassigned if none is passed in' do
|
119
135
|
t = Transaction.build(
|
@@ -136,6 +152,29 @@ module DoubleDouble
|
|
136
152
|
end
|
137
153
|
end
|
138
154
|
|
155
|
+
describe 'transaction_types, when multiple types exist together' do
|
156
|
+
it 'should segment based on transaction type' do
|
157
|
+
DoubleDouble::Liability.create!(name:'hotdogs', number: 1015)
|
158
|
+
DoubleDouble::Liability.create!(name:'junk', number: 1016)
|
159
|
+
TransactionType.create!(description: 'ketchup')
|
160
|
+
TransactionType.create!(description: 'onions')
|
161
|
+
Transaction.create!(
|
162
|
+
description: 'processed ketchup',
|
163
|
+
transaction_type: TransactionType.of(:ketchup),
|
164
|
+
debits: [{account: 'junk', amount: 60, context: @campaign1, accountee: @user1}],
|
165
|
+
credits: [{account: 'hotdogs', amount: 60, context: @campaign1, accountee: @user1}])
|
166
|
+
DoubleDouble::Account.named('hotdogs').credits_balance({context: @campaign1, accountee: @user1, transaction_type: TransactionType.of(:ketchup)}).should == 60
|
167
|
+
DoubleDouble::Account.named('hotdogs').credits_balance({context: @campaign1, accountee: @user1, transaction_type: TransactionType.of(:onions)}).should == 0
|
168
|
+
Transaction.create!(
|
169
|
+
description: 'processed onions',
|
170
|
+
transaction_type: TransactionType.of(:onions),
|
171
|
+
debits: [{account: 'junk', amount: 5, context: @campaign1, accountee: @user1}],
|
172
|
+
credits: [{account: 'hotdogs', amount: 5, context: @campaign1, accountee: @user1}])
|
173
|
+
DoubleDouble::Account.named('hotdogs').credits_balance({context: @campaign1, accountee: @user1, transaction_type: TransactionType.of(:ketchup)}).should == 60
|
174
|
+
DoubleDouble::Account.named('hotdogs').credits_balance({context: @campaign1, accountee: @user1, transaction_type: TransactionType.of(:onions)}).should == 5
|
175
|
+
end
|
176
|
+
end
|
177
|
+
|
139
178
|
describe 'amount accountee references' do
|
140
179
|
it 'should allow a Transaction to be built describing the accountee in the hash' do
|
141
180
|
Transaction.create!(
|
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.6
|
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-12 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: money
|