Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modifying devise modules after first generation

I'm in the process of learning rails. I've found Devise to be great with getting authentication up and running quickly and seamlessly but I do have one question.

How do I change the modules after the first run of the Devise generator (e.g. rails g devise User)? This defaults with the following migration:

def self.up
  create_table(:users) do |t|
    t.database_authenticatable :null => false
    t.recoverable
    t.rememberable
    t.trackable

    # t.confirmable
    # t.lockable :lock_strategy => :failed_attempts, :unlock_strategy => :both
    # t.token_authenticatable

    t.timestamps
  end

  add_index :users, :email,                :unique => true
  add_index :users, :reset_password_token, :unique => true
  # add_index :users, :confirmation_token,   :unique => true
  # add_index :users, :unlock_token,         :unique => true
end

If I've run this migration, how do I add/remove some of those modules at a later stage? E.g. Maybe I want to add lockable to an existing User model. I understand how to make the changes in the model and devise.rb but I'm not sure what to do with the migrations.

Apologies if the answer is here already, I've trawled for a couple of hours here and in google and couldn't find anything. Maybe I'm searching for the wrong thing.

Thanks in advance!
Jason
ps. I'm using
rails 3.0.0
devise 1.1.3

like image 638
Jason Avatar asked Sep 29 '10 00:09

Jason


1 Answers

I was looking for answer to the same question, and luckily, happened to be sitting next to someone who knew how to do it.

Here is the example of updating the users model to include confirmable module through a migration script (the skeleton script file generated with 'rails generate migration add_confirmable_to_users'):

class AddConfirmableToUser < ActiveRecord::Migration
  def self.up
    change_table :users do |t|
      t.confirmable
    end
    add_index :users, :confirmation_token,   :unique => true
  end

  def self.down
    remove_column :users, :confirmable
    remove_index :users, :confirmation_token
  end
end
like image 113
Prakash Murthy Avatar answered Oct 28 '22 06:10

Prakash Murthy