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:
parent
f8a83e02d8
commit
385dba551e
9 changed files with 117 additions and 11 deletions
|
@ -1,20 +1,21 @@
|
||||||
== 0.1.2 (development)
|
== 0.1.2 (development)
|
||||||
|
|
||||||
* bug fixes
|
* enhancements
|
||||||
* Fixed requiring devise strategies
|
* [#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
|
* optimize
|
||||||
* Do not load hooks or strategies if they are not used
|
* Do not load hooks or strategies if they are not used
|
||||||
|
|
||||||
* enhancements
|
* bug fixes
|
||||||
* Allow option :null => true in authenticable migration
|
* [#2] Fixed requiring devise strategies
|
||||||
* Customizable time frame for rememberable with :remember_for config
|
|
||||||
* Customizable time frame for confirmable with :confirm_in config
|
|
||||||
|
|
||||||
== 0.1.1
|
== 0.1.1
|
||||||
|
|
||||||
* bug fixes
|
* bug fixes
|
||||||
* Fixed requiring devise mapping
|
* [#1] Fixed requiring devise mapping
|
||||||
|
|
||||||
== 0.1.0
|
== 0.1.0
|
||||||
|
|
||||||
|
|
|
@ -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:
|
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:
|
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
1
generators/devise/USAGE
Normal file
|
@ -0,0 +1 @@
|
||||||
|
script/generate devise User
|
25
generators/devise/devise_generator.rb
Normal file
25
generators/devise/devise_generator.rb
Normal 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
|
32
generators/devise/lib/route_devise.rb
Normal file
32
generators/devise/lib/route_devise.rb
Normal 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
|
21
generators/devise/templates/README
Normal file
21
generators/devise/templates/README
Normal 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'
|
||||||
|
|
||||||
|
================================================================================
|
20
generators/devise/templates/migration.rb
Normal file
20
generators/devise/templates/migration.rb
Normal 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
|
5
generators/devise/templates/model.rb
Normal file
5
generators/devise/templates/model.rb
Normal 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
|
|
@ -56,9 +56,10 @@ end
|
||||||
#
|
#
|
||||||
# 1) Include Devise::ActiveRecord and Devise::Migrations
|
# 1) Include Devise::ActiveRecord and Devise::Migrations
|
||||||
# 2) Load and config warden
|
# 2) Load and config warden
|
||||||
# 3) Add routes extensions
|
# 3) Load devise mapping structure
|
||||||
# 4) Load routes definitions
|
# 4) Add routes extensions
|
||||||
# 5) Include filters and helpers in controllers and views
|
# 5) Load routes definitions
|
||||||
|
# 6) Include filters and helpers in controllers and views
|
||||||
#
|
#
|
||||||
Rails.configuration.after_initialize do
|
Rails.configuration.after_initialize do
|
||||||
ActiveRecord::Base.extend Devise::ActiveRecord
|
ActiveRecord::Base.extend Devise::ActiveRecord
|
||||||
|
|
Loading…
Reference in a new issue