Come up with a gem: add a module after initial installation

It may not be Devise , but I wonder how to add an additional module to the stone that was already installed when the initial installation did not include the specified module? In the case of Devise, the t.confirmable migration t.confirmable is useful in the initial migration method Self.up , and the entire user table is reset to Self.down . My Rails-fu is not strong enough to reveal what the t.confirmable helper t.confirmable ...

What happens when the User table already exists and you want to add something like :confirmable or :token_authenticatable ? Obviously, you again cannot just create_table(:users) ... so I think I want add_column :users, ... and remove_column :users, ... , but how do we know what should happen?

+4
source share
2 answers

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

  add_column :users, :confirmation_token, :string add_column :users, :confirmed_at, :datetime add_column :users, :confirmation_sent_at, :datetime 

and vice versa for down ..

+4
source

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 
0
source

All Articles