Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added a template to create a migration when the model already exists.…

… Changed the generator code to check if model exists, and if it does, call the new template instead of the standard template.
  • Loading branch information...
commit 9629da863dfaed359d26f79faa923a3544681861 1 parent 4f76e6f
@seanski authored
View
6 lib/generators/active_record/devise_generator.rb
@@ -14,7 +14,11 @@ def generate_model
end
def copy_devise_migration
- migration_template "migration.rb", "db/migrate/devise_create_#{table_name}"
+ unless model_exists?
+ migration_template "migration.rb", "db/migrate/devise_create_#{table_name}"
+ else
+ migration_template "migration_existing.rb", "db/migrate/add_devise_to_#{table_name}"
+ end
end
def inject_devise_content
View
32 lib/generators/active_record/templates/migration_existing.rb
@@ -0,0 +1,32 @@
+class AddDeviseTo<%= table_name.camelize %> < ActiveRecord::Migration
+ def self.up
+ change_table :<%= table_name %> do |t|
+ t.database_authenticatable :null => false
+ t.recoverable
+ t.rememberable
+ t.trackable
+
+ # t.encryptable
+ # t.confirmable
+ # t.lockable :lock_strategy => :<%= Devise.lock_strategy %>, :unlock_strategy => :<%= Devise.unlock_strategy %>
+ # t.token_authenticatable
+
+<% for attribute in attributes -%>
+ t.<%= attribute.type %> :<%= attribute.name %>
+<% end -%>
+ #Uncomment below if timestamps were not included in your original model.
+ t.timestamps
+ end
+ add_index :<%= table_name %>, :email, :unique => true
+ add_index :<%= table_name %>, :reset_password_token, :unique => true
+ # add_index :<%= table_name %>, :confirmation_token, :unique => true
+ # add_index :<%= table_name %>, :unlock_token, :unique => true
+ # add_index :<%= table_name %>, :authentication_token, :unique => true
+ end
+
+ def self.down
+ #By default, we don't want to make any assumption about how to roll back a migration when your
+ #model already existed. Please edit below which fields you would like to remove in this migration.
+ raise ActiveRecord::IrreversibleMigration
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.