Permalink
Browse files

Renamed Days to be Balances, and changed the field on that table from…

… balance to amount. In preparation for there to be arbitrarily timed balances
  • Loading branch information...
1 parent a82257e commit a46cc4df462b4e94a2e1db4b5f4aace2c04ff6b9 @konklone konklone committed Mar 24, 2010
Showing with 167 additions and 91 deletions.
  1. +12 −9 Rakefile
  2. +15 −16 charting.rb
  3. +1 −1 environment.rb
  4. +20 −20 fixtures/{days.yml → balances.yml}
  5. +16 −0 migrations/03_create_balances.rb
  6. +0 −14 migrations/03_create_days.rb
  7. +8 −8 models.rb
  8. +34 −1 ohnomymoney.rb
  9. +61 −22 views/index.erb
View
@@ -1,3 +1,6 @@
+require 'logger'
+
+
desc "Update all accounts"
task :update => :environment do
require 'updater/updater'
@@ -8,33 +11,33 @@ task :update => :environment do
end
-
namespace :fixtures do
desc "Load all fixtures, or one model's"
task :load => :environment do
- models = ENV['model'] ? [ENV['model'].singularize.camelize.constantize] : all_models
- models.each {|model| restore_fixture model}
+ fixtures = ENV['model'] ? [ENV['model']] : all_fixtures
+ fixtures.each {|fixture| restore_fixture fixture.singularize.camelize.constantize}
end
desc "Dump all models into fixtures, or one model"
task :dump => :environment do
- models = ENV['model'] ? [ENV['model'].singularize.camelize.constantize] : all_models
- models.each {|model| dump_fixture model}
+ fixtures = ENV['model'] ? [ENV['model']] : all_fixtures
+ fixtures.each {|fixture| dump_fixture fixture.singularize.camelize.constantize}
+ end
+
+ def all_fixtures
+ Dir.glob("fixtures/*.yml").map {|f| File.basename(f, ".yml")}
end
end
+
desc 'Migrate the database'
task :migrate => :environment do
ActiveRecord::Base.logger = Logger.new STDOUT
ActiveRecord::Migrator.migrate 'migrations', (ENV['version'] ? ENV['version'].to_i : nil)
end
-def all_models
- [User, Account, Day]
-end
-
desc 'Loads environment'
task :environment do
require 'ohnomymoney'
View
@@ -2,13 +2,13 @@
helpers do
- def chart_for(days)
- oldest = days.first
- newest = days[days.size-1]
- balances = days.map {|day| day.balance}
+ def chart_for(balances)
+ oldest = balances.first
+ newest = balances[balances.size-1]
+ amounts = balances.map {|day| day.amount}
- min = balances.min
- max = balances.max
+ min = amounts.min
+ max = amounts.max
disparity = max - min
y = (disparity * 1.66).to_i
buffer = (y - disparity) / 2
@@ -18,19 +18,19 @@ def chart_for(days)
y = y_max - y_min
y_step = round(y / 10.0, 2)
- x_step = days.size / 7
+ x_step = balances.size / 7
x_step = 1 if x_step < 1
- x_days = []
- days.each_with_index {|b, i| x_days << b if i % x_step == 0}
- x_days[0] = days[0]
- x_days[-1] = days[-1]
+ x_balances = []
+ balances.each_with_index {|b, i| x_balances << b if i % x_step == 0}
+ x_balances[0] = balances[0]
+ x_balances[-1] = balances[-1]
y_labels = (0..10).map {|i| format_balance(y_min + (y_step * i))}
- x_labels = x_days.map {|day| format_date day.date_of}
+ x_labels = x_balances.map {|day| format_date day.date_of}
- label = newest.balance > 0 ? 'Money' : 'Debt'
- data = balances.map {|balance| (balance - y_min).to_f}
- color = newest.balance > 0 ? '008800' : 'cc0000'
+ label = newest.amount > 0 ? 'Money' : 'Debt'
+ data = amounts.map {|amount| (amount - y_min).to_f}
+ color = newest.amount > 0 ? '008800' : 'cc0000'
chart = GoogleChart::LineChart.new '857x350'
chart.data_encoding = :text
@@ -40,7 +40,6 @@ def chart_for(days)
chart.axis :y, :labels => y_labels
chart.axis :right, :labels => y_labels
- p chart.to_url(:chdlp => 'b')
chart.to_url :chdlp => 'b'
end
View
@@ -1,6 +1,6 @@
require 'rubygems'
-require 'activerecord'
+require 'active_record'
require 'models'
configure do
@@ -5,137 +5,137 @@
id: 3
user_id: 1
date_of: 2010-02-12
- balance: 100000
+ amount: 100000
- created_at: 2010-02-15 02:44:33 -05:00
updated_at: 2010-02-15 02:44:33 -05:00
account_id: 2
id: 4
user_id: 1
date_of: 2010-02-13
- balance: 200000
+ amount: 200000
- created_at: 2010-02-15 02:44:40 -05:00
updated_at: 2010-02-15 02:44:40 -05:00
account_id: 2
id: 5
user_id: 1
date_of: 2010-02-14
- balance: 400000
+ amount: 400000
- created_at: 2010-02-15 02:44:48 -05:00
updated_at: 2010-02-15 02:44:48 -05:00
account_id: 2
id: 6
user_id: 1
date_of: 2010-02-15
- balance: 300000
+ amount: 300000
- created_at: 2010-02-15 02:45:14 -05:00
updated_at: 2010-02-15 02:45:14 -05:00
account_id: 3
id: 7
user_id: 1
date_of: 2010-02-12
- balance: 0
+ amount: 0
- created_at: 2010-02-15 02:45:26 -05:00
updated_at: 2010-02-15 02:45:26 -05:00
account_id: 3
id: 8
user_id: 1
date_of: 2010-02-13
- balance: 50000
+ amount: 50000
- created_at: 2010-02-15 02:45:33 -05:00
updated_at: 2010-02-15 02:45:33 -05:00
account_id: 3
id: 9
user_id: 1
date_of: 2010-02-14
- balance: 100000
+ amount: 100000
- created_at: 2010-02-15 02:45:38 -05:00
updated_at: 2010-02-15 02:45:38 -05:00
account_id: 3
id: 10
user_id: 1
date_of: 2010-02-15
- balance: 150000
+ amount: 150000
- created_at: 2010-02-15 02:45:50 -05:00
updated_at: 2010-02-15 02:45:50 -05:00
account_id: 4
id: 11
user_id: 1
date_of: 2010-02-12
- balance: -300000
+ amount: -300000
- created_at: 2010-02-15 02:49:12 -05:00
updated_at: 2010-02-15 02:49:12 -05:00
account_id: 4
id: 12
user_id: 1
date_of: 2010-02-13
- balance: -200000
+ amount: -200000
- created_at: 2010-02-15 02:49:17 -05:00
updated_at: 2010-02-15 02:49:17 -05:00
account_id: 4
id: 13
user_id: 1
date_of: 2010-02-14
- balance: -300000
+ amount: -300000
- created_at: 2010-02-15 02:49:19 -05:00
updated_at: 2010-02-15 02:49:19 -05:00
account_id: 4
id: 14
user_id: 1
date_of: 2010-02-15
- balance: -300000
+ amount: -300000
- created_at: 2010-02-15 02:49:32 -05:00
updated_at: 2010-02-15 02:49:32 -05:00
account_id: 5
id: 15
user_id: 1
date_of: 2010-02-12
- balance: -2000000
+ amount: -2000000
- created_at: 2010-02-15 02:49:42 -05:00
updated_at: 2010-02-15 02:49:42 -05:00
account_id: 5
id: 16
user_id: 1
date_of: 2010-02-13
- balance: -1900000
+ amount: -1900000
- created_at: 2010-02-15 02:49:49 -05:00
updated_at: 2010-02-15 02:49:49 -05:00
account_id: 5
id: 17
user_id: 1
date_of: 2010-02-14
- balance: -1800000
+ amount: -1800000
- created_at: 2010-02-15 02:49:54 -05:00
updated_at: 2010-02-15 02:49:54 -05:00
account_id: 5
id: 18
user_id: 1
date_of: 2010-02-15
- balance: -800000
+ amount: -800000
- created_at: 2010-02-15 03:04:25 -05:00
updated_at: 2010-02-15 03:04:25 -05:00
account_id: 1
id: 19
user_id: 1
date_of: 2010-02-12
- balance: -2200000
+ amount: -2200000
- created_at: 2010-02-15 03:04:42 -05:00
updated_at: 2010-02-15 03:04:42 -05:00
account_id: 1
id: 20
user_id: 1
date_of: 2010-02-13
- balance: -1850000
+ amount: -1850000
- created_at: 2010-02-15 03:05:00 -05:00
updated_at: 2010-02-15 03:05:00 -05:00
account_id: 1
id: 21
user_id: 1
date_of: 2010-02-14
- balance: -1600000
+ amount: -1600000
- created_at: 2010-02-15 03:06:57 -05:00
updated_at: 2010-02-15 03:06:57 -05:00
account_id: 1
id: 22
user_id: 1
date_of: 2010-02-15
- balance: -650000
+ amount: -650000
@@ -0,0 +1,16 @@
+class CreateBalances < ActiveRecord::Migration
+ def self.up
+ create_table :balances do |t|
+ t.integer :amount
+ t.integer :account_id, :user_id
+ t.date :date_of
+ t.timestamps
+ end
+ add_index :balances, :account_id
+ add_index :balances, :user_id
+ end
+
+ def self.down
+ drop_table :balances
+ end
+end
@@ -1,14 +0,0 @@
-class CreateDays < ActiveRecord::Migration
- def self.up
- create_table :days do |t|
- t.integer :balance
- t.integer :account_id, :user_id
- t.date :date_of
- t.timestamps
- end
- end
-
- def self.down
- drop_table :days
- end
-end
View
@@ -1,23 +1,23 @@
-class Day < ActiveRecord::Base
+class Balance < ActiveRecord::Base
belongs_to :account
belongs_to :user
- validates_presence_of :account_id, :user_id, :balance, :date_of
+ validates_presence_of :account_id, :user_id, :amount, :date_of
end
class Account < ActiveRecord::Base
TYPES = %w( worth assets debts )
belongs_to :user
- has_many :days
+ has_many :balances
validates_presence_of :name, :if => :needs_name
validates_presence_of :user_id
validates_inclusion_of :account_type, :in => TYPES
- named_scope :worth, :conditions => {:account_type => 'worth'}
- named_scope :assets, :conditions => {:account_type => 'assets'}
- named_scope :debts, :conditions => {:account_type => 'debts'}
+ scope :worth, :conditions => {:account_type => 'worth'}
+ scope :assets, :conditions => {:account_type => 'assets'}
+ scope :debts, :conditions => {:account_type => 'debts'}
def assets?
account_type == 'credit'
@@ -31,8 +31,8 @@ def needs_name
account_type != 'worth'
end
- def balance
- days.count > 0 ? days.first(:order => "created_at DESC").balance : 0
+ def amount
+ balances.count > 0 ? balances.first(:order => "created_at DESC").amount : 0
end
end
View
@@ -9,7 +9,11 @@
# For now, have the root URL act as if we visited the userpage of the first user
before do
- request.path_info = "/#{User.first.handle}" if request.path_info == '/'
+ if request.path_info == '/'
+ request.path_info = "/#{User.first.handle}"
+ elsif request.path_info == '/worth.xml'
+ request.path_info = "/#{User.first.handle}/worth.xml"
+ end
end
get "/:handle" do
@@ -21,4 +25,33 @@
:assets => user.accounts.assets.all,
:debts => user.accounts.debts.all
}
+end
+
+get "/:handle/worth.xml" do
+ headers['Content-Type'] = 'application/rss+xml'
+ "<?xml><xml/>"
+end
+
+helpers do
+
+ def date(date)
+ date.strftime("%B ") + date.strftime("%d").gsub(/^0/, "")
+ end
+
+ def direction(balance)
+ balance >= 0 ? 'positive' : 'negative'
+ end
+
+ def format_amount(amount)
+ number = amount.to_f / 100
+ parts = ("%01.2f" % number).split('.')
+ answer = '$' + number_with_delimiter(parts[0]) + '.' + parts[1].to_s
+ if number > 0
+ answer = '+' + answer
+ elsif number < 0
+ answer = '-' + answer
+ end
+ answer
+ end
+
end
Oops, something went wrong.

0 comments on commit a46cc4d

Please sign in to comment.