Skip to content

Instantly share code, notes, and snippets.

@jmonegro
Forked from forkfight/balance_entry.rb
Created February 10, 2014 21:09
Show Gist options
  • Save jmonegro/8924253 to your computer and use it in GitHub Desktop.
Save jmonegro/8924253 to your computer and use it in GitHub Desktop.
I want to manage the finances of an account with methods for crediting, debiting, and determining the balance of said account. I'd like for a better way of handling this.

Manage the balance of a user account (credits, debits, and balance)

class BalanceEntry < ActiveRecord::Base
belongs_to :user
# attributes: kind (debit or credit), source (stripe, paypal, bank transfer, etc.), amount (positive or negative float), verified (boolean)
after_create :perform_transaction
def perform_transaction
# withdraw or deposit on Stripe, Paypal, etc.
end
end
class User < ActiveRecord::Base
has_many :balance_entries
def balance
balance_entries.where(verified: true).collect {|e| e }.sum
end
def credit(amount=0.0, source='fairygodparents')
balance_entries.create kind: 'credit', source: source, amount: amount
end
def debit(amount=0.0, source='fairygodparents')
balance_entries.create kind: 'credit', source: source, amount: amount
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment