Retrospring/app/models/user.rb

135 lines
4.2 KiB
Ruby
Raw Normal View History

2014-08-01 03:07:16 -07:00
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
2014-08-01 06:27:08 -07:00
:recoverable, :rememberable, :trackable,
:validatable, :authentication_keys => [:login]
2014-11-30 10:43:22 -08:00
2014-08-01 06:27:08 -07:00
# attr_accessor :login
2014-11-30 10:43:22 -08:00
2014-10-27 22:36:38 -07:00
has_many :questions, dependent: :destroy
has_many :answers, dependent: :destroy
has_many :comments, dependent: :destroy
2014-11-10 14:45:36 -08:00
has_many :inboxes, dependent: :destroy
2014-11-30 05:43:35 -08:00
has_many :active_relationships, class_name: 'Relationship',
foreign_key: 'source_id',
dependent: :destroy
has_many :passive_relationships, class_name: 'Relationship',
foreign_key: 'target_id',
dependent: :destroy
has_many :friends, through: :active_relationships, source: :target
has_many :followers, through: :passive_relationships, source: :source
2014-11-30 11:31:22 -08:00
has_many :smiles
2014-12-12 12:42:34 -08:00
has_many :services
2014-12-14 05:34:51 -08:00
has_many :notifications, foreign_key: :recipient_id
2014-11-30 05:43:35 -08:00
2014-08-01 06:27:08 -07:00
SCREEN_NAME_REGEX = /\A[a-zA-Z0-9_]{1,16}\z/
WEBSITE_REGEX = /https?:\/\/([A-Za-z.\-]+)\/?(?:.*)/i
2014-11-30 10:43:22 -08:00
2014-08-01 06:27:08 -07:00
validates :screen_name, presence: true, format: { with: SCREEN_NAME_REGEX }, uniqueness: { case_sensitive: false }
2014-12-12 09:52:56 -08:00
validates :display_name, length: { maximum: 50 }
2014-12-16 03:48:40 -08:00
validates :bio, length: { maximum: 200 }
2014-12-12 09:52:56 -08:00
2014-12-01 14:17:16 -08:00
# validates :website, format: { with: WEBSITE_REGEX }
2014-12-26 16:22:13 -08:00
before_save do
2014-12-26 16:45:24 -08:00
self.display_name = 'WRYYYYYYYY' if display_name == 'Dio Brando'
2014-12-26 16:22:13 -08:00
self.website = if website.match %r{\Ahttps?://}
website
else
"http://#{website}"
end unless website.blank?
end
2014-08-01 06:27:08 -07:00
def login=(login)
@login = login
end
def login
@login || self.screen_name || self.email
end
2014-11-30 10:43:22 -08:00
2014-08-01 06:27:08 -07:00
def self.find_first_by_auth_conditions(warden_conditions)
conditions = warden_conditions.dup
if login = conditions.delete(:login)
where(conditions).where(["lower(screen_name) = :value OR lower(email) = :value", { :value => login.downcase }]).first
else
where(conditions).first
end
end
2014-11-30 06:39:13 -08:00
# @return [Array] the users' timeline
def timeline
Answer.where("user_id in (?) OR user_id = ?", friend_ids, id).order(:created_at).reverse_order
end
# follows an user.
def follow(target_user)
2014-12-14 05:58:29 -08:00
relationship = active_relationships.create(target: target_user)
Notification.notify target_user, relationship
# increment counts
increment! :friend_count
target_user.increment! :follower_count
end
2014-11-30 06:39:13 -08:00
# unfollows an user
def unfollow(target_user)
2014-12-14 12:35:24 -08:00
relationship = active_relationships.find_by(target: target_user).destroy
2014-12-14 06:42:37 -08:00
Notification.denotify target_user, relationship
# decrement counts
decrement! :friend_count
target_user.decrement! :follower_count
end
2014-11-30 11:31:22 -08:00
# @return [Boolean] true if +self+ is following +target_user+
def following?(target_user)
friends.include? target_user
end
2014-11-30 10:43:22 -08:00
# smiles an answer
# @param answer [Answer] the answer to smile
def smile(answer)
2014-12-14 06:06:10 -08:00
smile = Smile.create(user: self, answer: answer)
Notification.notify answer.user, smile unless answer.user == self
2014-11-30 10:43:22 -08:00
increment! :smiled_count
answer.increment! :smile_count
end
# unsmile an answer
# @param answer [Answer] the answer to unsmile
def unsmile(answer)
2014-12-14 06:42:37 -08:00
smile = Smile.find_by(user: self, answer: answer).destroy
Notification.denotify answer.user, smile unless answer.user == self
2014-11-30 10:43:22 -08:00
decrement! :smiled_count
answer.decrement! :smile_count
end
2014-11-30 11:31:22 -08:00
def smiled?(answer)
# TODO: you know what to do here, nilsding
answer.smiles.each { |s| return true if s.user_id == self.id }
false
end
def display_website
website.match(/https?:\/\/([A-Za-z.\-]+)\/?(?:.*)/i)[1]
rescue NoMethodError
website
end
2014-12-05 05:11:08 -08:00
def comment(answer, content)
2014-12-14 06:10:23 -08:00
comment = Comment.create!(user: self, answer: answer, content: content)
Notification.notify answer.user, comment unless answer.user == self
2014-12-05 05:11:08 -08:00
increment! :commented_count
answer.increment! :comment_count
end
# @return [Boolean] is the user a moderator?
def mod?
return true if self.moderator? or self.admin?
false
end
2014-08-01 03:07:16 -07:00
end