Devise gem: add module after initial install
This may not be Devise specific but I'm wondering how to add an additional module to a gem that has already been installed when the initial install didn't include said module? In the case of Devise the migration helper t.confirmable
is useful in the initial migration's Self.up
method and the whole User table is torn down in the Self.down
. My Rails-fu isn't strong enough to uncover what the t.confirmable
helper is actually doing...
What happens when the User
table already exists and you want to add something like :confirmable
or :token_authenticatable
? Obviously you can't just create_table(:users)
again... so me thinks I want to add_column :users, ...
and remove_c开发者_运维知识库olumn :users, ...
but how do we go about finding out what needs to happen?
Take a look at Devise::Schema
https://github.com/plataformatec/devise/blob/master/lib/devise/schema.rb
which has this
# Creates confirmation_token, confirmed_at and confirmation_sent_at.
def confirmable
apply_devise_schema :confirmation_token, String
apply_devise_schema :confirmed_at, DateTime
apply_devise_schema :confirmation_sent_at, DateTime
end
and then
https://github.com/plataformatec/devise/blob/master/lib/devise/orm/active_record.rb
def apply_devise_schema(name, type, options={})
column name, type.to_s.downcase.to_sym, options
end
So in your migration just do
add_column :users, :confirmation_token, :string
add_column :users, :confirmed_at, :datetime
add_column :users, :confirmation_sent_at, :datetime
and the opposite for the down..
Your migration:
class DeviseAddConfirmable < ActiveRecord::Migration
def change
change_table(:users) do |t|
t.confirmable
end
add_index :users, :confirmation_token, :unique => true
end
end
精彩评论