1
0
Fork 0
mirror of https://github.com/heartcombo/devise.git synced 2022-11-09 12:18:31 -05:00

Devise migratable.

This commit is contained in:
José Valim 2009-10-21 00:09:26 -02:00
parent b3f68ab287
commit f00d29c97a
5 changed files with 69 additions and 27 deletions

View file

@ -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

2
TODO
View file

@ -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

View file

@ -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

51
lib/devise/migrations.rb Normal file
View file

@ -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

View file

@ -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