OSDN Git Service

Implementing automatic password creation.
authorJakub Troszok <troszok@gmail.com>
Sun, 24 Jun 2012 20:26:13 +0000 (22:26 +0200)
committerJakub Troszok <troszok@gmail.com>
Sun, 24 Jun 2012 20:26:13 +0000 (22:26 +0200)
app/models/user.rb

index 69d5ec7..484853b 100644 (file)
@@ -53,6 +53,14 @@ class User < ActiveRecord::Base
   scope :blocked, where(:blocked =>  true)
   scope :active, where(:blocked =>  false)
 
+  before_validation :generate_password, :on1 => :create
+
+  def generate_password
+    if self.password.blank? && self.password_confirmation.blank?
+      self.password = self.password_confirmation = Devise.friendly_token.first(8)
+    end
+  end
+
   def self.filter filter_name
     case filter_name
     when "admins"; self.admins