Permalink
Browse files

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

  • Loading branch information...
1 parent f8a83e0 commit 385dba551e6bec3c419b975bb7eeb59a4d78ec6a @carlosantoniodasilva carlosantoniodasilva committed Oct 23, 2009
View
@@ -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
@@ -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:
View
@@ -0,0 +1 @@
+script/generate devise User
@@ -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
@@ -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
@@ -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'
+
+================================================================================
@@ -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
@@ -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
@@ -56,9 +56,10 @@ def #{accessor}=(value)
#
# 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

0 comments on commit 385dba5

Please sign in to comment.