OSDN Git Service

merge
[pettanr/pettanr.git] / app / models / user.rb
index d59f35e..d9872ca 100644 (file)
@@ -5,15 +5,23 @@ class User < ActiveRecord::Base
   # Include default devise modules. Others available are:
   # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
   devise :database_authenticatable, :registerable,
-         :recoverable, :rememberable, :trackable, :token_authenticatable, :validatable#, :confirmable
-
-  # Setup accessible (or protected) attributes for your model
-  attr_accessible :id, :name, :password, :password_confirmation, :remember_me , :email
+         :recoverable, :rememberable, :trackable,  :validatable, 
+         :omniauthable#, :confirmable
 
   def create_token
-    self.ensure_authentication_token
+    loop do
+      token = Devise.friendly_token
+      if token_suitable?(token)
+        self.authentication_token = token
+        break
+      end
+    end
     self.save
   end
+
+  def token_suitable?(token)
+    !self.class.exists?(authentication_token: token)
+  end
   
   def delete_token
     self.authentication_token = nil
@@ -42,4 +50,9 @@ class User < ActiveRecord::Base
     res
   end
   
+  private
+  def user_params
+    params.require(:user).permit(:email, :password, :password_confirmation, :remember_me, :authentication_token)
+  end
+  
 end