<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array">
    <added>
      <filename>db/migrate/20090516072907_add_limit_to_accounts.rb</filename>
    </added>
  </added>
  <modified type="array">
    <modified>
      <diff>@@ -12,6 +12,16 @@ module SubscriptionsHelper
     classes = %w(number)
     classes += Array(options[:classes]) if options[:classes]
     classes &lt;&lt; &quot;negative&quot; if balance &lt; 0
+    classes &lt;&lt; &quot;current_balance&quot;
+
+    if container.is_a?(Account) &amp;&amp; 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 &lt;&lt; if percentage_used &gt;= Account::DEFAULT_LIMIT_VALUES[:critical]:  &quot;critical&quot;
+                 elsif percentage_used &gt;= Account::DEFAULT_LIMIT_VALUES[:high]:   &quot;high&quot;
+                 elsif percentage_used &gt;= Account::DEFAULT_LIMIT_VALUES[:medium]: &quot;medium&quot;
+                 else  &quot;low&quot; end
+    end
 
     content = format_amount(balance)
     if real_balance != balance</diff>
      <filename>app/helpers/subscriptions_helper.rb</filename>
    </modified>
    <modified>
      <diff>@@ -1,13 +1,22 @@
 class Account &lt; ActiveRecord::Base
   DEFAULT_BUCKET_NAME = &quot;General&quot;
 
+  # When should the levels of credit cards be reached (in %)
+  DEFAULT_LIMIT_VALUES = {
+    :critical =&gt; 100,
+    :high =&gt; 80,
+    :medium =&gt; 30,
+    :low =&gt; 0
+  }
+
   belongs_to :subscription
   belongs_to :author, :class_name =&gt; &quot;User&quot;, :foreign_key =&gt; &quot;user_id&quot;
 
   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 =&gt; :credit_card?
   validates_uniqueness_of :name, :scope =&gt; :subscription_id, :case_sensitive =&gt; false
 
   has_many :buckets do</diff>
      <filename>app/models/account.rb</filename>
    </modified>
    <modified>
      <diff>@@ -18,7 +18,12 @@
     %p
       %label
         &lt;strong&gt;What kind&lt;/strong&gt; of account is this?
-        = form.select :role, [[&quot;Checking&quot;, &quot;checking&quot;], [&quot;Credit card&quot;, &quot;credit-card&quot;], [&quot;Other&quot;, &quot;other&quot;]]
+        = form.select :role, [[&quot;Checking&quot;, &quot;checking&quot;], [&quot;Credit card&quot;, &quot;credit-card&quot;], [&quot;Other&quot;, &quot;other&quot;]], {}, :onchange =&gt; &quot;Accounts.showOrHideCreditLimit(this.value);&quot;
+
+    %p{:style =&gt; 'display: none;', :id =&gt; 'credit_limit_div'}
+      %label
+        What is the &lt;strong&gt;credit limit&lt;/strong&gt;:
+        == $#{form.text_field :limit, :class =&gt; &quot;number&quot;, :size =&gt; 8}
 
   - if form.object.nil? || form.object.new_record?
     %fieldset</diff>
      <filename>app/views/accounts/_form.html.haml</filename>
    </modified>
    <modified>
      <diff>@@ -1,4 +1,7 @@
 %span.actions
+  - if account.credit_card?
+    = link_to_function(&quot;Adjust Limit&quot;, &quot;Accounts.adjustLimit(#{account_path(account).to_json}, #{account.limit.to_json}, #{form_authenticity_token.to_json})&quot;)
+  |
   - if account.statements.pending.any?
     = link_to(&quot;Resume reconciling&quot;, edit_statement_path(account.statements.pending.first))
   - else</diff>
      <filename>app/views/accounts/_name.html.haml</filename>
    </modified>
    <modified>
      <diff>@@ -9,7 +9,7 @@
 #
 # It's strongly recommended to check this file into your version control system.
 
-ActiveRecord::Schema.define(:version =&gt; 20090506161959) do
+ActiveRecord::Schema.define(:version =&gt; 20090516072907) do
 
   create_table &quot;account_items&quot;, :force =&gt; true do |t|
     t.integer &quot;event_id&quot;,     :null =&gt; false
@@ -31,6 +31,7 @@ ActiveRecord::Schema.define(:version =&gt; 20090506161959) do
     t.datetime &quot;created_at&quot;
     t.datetime &quot;updated_at&quot;
     t.integer  &quot;balance&quot;,         :default =&gt; 0, :null =&gt; false
+    t.integer  &quot;limit&quot;
   end
 
   add_index &quot;accounts&quot;, [&quot;subscription_id&quot;, &quot;name&quot;], :name =&gt; &quot;index_accounts_on_subscription_id_and_name&quot;, :unique =&gt; true</diff>
      <filename>db/schema.rb</filename>
    </modified>
    <modified>
      <diff>@@ -34,9 +34,18 @@ var Accounts = {
       return false;
     }
 
+    if($F('account_role') == 'credit-card' &amp;&amp; $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;
   },
 
@@ -57,5 +66,43 @@ var Accounts = {
         parameters:params
       });
     }
+  },
+
+  adjustLimit: function(url, limit, token) {
+    new_limit = prompt(&quot;Enter the new limit for this account:&quot;, Money.formatValue(limit));
+    new_limit = Money.parseValue(new_limit);
+    while(new_limit == '') {
+      new_limit = prompt(&quot;Cannot have have a blank limit. Please re-enter it:&quot;, Money.formatValue(limit));
+    }
+    if(new_limit &amp;&amp; new_limit != limit) {
+      params = encodeURIComponent(&quot;account[limit]&quot;) + &quot;=&quot; + encodeURIComponent(new_limit) +
+        &quot;&amp;authenticity_token=&quot; + 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();
   }
 }</diff>
      <filename>public/javascripts/accounts.js</filename>
    </modified>
    <modified>
      <diff>@@ -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;
@@ -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;</diff>
      <filename>public/stylesheets/money.css</filename>
    </modified>
    <modified>
      <diff>@@ -17,6 +17,7 @@ john_mastercard:
   name: Mastercard
   role: credit-card
   balance: -2525
+  limit: 5000
   created_at: &lt;%= (60.days.ago + 2.hours).utc.to_s(:db) %&gt;
   updated_at: &lt;%= (60.days.ago + 2.hours).utc.to_s(:db) %&gt;
 </diff>
      <filename>test/fixtures/accounts.yml</filename>
    </modified>
    <modified>
      <diff>@@ -141,7 +141,8 @@ class AccountTest &lt; ActiveSupport::TestCase
       subscription = options.delete(:subscription) || subscriptions(:john)
 
       options = {:name =&gt; &quot;Visa&quot;,
-                 :role =&gt; &quot;credit-card&quot;}.merge(options)
+                 :role =&gt; &quot;credit-card&quot;,
+                 :limit =&gt; 5000}.merge(options)
 
       subscription.accounts.create(options, :author =&gt; users(:john))
     end</diff>
      <filename>test/unit/account_test.rb</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>6ba02eefe76f105f76e6ec5996627bc23608ac22</id>
    </parent>
  </parents>
  <author>
    <name>Kieran Pilkington</name>
    <email>kieran@katipo.co.nz</email>
  </author>
  <url>http://github.com/jamis/bucketwise/commit/a42095f712e7fc160e30bc544a8413263568e2d3</url>
  <id>a42095f712e7fc160e30bc544a8413263568e2d3</id>
  <committed-date>2009-05-25T20:49:49-07:00</committed-date>
  <authored-date>2009-05-16T02:30:13-07:00</authored-date>
  <message>Adding credit card limit field so that current balances can be colored according to their current amount used. Defaults are low (0% or near), medium (30%), high (80%), and critical (100%).</message>
  <tree>d0d55a2eabbc2ed76d585a5c3033b48a504469b4</tree>
  <committer>
    <name>Jamis Buck</name>
    <email>jamis@37signals.com</email>
  </committer>
</commit>
