Class: User
Constant Summary
collapse
- MALE =
'M'
- FEMALE =
'F'
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
included
included
Methods included from UrlUpload
#data_from_url, #validate
Instance Attribute Details
#authorizing_from_omniauth ⇒ Object
Returns the value of attribute authorizing_from_omniauth.
109
110
111
|
# File 'app/models/user.rb', line 109
def authorizing_from_omniauth
@authorizing_from_omniauth
end
|
Class Method Details
.build_conditions_for_search(search) ⇒ Object
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
# File 'app/models/user.rb', line 148
def self.build_conditions_for_search(search)
user = User.arel_table
users = User.active
if search['country_id'] && !(search['metro_area_id'] || search['state_id'])
users = users.where(user[:country_id].eq(search['country_id']))
end
if search['state_id'] && !search['metro_area_id']
users = users.where(user[:state_id].eq(search['state_id']))
end
if search['metro_area_id']
users = users.where(user[:metro_area_id].eq(search['metro_area_id']))
end
if search['login']
users = users.where(user[:login].matches("%#{search['login']}%"))
end
if search['vendor']
users = users.where(user[:vendor].eq(true))
end
if search['description']
users = users.where(user[:description].matches("%#{search['description']}%"))
end
users
end
|
.build_search_conditions(query) ⇒ Object
213
214
215
|
# File 'app/models/user.rb', line 213
def self.build_search_conditions(query)
query
end
|
.currently_online ⇒ Object
203
204
205
|
# File 'app/models/user.rb', line 203
def self.currently_online
User.where("sb_last_seen_at > ?", Time.now.utc-5.minutes)
end
|
.find_by_activity(options = {}) ⇒ Object
172
173
174
175
176
177
178
179
180
181
182
|
# File 'app/models/user.rb', line 172
def self.find_by_activity(options = {})
options.reverse_merge! :limit => 30, :require_avatar => true, :since => 7.days.ago
activities = Activity.since(options[:since]).select('activities.user_id, count(*) as count').
group('activities.user_id').
where("#{options[:require_avatar] ? ' users.avatar_id IS NOT NULL AND ' : ''} users.activated_at IS NOT NULL").
order('count DESC').
joins( "LEFT JOIN users ON users.id = activities.user_id").
limit(options[:limit])
activities.map{|a| find(a.user_id) }
end
|
.find_by_login_or_email(string) ⇒ Object
114
115
116
|
# File 'app/models/user.rb', line 114
def self.find_by_login_or_email(string)
self.where("LOWER(email) = ? OR LOWER(login) = ?", string.downcase, string.downcase).first
end
|
.find_country_and_state_from_search_params(search) ⇒ Object
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
# File 'app/models/user.rb', line 118
def self.find_country_and_state_from_search_params(search)
country = Country.find(search['country_id']) if !search['country_id'].blank?
state = State.find(search['state_id']) if !search['state_id'].blank?
metro_area = MetroArea.find(search['metro_area_id']) if !search['metro_area_id'].blank?
if metro_area && metro_area.country
country ||= metro_area.country
state ||= metro_area.state
search['country_id'] = metro_area.country.id if metro_area.country
search['state_id'] = metro_area.state.id if metro_area.state
end
states = country ? country.states.sort_by{|s| s.name} : []
if states.any?
metro_areas = state ? state.metro_areas.order("name") : []
else
metro_areas = country ? country.metro_areas : []
end
return [metro_areas, states]
end
|
.find_featured ⇒ Object
184
185
186
|
# File 'app/models/user.rb', line 184
def self.find_featured
self.featured
end
|
.find_or_create_from_authorization(auth) ⇒ Object
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
|
# File 'app/models/user.rb', line 416
def self.find_or_create_from_authorization(auth)
user = User.where(:email => auth.email).first_or_initialize
user.login ||= auth.nickname
if user.new_record?
new_password = user.newpass(8)
user.password = new_password
user.password_confirmation = new_password
end
user.authorizing_from_omniauth = true
if user.save
user.activate unless user.active?
user.reset_persistence_token!
end
user
end
|
.prepare_params_for_search(params) ⇒ Object
140
141
142
143
144
145
146
|
# File 'app/models/user.rb', line 140
def self.prepare_params_for_search(params)
search = {}.merge(params)
search['metro_area_id'] = params[:metro_area_id] || nil
search['state_id'] = params[:state_id] || nil
search['country_id'] = params[:country_id] || nil
search
end
|
.recent_activity(options = {}) ⇒ Object
198
199
200
201
|
# File 'app/models/user.rb', line 198
def self.recent_activity(options = {})
options.reverse_merge! :per_page => 10, :page => 1
Activity.recent.joins("LEFT JOIN users ON users.id = activities.user_id").where('users.activated_at IS NOT NULL').select('activities.*').page(options[:page]).per(options[:per_page])
end
|
.search(query, options = {}) ⇒ Object
207
208
209
210
211
|
# File 'app/models/user.rb', line 207
def self.search(query, options = {})
with_scope :find => { :conditions => build_search_conditions(query) } do
where(options)
end
end
|
.search_conditions_with_metros_and_states(params) ⇒ Object
188
189
190
191
192
193
194
195
|
# File 'app/models/user.rb', line 188
def self.search_conditions_with_metros_and_states(params)
search = prepare_params_for_search(params)
metro_areas, states = find_country_and_state_from_search_params(search)
users = build_conditions_for_search(search)
return users, search, metro_areas, states
end
|
Instance Method Details
#activate ⇒ Object
270
271
272
273
274
275
276
|
# File 'app/models/user.rb', line 270
def activate
User.transaction do
update_attribute(:activated_at, Time.now.utc)
update_attribute(:activation_code, nil)
end
UserNotifier.activation(self).deliver
end
|
#active? ⇒ Boolean
278
279
280
|
# File 'app/models/user.rb', line 278
def active?
activation_code.nil? && !activated_at.nil?
end
|
#admin? ⇒ Boolean
373
374
375
|
# File 'app/models/user.rb', line 373
def admin?
role && role.eql?(Role[:admin])
end
|
#avatar_photo_url(size = :original) ⇒ Object
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
|
# File 'app/models/user.rb', line 245
def avatar_photo_url(size = :original)
if avatar_id
avatar.photo.url(size)
elsif facebook?
facebook_authorization.picture_url
elsif
.picture_url
else
case size
when :thumb
configatron.photo.missing_thumb.to_s
else
configatron.photo.missing_medium.to_s
end
end
end
|
#can_request_friendship_with(user) ⇒ Object
320
321
322
|
# File 'app/models/user.rb', line 320
def can_request_friendship_with(user)
!self.eql?(user) && !self.friendship_exists_with?(user)
end
|
#check_spam ⇒ Object
435
436
437
438
439
|
# File 'app/models/user.rb', line 435
def check_spam
if configatron.has_key?(:akismet_key) && self.spam?
self.errors.add(:base, :user_spam_error.l)
end
end
|
351
352
353
354
355
|
# File 'app/models/user.rb', line 351
def (page = {}, since = 1.week.ago)
page.reverse_merge :per_page => 10, :page => 1
Activity.recent.since(since).where('comments.recipient_id = ? AND activities.user_id != ?', self.id, self.id).joins("LEFT JOIN comments ON comments.id = activities.item_id AND activities.item_type = 'Comment'").page(page[:per_page]).per(page[:page])
end
|
#deactivate ⇒ Object
262
263
264
265
266
267
268
|
# File 'app/models/user.rb', line 262
def deactivate
return if admin? User.transaction do
update_attribute(:activated_at, nil)
update_attribute(:activation_code, make_activation_code)
end
end
|
#deliver_password_reset_instructions! ⇒ Object
#deliver_signup_notification ⇒ Object
#display_name ⇒ Object
369
370
371
|
# File 'app/models/user.rb', line 369
def display_name
login
end
|
#female ⇒ Object
389
390
391
|
# File 'app/models/user.rb', line 389
def female
gender && gender.eql?(FEMALE)
end
|
#friends_ids ⇒ Object
357
358
359
360
|
# File 'app/models/user.rb', line 357
def friends_ids
return [] if accepted_friendships.empty?
accepted_friendships.map{|fr| fr.friend_id }
end
|
#friendship_exists_with?(friend) ⇒ Boolean
324
325
326
|
# File 'app/models/user.rb', line 324
def friendship_exists_with?(friend)
Friendship.where("user_id = ? AND friend_id = ?", self.id, friend.id).first
end
|
#full_location ⇒ Object
294
295
296
|
# File 'app/models/user.rb', line 294
def full_location
"#{metro_area.name if self.metro_area}#{" , #{self.country.name}" if self.country}"
end
|
#has_reached_daily_friend_request_limit? ⇒ Boolean
337
338
339
|
# File 'app/models/user.rb', line 337
def has_reached_daily_friend_request_limit?
friendships_initiated_by_me.where('created_at > ?', Time.now.beginning_of_day).count >= Friendship.daily_request_limit
end
|
#invite_code ⇒ Object
286
287
288
|
# File 'app/models/user.rb', line 286
def invite_code
Digest::SHA1.hexdigest("#{self.id}--#{self.email}--#{self.password_salt}")
end
|
#last_months_posts ⇒ Object
241
242
243
|
# File 'app/models/user.rb', line 241
def last_months_posts
self.posts.where("published_at > ? and published_at < ?", DateTime.now.to_time.at_beginning_of_month.months_ago(1), DateTime.now.to_time.at_beginning_of_month)
end
|
#location ⇒ Object
290
291
292
|
# File 'app/models/user.rb', line 290
def location
metro_area && metro_area.name || ""
end
|
#male? ⇒ Boolean
385
386
387
|
# File 'app/models/user.rb', line 385
def male?
gender && gender.eql?(MALE)
end
|
#member? ⇒ Boolean
381
382
383
|
# File 'app/models/user.rb', line 381
def member?
role && role.eql?(Role[:member])
end
|
#moderator? ⇒ Boolean
377
378
379
|
# File 'app/models/user.rb', line 377
def moderator?
role && role.eql?(Role[:moderator])
end
|
#moderator_of?(forum) ⇒ Boolean
222
223
224
|
# File 'app/models/user.rb', line 222
def moderator_of?(forum)
moderatorships.where('forum_id = ?', (forum.is_a?(Forum) ? forum.id : forum)).count == 1
end
|
#monitoring_topic?(topic) ⇒ Boolean
226
227
228
|
# File 'app/models/user.rb', line 226
def monitoring_topic?(topic)
monitored_topics.find_by_id(topic.id)
end
|
#network_activity(page = {}, since = 1.week.ago) ⇒ Object
341
342
343
344
345
346
347
348
349
|
# File 'app/models/user.rb', line 341
def network_activity(page = {}, since = 1.week.ago)
page.reverse_merge! :per_page => 10, :page => 1
friend_ids = self.friends_ids
metro_area_people_ids = self.metro_area ? self.metro_area.users.map(&:id) : []
ids = ((friends_ids | metro_area_people_ids) - [self.id])[0..100]
Activity.recent.since(since).by_users(ids).page(page[:page]).per(page[:per_page])
end
|
#newpass(len) ⇒ Object
305
306
307
308
309
310
|
# File 'app/models/user.rb', line 305
def newpass( len )
chars = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a
new_password = ""
1.upto(len) { |i| new_password << chars[rand(chars.size-1)] }
return new_password
end
|
#owner ⇒ Object
312
313
314
|
# File 'app/models/user.rb', line 312
def owner
self
end
|
#recommended_posts(since = 1.week.ago) ⇒ Object
362
363
364
365
366
367
|
# File 'app/models/user.rb', line 362
def recommended_posts(since = 1.week.ago)
return [] if tags.empty?
rec_posts = Post.tagged_with(tags.map(&:name), :any => true).where(['posts.user_id != ? AND published_at > ?', self.id, since ])
rec_posts = rec_posts.order('published_at DESC').limit(10)
rec_posts
end
|
#recount_metro_area_users ⇒ Object
230
231
232
233
234
235
|
# File 'app/models/user.rb', line 230
def recount_metro_area_users
return unless self.metro_area
ma = self.metro_area
ma.users_count = User.where("metro_area_id = ?", ma.id).count
ma.save
end
|
#reset_password ⇒ Object
298
299
300
301
302
303
|
# File 'app/models/user.rb', line 298
def reset_password
new_password = newpass(8)
self.password = new_password
self.password_confirmation = new_password
return self.valid?
end
|
#staff? ⇒ Boolean
316
317
318
|
# File 'app/models/user.rb', line 316
def staff?
featured_writer?
end
|
#this_months_posts ⇒ Object
237
238
239
|
# File 'app/models/user.rb', line 237
def this_months_posts
self.posts.where("published_at > ?", DateTime.now.to_time.at_beginning_of_month)
end
|
#unread_message_count ⇒ Object
402
403
404
|
# File 'app/models/user.rb', line 402
def unread_message_count
message_threads_as_recipient.includes(:message).where("messages.recipient_id = ? AND messages.recipient_deleted = ? AND read_at IS NULL", self.id, false).references(:messages).count
end
|
#unread_messages? ⇒ Boolean
398
399
400
|
# File 'app/models/user.rb', line 398
def unread_messages?
unread_message_count > 0 ? true : false
end
|
#update_last_login ⇒ Object
332
333
334
335
|
# File 'app/models/user.rb', line 332
def update_last_login
self.track_activity(:logged_in) if self.active? && self.last_login_at.nil? || (self.last_login_at && self.last_login_at < Time.now.beginning_of_day)
self.update_attribute(:last_login_at, Time.now)
end
|
#update_last_seen_at ⇒ Object
393
394
395
396
|
# File 'app/models/user.rb', line 393
def update_last_seen_at
User.where('id = ?', self.id).update_all(['sb_last_seen_at = ?', Time.now.utc])
self.sb_last_seen_at = Time.now.utc
end
|
#valid_birthday ⇒ Object
411
412
413
414
|
# File 'app/models/user.rb', line 411
def valid_birthday
date = configatron.min_age.years.ago
errors.add(:birthday, "must be before #{date.strftime("%Y-%m-%d")}") unless birthday && (birthday.to_date <= date.to_date)
end
|
#valid_invite_code?(code) ⇒ Boolean
282
283
284
|
# File 'app/models/user.rb', line 282
def valid_invite_code?(code)
code == invite_code
end
|