-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser.rb
More file actions
136 lines (113 loc) · 3.53 KB
/
Copy pathuser.rb
File metadata and controls
136 lines (113 loc) · 3.53 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
class User < ActiveRecord::Base
validates :first_name, :last_name, :email, :session_token, :password_digest, presence: true
validates :password, length: {minimum: 6, allow_nil: true}
validate :email_validation
validates :email, uniqueness: true
has_attached_file :picture, :styles => { :medium => "200x200>", :thumb => "50x50>" }, :default_url => "/images/:style/missing.png"
validates_attachment_content_type :picture, :content_type => /\Aimage\/.*\Z/
include PgSearch
multisearchable :against => [:first_name, :last_name]
has_many :notifications, inverse_of: :user, dependent: :destroy
has_many :comments, as: :commentable, dependent: :destroy
has_many(
:authored_comments,
class_name: "Comment",
foreign_key: :author_id,
primary_key: :id,
inverse_of: :author,
dependent: :destroy
)
# follow objects from other users which are following current user
has_many :follows, as: :followable, dependent: :destroy
# user objects for users who are following current user
has_many :followers, through: :follows, source: :follower
# returns all follow objects which belong to user
has_many(
:followings,
class_name: "Follow",
foreign_key: :follower_id,
primary_key: :id,
inverse_of: :follower,
dependent: :destroy
)
has_many :ratings, as: :rateable, dependent: :destroy
has_many(
:rated_objects,
class_name: "Rating",
foreign_key: :rater_id,
primary_key: :id,
inverse_of: :rater,
dependent: :destroy
)
# gets all companies through follows relation with source type Company
has_many(
:followed_companies,
through: :followings,
source: :followable,
source_type: "Company"
)
# gets all users through follows realtion with source type User
has_many(
:followed_users,
through: :followings,
source: :followable,
source_type: "User"
)
def first_name
read_attribute(:first_name).split.map(&:capitalize).join(" ")
end
def last_name
read_attribute(:last_name).split.map(&:capitalize).join(" ")
end
def name
"#{first_name} #{last_name}"
end
def rating
return 0 if ratings.empty?
(ratings.pluck(:rating).inject(:+).to_f / ratings.count).round(2)
end
attr_reader :password
after_initialize :ensure_session_token
def password=(password)
@password = password
self.password_digest = BCrypt::Password.create(password)
end
def is_password?(password)
BCrypt::Password.new(self.password_digest).is_password?(password)
end
def reset_session_token!
self.session_token = SecureRandom.urlsafe_base64
self.save!
self.session_token
end
def self.find_by_credentials(user_params)
user = User.find_by_email(user_params[:email])
user.try(:is_password?, user_params[:password]) ? user : nil
end
def self.find_or_create_by_auth_hash(auth_hash)
user = User.find_by(
provider: auth_hash[:provider],
uid: auth_hash[:uid])
unless user
info = auth_hash[:info]
user = User.create!(
provider: auth_hash[:provider],
uid: auth_hash[:uid],
first_name: info[:first_name],
last_name: info[:last_name],
email: info[:email],
password: SecureRandom::urlsafe_base64,
picture: info[:image])
end
user
end
private
def ensure_session_token
self.session_token ||= SecureRandom.urlsafe_base64
end
def email_validation
unless email =~ /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i
errors[:email] << "is not a valid email address"
end
end
end