-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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