forked from sigmike/peer4commit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser.rb
More file actions
69 lines (55 loc) · 1.7 KB
/
user.rb
File metadata and controls
69 lines (55 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable
devise :database_authenticatable, :registerable, :recoverable,
:rememberable, :trackable, :validatable
devise :omniauthable, :omniauth_providers => [:github]
# Validations
validates :bitcoin_address, bitcoin_address: true
# Associations
has_many :tips
# Callbacks
before_create :set_login_token!, unless: :login_token?
# Instance Methods
def github_url
"https://github.com/#{nickname}"
end
def balance
tips.unpaid.sum(:amount)
end
def full_name
name.presence || nickname.presence || email
end
def subscribed?
!unsubscribed?
end
# Class Methods
def self.update_cache
includes(:tips).find_each do |user|
user.update commits_count: user.tips.count
user.update withdrawn_amount: user.tips.paid.sum(:amount)
end
end
def self.create_with_omniauth!(auth_info)
generated_password = Devise.friendly_token.first(Devise.password_length.min)
create!( email: auth_info.primary_email,
password: generated_password,
nickname: auth_info.nickname)
end
def self.find_or_create_with_commit commit
author = commit.commit.author
where(email: author.email).first_or_create do |user|
user.email = author.email
user.password = Devise.friendly_token.first(Devise.password_length.min)
user.name = author.name
user.nickname = commit.author.try(:login)
end
end
private
def set_login_token!
loop do
self.login_token = SecureRandom.urlsafe_base64
break login_token unless User.exists?(login_token: login_token)
end
end
end