First generator for Rails 3.
This commit is contained in:
parent
b4bbd3b892
commit
38bfe3f990
|
@ -1,5 +0,0 @@
|
||||||
To create a devise resource user:
|
|
||||||
|
|
||||||
script/generate devise User
|
|
||||||
|
|
||||||
This will generate a model named User, a route map for devise called :users, and a migration file for table :users with all devise modules.
|
|
|
@ -1,15 +0,0 @@
|
||||||
require File.expand_path(File.dirname(__FILE__) + "/lib/route_devise.rb")
|
|
||||||
|
|
||||||
class DeviseGenerator < Rails::Generator::NamedBase
|
|
||||||
|
|
||||||
def manifest
|
|
||||||
record do |m|
|
|
||||||
m.directory(File.join('app', 'models', class_path))
|
|
||||||
m.template 'model.rb', File.join('app', 'models', "#{file_path}.rb")
|
|
||||||
|
|
||||||
m.migration_template 'migration.rb', 'db/migrate', :migration_file_name => "devise_create_#{table_name}"
|
|
||||||
m.route_devise table_name
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
|
|
@ -1,32 +0,0 @@
|
||||||
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
|
|
|
@ -1,9 +0,0 @@
|
||||||
class <%= class_name %> < ActiveRecord::Base
|
|
||||||
# Include default devise modules. Others available are:
|
|
||||||
# :http_authenticatable, :token_authenticatable, :lockable, :timeoutable and :activatable
|
|
||||||
devise :registerable, :authenticatable, :confirmable, :recoverable,
|
|
||||||
:rememberable, :trackable, :validatable
|
|
||||||
|
|
||||||
# Setup accessible (or protected) attributes for your model
|
|
||||||
attr_accessible :email, :password, :password_confirmation
|
|
||||||
end
|
|
|
@ -0,0 +1,5 @@
|
||||||
|
To create a devise resource user:
|
||||||
|
|
||||||
|
script/generate devise User
|
||||||
|
|
||||||
|
This will generate a model named User (if one does not exist), add devise configuration, a route map for devise called :users and a migration file for table :users with default devise modules.
|
|
@ -0,0 +1,54 @@
|
||||||
|
require 'rails/generators/migration'
|
||||||
|
|
||||||
|
class DeviseGenerator < Rails::Generators::NamedBase
|
||||||
|
include Rails::Generators::Migration
|
||||||
|
|
||||||
|
def self.source_root
|
||||||
|
@_devise_source_root ||= File.expand_path("../templates", __FILE__)
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.orm_has_migration?
|
||||||
|
Rails::Generators.options[:rails][:orm] == :active_record
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.next_migration_number(path)
|
||||||
|
Time.now.utc.strftime("%Y%m%d%H%M%S")
|
||||||
|
end
|
||||||
|
|
||||||
|
class_option :migration, :type => :boolean, :default => orm_has_migration?
|
||||||
|
|
||||||
|
def invoke_orm_model
|
||||||
|
if File.exists?(File.join(destination_root, model_path))
|
||||||
|
say "* Model already exists. Adding Devise behavior."
|
||||||
|
else
|
||||||
|
invoke "model", [name], :migration => false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def inject_devise_config_into_model
|
||||||
|
inject_into_class model_path, class_name, <<-CONTENT
|
||||||
|
# Include default devise modules. Others available are:
|
||||||
|
# :http_authenticatable, :token_authenticatable, :lockable, :timeoutable and :activatable
|
||||||
|
devise :registerable, :authenticatable, :confirmable, :recoverable,
|
||||||
|
:rememberable, :trackable, :validatable
|
||||||
|
|
||||||
|
# Setup accessible (or protected) attributes for your model
|
||||||
|
attr_accessible :email, :password, :password_confirmation
|
||||||
|
CONTENT
|
||||||
|
end
|
||||||
|
|
||||||
|
def copy_migration_template
|
||||||
|
return unless options.migration?
|
||||||
|
migration_template "migration.rb", "db/migrate/devise_create_#{table_name}"
|
||||||
|
end
|
||||||
|
|
||||||
|
def add_devise_routes
|
||||||
|
route "devise_for :#{table_name}"
|
||||||
|
end
|
||||||
|
|
||||||
|
protected
|
||||||
|
|
||||||
|
def model_path
|
||||||
|
@model_path ||= File.join("app", "models", "#{file_path}.rb")
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in New Issue