Skip to content
This repository was archived by the owner on Nov 17, 2018. It is now read-only.

Commit 22306a4

Browse files
committed
A few updates and fixessss
1 parent ba27874 commit 22306a4

File tree

3 files changed

+21
-20
lines changed

3 files changed

+21
-20
lines changed

app/assets/javascripts/oauth_balance.js.coffee

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ options =
1515

1616
processData: (d) -> d.map(([dateStr, balance]) ->
1717
date = new Date(dateStr)
18-
{c: [{v: date, f: prettyTime(date)}, {v: Number(balance)}]}
18+
{c: [{v: date, f: prettyTime(date)}, {v: Number(balance), f:"$"+Number(balance).toFixed(2)}]}
1919
)
2020

2121
optionsMainChart:

app/assets/javascripts/oauth_expenses.js.coffee

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ options =
1515

1616
processData: (d) -> d.map((e) ->
1717
date = new Date(e.date)
18-
{c: [{v: date, f: prettyTime(date)}, {v: Number(e.expense)}]}
18+
{c: [{v: date, f: prettyTime(date)}, {v: Number(e.expense), f:"$"+Number(e.expense).toFixed(2)}]}
1919
)
2020

2121
optionsMainChart:

app/models/user.rb

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def initialize access_token
1616

1717
['get_current_user', 'get_expenses'].each do |method|
1818
define_method method.to_sym do
19-
JSON.parse(@access_token.get(API_URL+method).body)
19+
JSON.parse(@access_token.get(API_URL+method+"?limit=250&visible=true").body)
2020
end
2121
end
2222

@@ -32,27 +32,28 @@ def each_expense_and_share &block
3232
id = get_current_user_id
3333
expenses = get_expenses['expenses']
3434
expenses.sortBy! do |expense|
35-
expense['updated_at']
35+
expense['date']
3636
end
3737
expenses.collect! do |expense|
3838
users = expense['users'].select do |share|
39-
next share['user']['id'] == id
39+
next (share['user'] and share['user']['id'] == id)
4040
end
4141
if users.length == 1
4242
share = users[0]
43+
block.call(expense, share)
4344
else
44-
throw "Found not one but #{users.length} users!"
45+
# TODO: handle expenses not involving the current user
46+
# throw "Found not one but #{users.length} users!"
4547
end
46-
block.call(expense, share)
4748
end
4849
end
4950

5051
def get_balance_over_time
5152
balance = 0
5253
result = []
5354
each_expense_and_share do |expense, share|
54-
balance += share['net_balance'].to_i
55-
result.push({'date' => expense['updated_at'], 'balance' => balance.to_s})
55+
balance += share['net_balance'].to_f
56+
result.push({'date' => expense['date'], 'balance' => balance.to_s})
5657
end
5758
result
5859
end
@@ -63,10 +64,10 @@ def get_balances_with_friends
6364
friend_id_to_name = {}
6465
balances = {}
6566
each_expense do |expense|
66-
if balances[expense['updated_at']]
67-
throw "I find a date in balances already set!"
67+
if balances[expense['date']]
68+
#throw "I find a date in balances already set!"
6869
end
69-
balances[expense['updated_at']] = []
70+
balances[expense['date']] = []
7071
expense['repayments'].each do |repayment|
7172
if repayment['from'] == id
7273
index = friend_ids.index(repayment['to'])
@@ -76,8 +77,8 @@ def get_balances_with_friends
7677
user['user_id'] == friend_ids[index]
7778
end.first['user']
7879
end
79-
balances[expense['updated_at']][index] ||= 0
80-
balances[expense['updated_at']][index] -= repayment['amount'].to_f
80+
balances[expense['date']][index] ||= 0
81+
balances[expense['date']][index] -= repayment['amount'].to_f
8182
elsif repayment['to'] == id
8283
index = friend_ids.index(repayment['from'])
8384
unless index
@@ -86,8 +87,8 @@ def get_balances_with_friends
8687
user['user_id'] == friend_ids[index]
8788
end.first['user']
8889
end
89-
balances[expense['updated_at']][index] ||= 0
90-
balances[expense['updated_at']][index] += repayment['amount'].to_f
90+
balances[expense['date']][index] ||= 0
91+
balances[expense['date']][index] += repayment['amount'].to_f
9192
end
9293
end
9394
end
@@ -133,7 +134,7 @@ def get_expenses_over_time
133134
expenses = []
134135
each_expense_and_share do |expense, share|
135136
expenses.push({
136-
"date" => expense['updated_at'],
137+
"date" => expense['date'],
137138
"expense" => share['owed_share']
138139
})
139140
end
@@ -157,7 +158,7 @@ def get_expenses_over_time_by_category #Exports in the form {categories: ["foo",
157158
rows = []
158159
each_expense_and_share do |expense, share|
159160
rows.push({
160-
'date' => expense['updated_at'],
161+
'date' => expense['date'],
161162
expense['category']['name'] => share['owed_share']
162163
})
163164
categoryHash[expense['category']['name']] = true
@@ -194,7 +195,7 @@ def get_expenses_matching query
194195
(expenses['description'] + ' ' + expenses['category']['name']).match(q)
195196
}.length == processed_query.length
196197
expenses.push({
197-
"date" => expense['updated_at'],
198+
"date" => expense['date'],
198199
"expense" => share['owed_share']
199200
})
200201
end
@@ -254,7 +255,7 @@ def self.get_net_balances_over_time access_token
254255
expenses = []
255256
each_expense_and_share do |expense, share|
256257
expenses.push({
257-
"date" => expense['updated_at'],
258+
"date" => expense['date'],
258259
"net_balance" => share['net_balance']
259260
})
260261
end

0 commit comments

Comments
 (0)