Skip to content

Commit

Permalink
Adding credit card limit field so that current balances can be colore…
Browse files Browse the repository at this point in the history
…d according to their current amount used. Defaults are low (0% or near), medium (30%), high (80%), and critical (100%).
  • Loading branch information
Kieran Pilkington authored and jamis committed May 26, 2009
1 parent 6ba02ee commit a42095f
Show file tree
Hide file tree
Showing 10 changed files with 110 additions and 4 deletions.
10 changes: 10 additions & 0 deletions app/helpers/subscriptions_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ def balance_cell(container, options={})
classes = %w(number)
classes += Array(options[:classes]) if options[:classes]
classes << "negative" if balance < 0
classes << "current_balance"

if container.is_a?(Account) && container.credit_card?
percentage_used = container.limit.abs.to_i == 0 ? 100 :
((container.balance.abs.to_f / container.limit.abs.to_f) * 100).to_i
classes << if percentage_used >= Account::DEFAULT_LIMIT_VALUES[:critical]: "critical"
elsif percentage_used >= Account::DEFAULT_LIMIT_VALUES[:high]: "high"
elsif percentage_used >= Account::DEFAULT_LIMIT_VALUES[:medium]: "medium"
else "low" end
end

content = format_amount(balance)
if real_balance != balance
Expand Down
11 changes: 10 additions & 1 deletion app/models/account.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
class Account < ActiveRecord::Base
DEFAULT_BUCKET_NAME = "General"

# When should the levels of credit cards be reached (in %)
DEFAULT_LIMIT_VALUES = {
:critical => 100,
:high => 80,
:medium => 30,
:low => 0
}

belongs_to :subscription
belongs_to :author, :class_name => "User", :foreign_key => "user_id"

attr_accessor :starting_balance
attr_accessible :name, :role, :starting_balance
attr_accessible :name, :role, :limit, :starting_balance

validates_presence_of :name
validates_presence_of :limit, :if => :credit_card?
validates_uniqueness_of :name, :scope => :subscription_id, :case_sensitive => false

has_many :buckets do
Expand Down
7 changes: 6 additions & 1 deletion app/views/accounts/_form.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@
%p
%label
<strong>What kind</strong> of account is this?
= form.select :role, [["Checking", "checking"], ["Credit card", "credit-card"], ["Other", "other"]]
= form.select :role, [["Checking", "checking"], ["Credit card", "credit-card"], ["Other", "other"]], {}, :onchange => "Accounts.showOrHideCreditLimit(this.value);"

%p{:style => 'display: none;', :id => 'credit_limit_div'}
%label
What is the <strong>credit limit</strong>:
== $#{form.text_field :limit, :class => "number", :size => 8}

- if form.object.nil? || form.object.new_record?
%fieldset
Expand Down
3 changes: 3 additions & 0 deletions app/views/accounts/_name.html.haml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
%span.actions
- if account.credit_card?
= link_to_function("Adjust Limit", "Accounts.adjustLimit(#{account_path(account).to_json}, #{account.limit.to_json}, #{form_authenticity_token.to_json})")
|
- if account.statements.pending.any?
= link_to("Resume reconciling", edit_statement_path(account.statements.pending.first))
- else
Expand Down
9 changes: 9 additions & 0 deletions db/migrate/20090516072907_add_limit_to_accounts.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class AddLimitToAccounts < ActiveRecord::Migration
def self.up
add_column :accounts, :limit, :integer
end

def self.down
remove_column :accounts, :limit
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20090506161959) do
ActiveRecord::Schema.define(:version => 20090516072907) do

create_table "account_items", :force => true do |t|
t.integer "event_id", :null => false
Expand All @@ -31,6 +31,7 @@
t.datetime "created_at"
t.datetime "updated_at"
t.integer "balance", :default => 0, :null => false
t.integer "limit"
end

add_index "accounts", ["subscription_id", "name"], :name => "index_accounts_on_subscription_id_and_name", :unique => true
Expand Down
47 changes: 47 additions & 0 deletions public/javascripts/accounts.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,18 @@ var Accounts = {
return false;
}

if($F('account_role') == 'credit-card' && $F('account_limit').blank()) {
$('account_name').activate();
alert('Please provide a limit for the account.');
return false;
}

var balance = Money.parse('current_balance', true);
$('account_starting_balance_amount').value = balance;

var limit = Money.parse('account_limit', true);
$('account_limit').value = limit;

return true;
},

Expand All @@ -57,5 +66,43 @@ var Accounts = {
parameters:params
});
}
},

adjustLimit: function(url, limit, token) {
new_limit = prompt("Enter the new limit for this account:", Money.formatValue(limit));
new_limit = Money.parseValue(new_limit);
while(new_limit == '') {
new_limit = prompt("Cannot have have a blank limit. Please re-enter it:", Money.formatValue(limit));
}
if(new_limit && new_limit != limit) {
params = encodeURIComponent("account[limit]") + "=" + encodeURIComponent(new_limit) +
"&authenticity_token=" + encodeURIComponent(token);

new Ajax.Request(url, {
asynchronous:true,
evalScripts:true,
method:'put',
parameters:params,
onSuccess: function(request) {
window.location.reload();
}
});
}
},

showOrHideCreditLimit: function(value) {
if (value == 'credit-card') {
Accounts.showCreditLimit();
} else {
Accounts.hideCreditLimit();
}
},

showCreditLimit: function() {
$('credit_limit_div').show();
},

hideCreditLimit: function() {
$('credit_limit_div').hide();
}
}
20 changes: 20 additions & 0 deletions public/stylesheets/money.css
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,10 @@ td.number, th.number {
white-space: nowrap;
}

td.current_balance, th.current_balance {
color: #999;
}

span.real_balance {
font-size: 80%;
color: #999;
Expand All @@ -280,6 +284,22 @@ span.check {
color: red;
}

.critical {
color: red !important;
}

.high {
color: brown !important;
}

.medium {
color: sandybrown !important;
}

.low {
color: green !important;
}

td.date, th.date {
width: 1px;
padding-left: 0.3em;
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/accounts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ john_mastercard:
name: Mastercard
role: credit-card
balance: -2525
limit: 5000
created_at: <%= (60.days.ago + 2.hours).utc.to_s(:db) %>
updated_at: <%= (60.days.ago + 2.hours).utc.to_s(:db) %>

Expand Down
3 changes: 2 additions & 1 deletion test/unit/account_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ def new_account(options={})
subscription = options.delete(:subscription) || subscriptions(:john)

options = {:name => "Visa",
:role => "credit-card"}.merge(options)
:role => "credit-card",
:limit => 5000}.merge(options)

subscription.accounts.create(options, :author => users(:john))
end
Expand Down

0 comments on commit a42095f

Please sign in to comment.