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

Adding simple devise generator to create a model, migration and route.

This commit is contained in:
Carlos A. da Silva 2009-10-23 10:13:23 -02:00
parent f8a83e02d8
commit 385dba551e
9 changed files with 117 additions and 11 deletions

1
generators/devise/USAGE Normal file
View file

@ -0,0 +1 @@
script/generate devise User

View file

@ -0,0 +1,25 @@
require File.expand_path(File.dirname(__FILE__) + "/lib/route_devise.rb")
class DeviseGenerator < Rails::Generator::NamedBase
def manifest
record do |m|
# Check for class naming collisions.
m.class_collisions(class_name)
# Model
m.directory(File.join('app', 'models', class_path))
m.template 'model.rb', File.join('app', 'models', "#{file_path}.rb")
# Migration
m.migration_template 'migration.rb', 'db/migrate', :migration_file_name => "devise_create_#{table_name}"
# Routing
m.route_devise table_name
# Readme
m.readme "README"
end
end
end

View file

@ -0,0 +1,32 @@
module Rails
module Generator
module Commands
class Create < Base
# Create devise route. Based on route_resources
def route_devise(*resources)
resource_list = resources.map { |r| r.to_sym.inspect }.join(', ')
sentinel = 'ActionController::Routing::Routes.draw do |map|'
logger.route "map.devise_for #{resource_list}"
unless options[:pretend]
gsub_file 'config/routes.rb', /(#{Regexp.escape(sentinel)})/mi do |match|
"#{match}\n map.devise_for #{resource_list}\n"
end
end
end
end
class Destroy < RewindBase
# Destroy devise route. Based on route_resources
def route_devise(*resources)
resource_list = resources.map { |r| r.to_sym.inspect }.join(', ')
look_for = "\n map.devise_for #{resource_list}\n"
logger.route "map.devise_for #{resource_list}"
gsub_file 'config/routes.rb', /(#{look_for})/mi, ''
end
end
end
end
end

View file

@ -0,0 +1,21 @@
================================================================================
Some setup you must do manually if you haven't yet:
1. Setup defaut url options for your specific environment. Here is an example of development environment:
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
It's a Rails required configuration.
In production it must be the actual host your application is deployed to.
2. Setup default sender for mails.In config/environment.rb:
Notifier.sender = "test@example.com"
3. Ensure you have defined root_url to *something* in your config/routes.rb:
map.root :controller => 'home'
================================================================================

View file

@ -0,0 +1,20 @@
class DeviseCreate<%= table_name.camelize %> < ActiveRecord::Migration
def self.up
create_table(:<%= table_name %>) do |t|
t.authenticable
t.confirmable
t.recoverable
t.rememberable
t.timestamps
end
add_index :<%= table_name %>, :email, :unique => true
add_index :<%= table_name %>, :confirmation_token, :unique => true
add_index :<%= table_name %>, :reset_password_token, :unique => true
end
def self.down
drop_table :<%= table_name %>
end
end

View file

@ -0,0 +1,5 @@
class <%= class_name %> < ActiveRecord::Base
devise :all
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation
end