<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -10,23 +10,60 @@
 # t.datetime &quot;updated_at&quot;
 
 class CreditPitch &lt; ActiveRecord::Base
+    include AASMWithFixes
     belongs_to :user
     belongs_to :pitch
     
     validates_presence_of :user_id, :pitch_id, :amount
-    validates_numericality_of :amount, :greater_than =&gt; 0    
+    validates_numericality_of :amount, :greater_than =&gt; 0
+    validate_on_create :check_donation, :if =&gt; lambda { |me| me.pitch }
+    validate_on_update :disable_updating_paid_donations, :check_donation, :if =&gt; lambda { |me| me.pitch }
     named_scope :unpaid, :conditions =&gt; &quot;status = 'unpaid'&quot;
     named_scope :paid, :conditions =&gt; &quot;status = 'paid'&quot;
     
+    aasm_column :status
+    aasm_initial_state  :unpaid
+
+    aasm_state :unpaid
+    aasm_state :paid
+
+    aasm_event :pay do
+      transitions :from =&gt; :unpaid, :to =&gt; :paid 
+    end
+    
     def self.createable_by?(user)
       user
     end
     
     def update_pitch_funding
-      pitch.current_funding += amount
+      if pitch.requested_amount &lt; pitch.current_funding + amount
+            amount = pitch.request_amount - pitch.current_funding
+            pitch.current_funding = pitch.request_amount
+      else
+          pitch.current_funding += amount
+      end
       pitch.save
     end
     
+    protected
+    
+    def check_donation
+      if pitch.fully_funded?
+        errors.add_to_base(&quot;Great news! This pitch is already fully funded therefore it can't be donated to any longer.&quot;)
+        return
+      end
+    end
+    
+    def disable_updating_paid_donations
+      if paid? &amp;&amp; !being_marked_as_paid?
+        errors.add_to_base('Paid donations cannot be updated')
+      end
+    end
+
+    def being_marked_as_paid?
+      status_changed? &amp;&amp; status_was == 'unpaid'
+    end
+    
     def self.has_enough_credits?(credit_pitch_amounts, user)
         credit_total = 0
         credit_pitch_amounts.values.each do |val|</diff>
      <filename>app/models/credit_pitch.rb</filename>
    </modified>
    <modified>
      <diff>@@ -90,6 +90,15 @@ class Pitch &lt; NewsItem
       for_user(user).map(&amp;:amount).sum
     end
   end
+  has_many :credit_pitches, :dependent =&gt; :destroy do
+    def for_user(user)
+      find_all_by_user_id(user.id)
+    end
+
+    def total_amount_for_user(user)
+      for_user(user).map(&amp;:amount).sum
+    end
+  end
   has_many :organizational_donors, :through =&gt; :donations, :source =&gt; :user, :order =&gt; &quot;donations.created_at&quot;, 
             :conditions =&gt; &quot;users.type = 'organization'&quot;,
             :uniq =&gt; true
@@ -252,7 +261,7 @@ class Pitch &lt; NewsItem
   end
 
   def total_amount_donated
-    donations.paid.map(&amp;:amount).sum
+    donations.paid.map(&amp;:amount).sum + credit_pitches.paid.map(&amp;:amount).sum
   end
 
   def donated_to?</diff>
      <filename>app/models/pitch.rb</filename>
    </modified>
    <modified>
      <diff>@@ -194,11 +194,11 @@ class User &lt; ActiveRecord::Base
   def apply_credit_pitches
       #refactor - there must be a nicer ruby-like way to do this
       transaction do 
-          credit_pitch_ids = self.credit_pitches.map{|credit_pitch| [credit_pitch.id]}.join(&quot;, &quot;)
+          credit_pitch_ids = self.credit_pitches.unpaid.map{|credit_pitch| [credit_pitch.id]}.join(&quot;, &quot;)
           credit = Credit.create(:user =&gt; self, :description =&gt; &quot;Applied to Pitches (#{credit_pitch_ids})&quot;,
                           :amount =&gt; (0 - self.allocated_credits))
-          self.credit_pitches.each do |credit_pitch|
-              credit_pitch.status = &quot;paid&quot;
+          self.credit_pitches.unpaid.each do |credit_pitch|
+              credit_pitch.pay!
               credit_pitch.paid_credit_id = credit.id
               credit_pitch.update_pitch_funding
               credit_pitch.save</diff>
      <filename>app/models/user.rb</filename>
    </modified>
    <modified>
      <diff>@@ -1,4 +1,6 @@
 - form_for CreditPitch.new(:pitch =&gt; news_item, :amount =&gt; current_user.remaining_credits), :url =&gt; myspot_credit_pitches_path, :html =&gt; {:id =&gt; &quot;new_credit_pitch_#{news_item.id}&quot;, :class =&gt; &quot;auth&quot;} do |f|
   = f.hidden_field :amount, :id =&gt; &quot;credit_pitch_amount&quot;
   = f.hidden_field :pitch_id
-  = image_submit_tag &quot;apply_credits.png&quot;, :alt =&gt; &quot;Apply credits&quot;, :title =&gt; &quot;Apply credits&quot;
\ No newline at end of file
+  = image_submit_tag &quot;apply_credits.png&quot;, :alt =&gt; &quot;Apply credits&quot;, :title =&gt; &quot;Apply credits&quot;
+.double_content_border
+= render :partial =&gt; &quot;shared/progress_bar&quot;, :locals =&gt; {:pitch =&gt; news_item}
\ No newline at end of file</diff>
      <filename>app/views/credits/_credit_pitch.html.haml</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>bbcef7ab750c0b4bed266ad124604ac60c07d898</id>
    </parent>
  </parents>
  <author>
    <name>Dan Newman</name>
    <email>dpnewman@gmail.com</email>
  </author>
  <url>http://github.com/spot-us/spot-us/commit/046ed6f2085c2cf0ef45d1a809214f9b53fd122e</url>
  <id>046ed6f2085c2cf0ef45d1a809214f9b53fd122e</id>
  <committed-date>2009-10-22T08:05:37-07:00</committed-date>
  <authored-date>2009-10-22T08:05:37-07:00</authored-date>
  <message>Credit pitches - progress</message>
  <tree>1865a23c1c3c7a9f03b6afba4ff202fc597fd6b0</tree>
  <committer>
    <name>Dan Newman</name>
    <email>dpnewman@gmail.com</email>
  </committer>
</commit>
