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

View File

@ -1,20 +1,21 @@
== 0.1.2 (development)
* bug fixes
* Fixed requiring devise strategies
* enhancements
* [#4] Allow option :null => true in authenticable migration
* [#3] Remove attr_accessible calls from devise modules
* Customizable time frame for rememberable with :remember_for config
* Customizable time frame for confirmable with :confirm_in config
* optimize
* Do not load hooks or strategies if they are not used
* enhancements
* Allow option :null => true in authenticable migration
* Customizable time frame for rememberable with :remember_for config
* Customizable time frame for confirmable with :confirm_in config
* bug fixes
* [#2] Fixed requiring devise strategies
== 0.1.1
* bug fixes
* Fixed requiring devise mapping
* [#1] Fixed requiring devise mapping
== 0.1.0

View File

@ -168,7 +168,7 @@ After signing in a user, confirming it's account or updating it's password, devi
You also need to setup default url options for the mailer, if you are using confirmable or recoverable. It's a Rails required configuration, and you can do this inside your specific environments. Here is an example of development environment:
config.action_mailer.default_url_options = {:host => 'localhost:3000'}
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
Devise let's you setup as many roles as you want, so let's say you already have this User model and also want an Admin model with the same authentication stuff, but not confirmation or password recovery. Just follow the same steps:

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

View File

@ -56,9 +56,10 @@ end
#
# 1) Include Devise::ActiveRecord and Devise::Migrations
# 2) Load and config warden
# 3) Add routes extensions
# 4) Load routes definitions
# 5) Include filters and helpers in controllers and views
# 3) Load devise mapping structure
# 4) Add routes extensions
# 5) Load routes definitions
# 6) Include filters and helpers in controllers and views
#
Rails.configuration.after_initialize do
ActiveRecord::Base.extend Devise::ActiveRecord