forked from fredwu/angel_nest
/
user.rb
227 lines (180 loc) · 6.01 KB
/
user.rb
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
require 'digest/md5'
class User < ActiveRecord::Base
include Commentable,
Followable
devise :database_authenticatable,
:token_authenticatable,
:omniauthable,
:confirmable,
:recoverable,
:registerable,
:rememberable,
:trackable,
:validatable
attr_readonly :messages_count
attr_accessor :login
attr_accessible :login,
:username,
:name,
:email,
:password,
:password_confirmation,
:remember_me
has_many :messages, :order => 'created_at DESC'
has_one :investor_profile
has_and_belongs_to_many :proposals, :join_table => :proposal_for_investors
has_many :startup_users, :foreign_key => 'user_email', :primary_key => 'email'
has_many :startups, :through => :startup_users
has_many :target_followed, :class_name => 'TargetFollower', :as => :follower
has_many :users_followed, :through => :target_followed, :source => :target, :source_type => 'User'
has_many :startups_followed, :through => :target_followed, :source => :target, :source_type => 'Startup'
validates :username, :presence => true,
:uniqueness => { :case_sensitive => false },
:length => { :within => 4..20 },
:format => { :with => /^[A-Za-z0-9_]+$/ }
validates :name, :presence => true,
:length => { :within => 4..30 }
scope :new_users, joins { [startup_users.outer, investor_profile.outer] }.where { (startup_users.user_email == nil) & (investor_profiles.user_id == nil) }
scope :entrepreneurs, joins { startup_users }.where { startup_users.user_email != nil }
scope :investors, joins { investor_profile }.where { investor_profiles.user_id != nil }
before_save :email_nomarlisation
def incoming_messages
comments.private_only.topics
end
def outgoing_messages
messages.on_users.private_only.topics
end
def inbox_messages
incoming_messages.without_proposal.unarchived
end
def sent_messages
outgoing_messages.without_proposal
end
def archived_messages
incoming_messages.without_proposal.archived
end
def inbox_proposals
incoming_messages.with_proposal.unarchived
end
def sent_proposals
outgoing_messages.with_proposal
end
def archived_proposals
incoming_messages.with_proposal.archived
end
def has_new_messages?
inbox_messages.unread.any?
end
def has_new_proposals?
inbox_proposals.unread.any?
end
def is_admin?
!!is_admin
end
def is_new_user?
!is_entrepreneur? && !is_investor?
end
def is_entrepreneur?
startups.present?
end
def is_investor?
investor_profile.present?
end
def avatar(size = 80)
"http://www.gravatar.com/avatar/#{Digest::MD5.hexdigest(email)}?s=#{size}"
end
def send_private_message(target_user, content, extras = {})
messages.create!({
:content => content,
:is_private => true,
:target_id => target_user.id,
:target_type => 'User'
}.merge(extras)) && reload
end
def reply_private_message(topic, content, extras = {})
messages.create!({
:content => content,
:is_private => true,
:target_id => topic.user.id,
:target_type => 'User',
:topic_id => topic.id
}.merge(extras)) && reload
end
def add_micro_post(content)
unless content.blank?
messages.create(:content => content) && reload
true
else
false
end
end
def micro_posts
messages.micro_posts
end
def follow(target)
target.target_followers.create(
:follower_id => id,
:follower_type => 'User'
) && reload unless target == self || target.nil?
end
def unfollow(target)
target.target_followers.where(
:follower_id => id,
:follower_type => 'User'
).first.try(:destroy) && reload unless target.nil?
end
def is_following?(target)
return false if target.nil?
!!target_followed.where(:target_id => target.id, :target_type => target.class.name).first
end
def followed
users_followed + startups_followed
end
def followed_micro_posts
Message.where(:target_id => nil, :user_id => users_followed_ids + [id]).default_order
end
protected
# Devise's support for login using the :login virtual attribute
# https://github.com/plataformatec/devise/wiki/How-To:-Allow-users-to-sign_in-using-their-username-or-email-address
class << self
def find_for_database_authentication(warden_conditions)
conditions = warden_conditions.dup
login = conditions.delete(:login)
where(conditions).where(["lower(username) = :value OR lower(email) = :value", { :value => login.downcase }]).first
end
def send_reset_password_instructions(attributes={})
recoverable = find_recoverable_or_initialize_with_errors(reset_password_keys, attributes, :not_found)
recoverable.send_reset_password_instructions if recoverable.persisted?
recoverable
end
def find_recoverable_or_initialize_with_errors(required_attributes, attributes, error=:invalid)
(case_insensitive_keys || []).each { |k| attributes[k].try(:downcase!) }
attributes = attributes.slice(*required_attributes)
attributes.delete_if { |key, value| value.blank? }
if attributes.size == required_attributes.size
if attributes.has_key?(:login)
login = attributes.delete(:login)
record = find_record(login)
else
record = where(attributes).first
end
end
unless record
record = new
required_attributes.each do |key|
value = attributes[key]
record.send("#{key}=", value)
record.errors.add(key, value.present? ? error : :blank)
end
end
record
end
def find_record(login)
where(["username = :value OR email = :value", { :value => login }]).first
end
end
private
def email_nomarlisation
self.email = email.strip.downcase
end
end