完成user的字段

This commit is contained in:
刘惊坤 2015-11-29 14:12:37 +08:00
parent 9dd05e6bd6
commit 790ed1b791
1 changed files with 22 additions and 0 deletions

View File

@ -9,5 +9,27 @@ class User < ActiveRecord::Base
validates :email, format: {with: /\A[a-zA-Z0-9\-]+@[a-zA-Z0-9-]+\.(org|com|cn|io|net|cc|me)\z/}, uniqueness: true validates :email, format: {with: /\A[a-zA-Z0-9\-]+@[a-zA-Z0-9-]+\.(org|com|cn|io|net|cc|me)\z/}, uniqueness: true
validates :password, length: {minimum: 6}, confirmation: true, if: :need_valid_password? validates :password, length: {minimum: 6}, confirmation: true, if: :need_valid_password?
validates :nick_name, length: {minimum: 2, maximum: 20}, uniqueness: true, if: 'nick_name.present?' validates :nick_name, length: {minimum: 2, maximum: 20}, uniqueness: true, if: 'nick_name.present?'
def nickname
self.nick_name || self.username
end
def set_admin
self.admin = 0 unless self.admin.present?
end
def check_password(password)
self.authenticate(password)
end
def update_last_reply_time
self.update_attribute last_reply_time: DateTime.now
end
def can_reply?
(DateTime.now.to_i - self.last_reply_time.to_i) > 60
end
def need_valid_password?
new_record? || password.present?
end
end end