From f00d29c97a96232499f1eba7b77d165d949e785e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Wed, 21 Oct 2009 00:09:26 -0200 Subject: [PATCH] Devise migratable. --- README.rdoc | 29 +++++++++-------------- TODO | 2 +- lib/devise.rb | 1 + lib/devise/migrations.rb | 51 ++++++++++++++++++++++++++++++++++++++++ test/test_helper.rb | 13 ++++------ 5 files changed, 69 insertions(+), 27 deletions(-) create mode 100644 lib/devise/migrations.rb diff --git a/README.rdoc b/README.rdoc index 7c91b3f9..d664a4f7 100644 --- a/README.rdoc +++ b/README.rdoc @@ -39,27 +39,20 @@ Devise must be setted up within the model (or models) you want to use, and devis We're assuming here you want a User model. First of all you have to setup a migration with the following fields: - # required - t.string :email, :null => false - t.string :encrypted_password, :null => false - t.string :password_salt, :null => false - - # required for confirmable - t.string :confirmation_token - t.datetime :confirmation_sent_at - t.datetime :confirmed_at - - # required for recoverable - t.string :reset_password_token - - # required for rememberable - t.string :remember_token + create_table :users do + t.authenticable + t.confirmable + t.recoverable + t.rememberable + t.timestamps + end You may also want to add some indexes to improve performance: add_index :your_table, :email add_index :your_table, :confirmation_token # for confirmable add_index :your_table, :reset_password_token # for recoverable + add_index :your_table, :remember_token # for rememberable Now let's setup a User model adding the devise line to have your authentication working: @@ -164,9 +157,9 @@ After signing in a user, confirming it's account or updating it's password, devi Devise let's you setup as many roles as you want, so let's say you already have this User model and also want an Admin model with the same authentication stuff, but not confirmation or password recovery. Just follow the same steps: # Create a migration with the required fields - t.string :email, :null => false - t.string :encrypted_password, :null => false - t.string :password_salt, :null => false + create_table :admins do |t| + t.authenticable + end # Inside your Admin model devise :validatable diff --git a/TODO b/TODO index 52aec516..2f53b1e2 100644 --- a/TODO +++ b/TODO @@ -8,7 +8,6 @@ * Devise::BruteForceProtection * Devise::MagicColumns * Devise::Invitable -* Devise::Migratable == Done @@ -16,6 +15,7 @@ * Devise::Confirmable * Devise::Recoverable * Devise::Validatable +* Devise::Migratable * SessionsController * PasswordsController * ConfirmationsController diff --git a/lib/devise.rb b/lib/devise.rb index 83fcacef..76a7d8ed 100644 --- a/lib/devise.rb +++ b/lib/devise.rb @@ -46,4 +46,5 @@ require 'devise/routes' # able to create default filters. Rails.configuration.after_initialize do ActiveRecord::Base.extend Devise::ActiveRecord + ActiveRecord::ConnectionAdapters::TableDefinition.send :include, Devise::Migrations end diff --git a/lib/devise/migrations.rb b/lib/devise/migrations.rb new file mode 100644 index 00000000..274992ae --- /dev/null +++ b/lib/devise/migrations.rb @@ -0,0 +1,51 @@ +module Devise + # Helpers to migration: + # + # create_table :accounts do |t| + # t.authenticable + # t.confirmable + # t.recoverable + # t.rememberable + # t.timestamps + # end + # + # However this method does not add indexes. If you need them, here is the declaration: + # + # add_index "accounts", ["email"], :name => "email", :unique => true + # add_index "accounts", ["remember_token"], :name => "remember_token", :unique => true + # add_index "accounts", ["confirmation_token"], :name => "confirmation_token", :unique => true + # add_index "accounts", ["reset_password_token"], :name => "reset_password_token", :unique => true + # + module Migrations + + # Creates email, encrypted_password and password_salt. + # + def authenticable + string :email, :limit => 100, :null => false + string :encrypted_password, :limit => 40, :null => false + string :password_salt, :limit => 20, :null => false + end + + # Creates confirmation_token, confirmed_at and confirmation_sent_at. + # + def confirmable + string :confirmation_token, :limit => 40, :null => true + datetime :confirmed_at + datetime :confirmation_sent_at + end + + # Creates reset_password_token. + # + def recoverable + string :reset_password_token, :limit => 40, :null => true + end + + # Creates remember_token and remember_expires_at. + # + def rememberable + string :remember_token, :limit => 40, :null => true + datetime :remember_expires_at + end + + end +end diff --git a/test/test_helper.rb b/test/test_helper.rb index 6a557a42..8a01b844 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -16,15 +16,12 @@ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":me ActiveRecord::Schema.define(:version => 1) do [:users, :admins].each do |table| create_table table do |t| - t.string :email, :null => false - t.string :encrypted_password, :null => false - t.string :password_salt, :null => false + t.authenticable + if table == :users - t.string :confirmation_token - t.datetime :confirmation_sent_at - t.datetime :confirmed_at - t.string :reset_password_token - t.string :remember_token + t.confirmable + t.recoverable + t.rememberable end t.timestamps