2015-09-08 11:44:40 +02:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: users
|
|
|
|
#
|
|
|
|
# id :integer not null, primary key
|
|
|
|
# name :string
|
|
|
|
# balance :integer default(0), not null
|
|
|
|
# penning :boolean default(FALSE), not null
|
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
|
|
|
#
|
|
|
|
|
2015-09-08 11:30:11 +02:00
|
|
|
class User < ActiveRecord::Base
|
2015-09-08 11:44:07 +02:00
|
|
|
devise :timeoutable, :omniauthable, :omniauth_providers => [:zeuswpi]
|
2015-09-08 12:11:48 +02:00
|
|
|
has_many :incoming_transactions,
|
|
|
|
class_name: 'Transaction', foreign_key: 'creditor_id'
|
|
|
|
has_many :outgoing_transactions,
|
|
|
|
class_name: 'Transaction', foreign_key: 'debtor_id'
|
2015-09-08 12:45:11 +02:00
|
|
|
|
|
|
|
def transactions
|
|
|
|
Transaction.where("creditor_id = ? OR debtor_id = ?", id, id)
|
|
|
|
end
|
2015-09-08 13:34:29 +02:00
|
|
|
|
2015-09-08 13:09:34 +02:00
|
|
|
def calculate_balance!
|
|
|
|
balance = incoming_transactions.sum(:amount) -
|
|
|
|
outgoing_transactions.sum(:amount)
|
|
|
|
self.update_attribute :balance, balance
|
|
|
|
end
|
2015-09-08 13:36:16 +02:00
|
|
|
|
2015-09-08 13:34:29 +02:00
|
|
|
def self.from_omniauth(auth)
|
|
|
|
where(name: auth.uid).first_or_create do |user|
|
|
|
|
user.name = auth.uid
|
|
|
|
end
|
|
|
|
end
|
2015-09-08 11:30:11 +02:00
|
|
|
end
|