Class: User

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
FacebookProfile, Rakismet::Model, TwitterProfile, UrlUpload
Defined in:
app/models/user.rb

Constant Summary collapse

MALE =
'M'
FEMALE =
'F'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from TwitterProfile

included

Methods included from FacebookProfile

included

Methods included from UrlUpload

#data_from_url, #validate

Instance Attribute Details

#authorizing_from_omniauthObject

Returns the value of attribute authorizing_from_omniauth.



120
121
122
# File 'app/models/user.rb', line 120

def authorizing_from_omniauth
  @authorizing_from_omniauth
end

Class Method Details

.build_conditions_for_search(search) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'app/models/user.rb', line 158

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('`users`.login LIKE ?', "%#{search['login']}%")
  end
  if search['vendor']
    users = users.where(user[:vendor].eq(true))
  end
  if search['description']
    users = users.where('`users`.description LIKE ?', "%#{search['description']}%")
  end
  users
end

.build_search_conditions(query) ⇒ Object



225
226
227
# File 'app/models/user.rb', line 225

def self.build_search_conditions(query)
  query
end

.currently_onlineObject



215
216
217
# File 'app/models/user.rb', line 215

def self.currently_online
  User.find(:all, :conditions => ["sb_last_seen_at > ?", Time.now.utc-5.minutes])
end

.find_by_activity(options = {}) ⇒ Object



182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'app/models/user.rb', line 182

def self.find_by_activity(options = {})
  options.reverse_merge! :limit => 30, :require_avatar => true, :since => 7.days.ago

  activities = Activity.since(options[:since]).find(:all,
    :select => 'activities.user_id, count(*) as count',
    :group => 'activities.user_id',
    :conditions => "#{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

Class Methods



124
125
126
# File 'app/models/user.rb', line 124

def self.(string)
  self.first(:conditions => ["LOWER(email) = ? OR LOWER(login) = ?", string.downcase, string.downcase])
end

.find_country_and_state_from_search_params(search) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'app/models/user.rb', line 128

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.all(:order => "name") : []
  else
    metro_areas = country ? country.metro_areas : []
  end

  return [metro_areas, states]
end


196
197
198
# File 'app/models/user.rb', line 196

def self.find_featured
  self.featured
end

.find_or_create_from_authorization(auth) ⇒ Object



428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
# File 'app/models/user.rb', line 428

def self.find_or_create_from_authorization(auth)
  user = User.find_or_initialize_by_email(:email => auth.email)
  user. ||= 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



150
151
152
153
154
155
156
# File 'app/models/user.rb', line 150

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



210
211
212
213
# File 'app/models/user.rb', line 210

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



219
220
221
222
223
# File 'app/models/user.rb', line 219

def self.search(query, options = {})
  with_scope :find => { :conditions => build_search_conditions(query) } do
    find :all, options
  end
end

.search_conditions_with_metros_and_states(params) ⇒ Object



200
201
202
203
204
205
206
207
# File 'app/models/user.rb', line 200

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

#activateObject



282
283
284
285
286
287
288
# File 'app/models/user.rb', line 282

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

Returns:

  • (Boolean)


290
291
292
# File 'app/models/user.rb', line 290

def active?
  activation_code.nil? && !activated_at.nil?
end

#admin?Boolean

Returns:

  • (Boolean)


385
386
387
# File 'app/models/user.rb', line 385

def admin?
  role && role.eql?(Role[:admin])
end

#avatar_photo_url(size = :original) ⇒ Object



257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'app/models/user.rb', line 257

def avatar_photo_url(size = :original)
  if avatar_id
    avatar.photo.url(size)
  elsif facebook?
    facebook_authorization.picture_url
  elsif twitter?
    twitter_authorization.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



332
333
334
# File 'app/models/user.rb', line 332

def can_request_friendship_with(user)
  !self.eql?(user) && !self.friendship_exists_with?(user)
end

#check_spamObject



447
448
449
450
451
# File 'app/models/user.rb', line 447

def check_spam
  if !configatron.akismet_key.nil? && self.spam?
    self.errors.add(:base, :user_spam_error.l)
  end
end

#comments_activity(page = {}, since = 1.week.ago) ⇒ Object



363
364
365
366
367
# File 'app/models/user.rb', line 363

def comments_activity(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

#deactivateObject



274
275
276
277
278
279
280
# File 'app/models/user.rb', line 274

def deactivate
  return if admin? #don't allow admin deactivation
  User.transaction do
    update_attribute(:activated_at, nil)
    update_attribute(:activation_code, make_activation_code)
  end
end

#deliver_password_reset_instructions!Object



418
419
420
421
# File 'app/models/user.rb', line 418

def deliver_password_reset_instructions!
  reset_perishable_token!
  UserNotifier.password_reset_instructions(self).deliver
end

#deliver_signup_notificationObject



340
341
342
# File 'app/models/user.rb', line 340

def 
  UserNotifier.(self).deliver
end

#display_nameObject



381
382
383
# File 'app/models/user.rb', line 381

def display_name
  
end

#femaleObject



401
402
403
# File 'app/models/user.rb', line 401

def female
  gender && gender.eql?(FEMALE)
end

#friends_idsObject



369
370
371
372
# File 'app/models/user.rb', line 369

def friends_ids
  return [] if accepted_friendships.empty?
  accepted_friendships.map{|fr| fr.friend_id }
end

#friendship_exists_with?(friend) ⇒ Boolean

Returns:

  • (Boolean)


336
337
338
# File 'app/models/user.rb', line 336

def friendship_exists_with?(friend)
  Friendship.find(:first, :conditions => ["user_id = ? AND friend_id = ?", self.id, friend.id])
end

#full_locationObject



306
307
308
# File 'app/models/user.rb', line 306

def full_location
  "#{metro_area.name if self.metro_area}#{" , #{self.country.name}" if self.country}"
end

#has_reached_daily_friend_request_limit?Boolean

Returns:

  • (Boolean)


349
350
351
# File 'app/models/user.rb', line 349

def has_reached_daily_friend_request_limit?
  friendships_initiated_by_me.count(:conditions => ['created_at > ?', Time.now.beginning_of_day]) >= Friendship.daily_request_limit
end

#invite_codeObject



298
299
300
# File 'app/models/user.rb', line 298

def invite_code
  Digest::SHA1.hexdigest("#{self.id}--#{self.email}--#{self.password_salt}")
end

#last_months_postsObject



253
254
255
# File 'app/models/user.rb', line 253

def last_months_posts
  self.posts.find(:all, :conditions => ["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

#locationObject



302
303
304
# File 'app/models/user.rb', line 302

def location
  metro_area && metro_area.name || ""
end

#male?Boolean

Returns:

  • (Boolean)


397
398
399
# File 'app/models/user.rb', line 397

def male?
  gender && gender.eql?(MALE)
end

#member?Boolean

Returns:

  • (Boolean)


393
394
395
# File 'app/models/user.rb', line 393

def member?
  role && role.eql?(Role[:member])
end

#moderator?Boolean

Returns:

  • (Boolean)


389
390
391
# File 'app/models/user.rb', line 389

def moderator?
  role && role.eql?(Role[:moderator])
end

#moderator_of?(forum) ⇒ Boolean

Instance Methods

Returns:

  • (Boolean)


234
235
236
# File 'app/models/user.rb', line 234

def moderator_of?(forum)
  moderatorships.count(:all, :conditions => ['forum_id = ?', (forum.is_a?(Forum) ? forum.id : forum)]) == 1
end

#monitoring_topic?(topic) ⇒ Boolean

Returns:

  • (Boolean)


238
239
240
# File 'app/models/user.rb', line 238

def monitoring_topic?(topic)
  monitored_topics.find_by_id(topic.id)
end

#network_activity(page = {}, since = 1.week.ago) ⇒ Object



353
354
355
356
357
358
359
360
361
# File 'app/models/user.rb', line 353

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] #don't pull TOO much activity for now

  Activity.recent.since(since).by_users(ids).page(page[:page]).per(page[:per_page])
end

#newpass(len) ⇒ Object



317
318
319
320
321
322
# File 'app/models/user.rb', line 317

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

#ownerObject



324
325
326
# File 'app/models/user.rb', line 324

def owner
  self
end


374
375
376
377
378
379
# File 'app/models/user.rb', line 374

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_usersObject



242
243
244
245
246
247
# File 'app/models/user.rb', line 242

def recount_metro_area_users
  return unless self.metro_area
  ma = self.metro_area
  ma.users_count = User.count(:conditions => ["metro_area_id = ?", ma.id])
  ma.save
end

#reset_passwordObject



310
311
312
313
314
315
# File 'app/models/user.rb', line 310

def reset_password
   new_password = newpass(8)
   self.password = new_password
   self.password_confirmation = new_password
   return self.valid?
end

#staff?Boolean

Returns:

  • (Boolean)


328
329
330
# File 'app/models/user.rb', line 328

def staff?
  featured_writer?
end

#this_months_postsObject



249
250
251
# File 'app/models/user.rb', line 249

def this_months_posts
  self.posts.find(:all, :conditions => ["published_at > ?", DateTime.now.to_time.at_beginning_of_month])
end

#unread_message_countObject



414
415
416
# File 'app/models/user.rb', line 414

def unread_message_count
  message_threads_as_recipient.count(:conditions => ["messages.recipient_id = ? AND messages.recipient_deleted = ? AND read_at IS NULL", self.id, false], :include => :message)
end

#unread_messages?Boolean

Returns:

  • (Boolean)


410
411
412
# File 'app/models/user.rb', line 410

def unread_messages?
  unread_message_count > 0 ? true : false
end

#update_last_loginObject



344
345
346
347
# File 'app/models/user.rb', line 344

def 
  self.track_activity(:logged_in) if self.active? && self..nil? || (self. && self. < Time.now.beginning_of_day)
  self.update_attribute(:last_login_at, Time.now)
end

#update_last_seen_atObject



405
406
407
408
# File 'app/models/user.rb', line 405

def update_last_seen_at
  User.update_all ['sb_last_seen_at = ?', Time.now.utc], ['id = ?', self.id]
  self.sb_last_seen_at = Time.now.utc
end

#valid_birthdayObject



423
424
425
426
# File 'app/models/user.rb', line 423

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

Returns:

  • (Boolean)


294
295
296
# File 'app/models/user.rb', line 294

def valid_invite_code?(code)
  code == invite_code
end