tab/app/models/user.rb

53 lines
1.6 KiB
Ruby
Raw Normal View History

2015-09-08 09:44:40 +00: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 09:30:11 +00:00
class User < ActiveRecord::Base
2015-09-11 12:51:35 +00:00
include FriendlyId
friendly_id :name, use: :finders
2018-03-13 13:50:50 +00:00
devise :cas_authenticatable
2015-09-08 10:11:48 +00:00
has_many :incoming_transactions,
class_name: 'Transaction', foreign_key: 'creditor_id'
has_many :outgoing_transactions,
class_name: 'Transaction', foreign_key: 'debtor_id'
2017-01-09 14:46:43 +00:00
has_many :incoming_requests,
class_name: 'Request', foreign_key: 'debtor_id'
has_many :outgoing_requests,
class_name: 'Request', foreign_key: 'creditor_id'
2017-01-09 15:22:58 +00:00
has_many :notifications
2015-09-08 10:45:11 +00:00
2015-09-09 10:49:24 +00:00
has_many :issued_transactions, as: :issuer, class_name: 'Transaction'
2015-09-08 13:51:23 +00:00
validates :name, presence: true, uniqueness: true
2018-03-13 13:50:50 +00:00
scope :humans, -> { where.not(id: self.wina) }
2015-09-14 15:21:53 +00:00
2015-09-08 10:45:11 +00:00
def transactions
Transaction.where("creditor_id = ? OR debtor_id = ?", id, id)
end
2015-09-08 11:34:29 +00:00
2015-09-08 11:09:34 +00:00
def calculate_balance!
balance = incoming_transactions.sum(:amount) -
outgoing_transactions.sum(:amount)
self.update_attribute :balance, balance
end
2018-03-13 13:50:50 +00:00
def cas_extra_attributes=(extra_attributes)
self.name = extra_attributes['display_name']
self.debt_allowed = extra_attributes['permissions'].include? 'HAVE_SCHULDEN'
self.penning = extra_attributes['permissions'].include? 'MANAGE_SCHULDEN'
2015-09-08 11:34:29 +00:00
end
2015-09-08 19:38:29 +00:00
2018-03-13 13:50:50 +00:00
def self.wina
@@wina ||= find_or_create_by name: 'WiNA', username: :WiNA
2015-09-08 19:38:29 +00:00
end
2015-09-08 09:30:11 +00:00
end