From 790ed1b7916049fc681fc514175c0cebda6f9597 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E6=83=8A=E5=9D=A4?= Date: Sun, 29 Nov 2015 14:12:37 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90user=E7=9A=84=E5=AD=97?= =?UTF-8?q?=E6=AE=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models/user.rb | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/app/models/user.rb b/app/models/user.rb index e029173..fdbe3e4 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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 :password, length: {minimum: 6}, confirmation: true, if: :need_valid_password? 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