Update generators to use Rails 3 syntax, i.e devise:install instead of devise_install.

This commit is contained in:
José Valim 2010-06-13 13:04:24 +02:00
parent 31910b85a2
commit 1f4a31f1cf
14 changed files with 181 additions and 175 deletions

View File

@ -10,11 +10,13 @@
* bug fix
* confirmation_required? is properly honored on active? calls. (by github.com/paulrosania)
* Fix a bug where session was timing out on sign out
* deprecations
* bcrypt is now the default encryptor
* devise.mailer.confirmations_instructions now should be devise.mailer.confirmations_instructions.subject
* devise.mailer.user.confirmations_instructions now should be devise.mailer.confirmations_instructions.user_subject
* Generators now use Rails 3 syntax (devise:install) instead of devise_install
== 1.1.rc

View File

@ -38,7 +38,7 @@ Devise master branch now supports Rails 3 and is NOT backward compatible. You ca
After you install Devise and add it to your Gemfile, you need to run the generator:
rails generate devise_install
rails generate devise:install
The generator will install an initializer which describes ALL Devise's configuration options and you MUST take a look at it. When you are done, you are ready to add Devise to any of your models using the generator:
@ -176,7 +176,7 @@ The devise method in your models also accepts some options to configure its modu
devise :database_authenticatable, :confirmable, :recoverable, :encryptor => :bcrypt
Besides :encryptor, you can define :pepper, :stretches, :confirm_within, :remember_for, :timeout_in, :unlock_in and other values. For details, see the initializer file that was created when you invoked the devise_install generator described above.
Besides :encryptor, you can define :pepper, :stretches, :confirm_within, :remember_for, :timeout_in, :unlock_in and other values. For details, see the initializer file that was created when you invoked the "devise:install" generator described above.
== Configuring views
@ -184,7 +184,7 @@ We built Devise to help you quickly develop an application that uses authenticat
Since Devise is an engine, all its views are packaged inside the gem. These views will help you get started, but after sometime you may want to change them. If this is the case, you just need to invoke the following generator, and it will copy all views to your application:
rails generate devise_views
rails generate devise:views
However, if you have more than one role in your application (such as "User" and "Admin"), you will notice that Devise uses the same views for all roles. Fortunately, Devise offers an easy way to customize views. All you need to do is set "config.scoped_views = true" inside "config/initializers/devise.rb".

View File

@ -0,0 +1,83 @@
require 'rails/generators/migration'
module Devise
module Generators
class DeviseGenerator < Rails::Generators::NamedBase
include Rails::Generators::Migration
source_root File.expand_path("../templates", __FILE__)
desc "Generates a model with the given NAME (if one does not exist) with devise " <<
"configuration plus a migration file and devise routes."
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 :orm
class_option :migration, :type => :boolean, :default => orm_has_migration?
def invoke_orm_model
if model_exists?
say "* Model already exists. Adding Devise behavior."
elsif options[:orm].present?
invoke "model", [name], :migration => false, :orm => options[:orm]
unless model_exists?
abort "Tried to invoke the model generator for '#{options[:orm]}' but could not find it.\n" <<
"Please create your model by hand before calling `rails g devise #{name}`."
end
else
abort "Cannot create a devise model because config.generators.orm is blank.\n" <<
"Please create your model by hand or configure your generators orm before calling `rails g devise #{name}`."
end
end
def inject_devise_config_into_model
devise_class_setup = <<-CONTENT
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable, :lockable and :timeoutable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
CONTENT
case options[:orm].to_s
when "mongoid"
inject_into_file model_path, devise_class_setup, :after => "include Mongoid::Document\n"
when "data_mapper"
inject_into_file model_path, devise_class_setup, :after => "include DataMapper::Resource\n"
when "active_record"
inject_into_class model_path, class_name, devise_class_setup + <<-CONTENT
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation
CONTENT
end
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_exists?
File.exists?(File.join(destination_root, model_path))
end
def model_path
@model_path ||= File.join("app", "models", "#{file_path}.rb")
end
end
end
end

View File

@ -1,81 +0,0 @@
require 'rails/generators/migration'
class DeviseGenerator < Rails::Generators::NamedBase
include Rails::Generators::Migration
desc "Generates a model with the given NAME (if one does not exist) with devise " <<
"configuration plus a migration file and devise routes."
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 :orm
class_option :migration, :type => :boolean, :default => orm_has_migration?
def invoke_orm_model
if model_exists?
say "* Model already exists. Adding Devise behavior."
elsif options[:orm].present?
invoke "model", [name], :migration => false, :orm => options[:orm]
unless model_exists?
abort "Tried to invoke the model generator for '#{options[:orm]}' but could not find it.\n" <<
"Please create your model by hand before calling `rails g devise #{name}`."
end
else
abort "Cannot create a devise model because config.generators.orm is blank.\n" <<
"Please create your model by hand or configure your generators orm before calling `rails g devise #{name}`."
end
end
def inject_devise_config_into_model
devise_class_setup = <<-CONTENT
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable, :lockable and :timeoutable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
CONTENT
case options[:orm].to_s
when "mongoid"
inject_into_file model_path, devise_class_setup, :after => "include Mongoid::Document\n"
when "data_mapper"
inject_into_file model_path, devise_class_setup, :after => "include DataMapper::Resource\n"
when "active_record"
inject_into_class model_path, class_name, devise_class_setup + <<-CONTENT
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation
CONTENT
end
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_exists?
File.exists?(File.join(destination_root, model_path))
end
def model_path
@model_path ||= File.join("app", "models", "#{file_path}.rb")
end
end

View File

@ -0,0 +1,24 @@
require 'active_support/secure_random'
module Devise
module Generators
class InstallGenerator < Rails::Generators::Base
source_root File.expand_path("../templates", __FILE__)
desc "Creates a Devise initializer and copy locale files to your application."
class_option :orm
def copy_initializer
template "devise.rb", "config/initializers/devise.rb"
end
def copy_locale
copy_file "../../../../../config/locales/en.yml", "config/locales/devise.en.yml"
end
def show_readme
readme "README"
end
end
end
end

View File

@ -0,0 +1,63 @@
module Devise
module Generators
class ViewsGenerator < Rails::Generators::Base
source_root File.expand_path("../../../../../app/views", __FILE__)
desc "Copies all Devise views to your application."
argument :scope, :required => false, :default => nil,
:desc => "The scope to copy views to"
class_option :template_engine, :type => :string, :aliases => "-t", :default => "erb",
:desc => "Template engine for the views. Available options are 'erb' and 'haml'."
def copy_views
case options[:template_engine]
when "haml"
verify_haml_existence
verify_haml_version
create_and_copy_haml_views
else
directory "devise", "app/views/#{scope || :devise}"
end
end
protected
def verify_haml_existence
begin
require 'haml'
rescue LoadError
say "HAML is not installed, or it is not specified in your Gemfile."
exit
end
end
def verify_haml_version
unless Haml.version[:major] == 2 and Haml.version[:minor] >= 3 or Haml.version[:major] >= 3
say "To generate HAML templates, you need to install HAML 2.3 or above."
exit
end
end
def create_and_copy_haml_views
require 'tmpdir'
html_root = "#{self.class.source_root}/devise"
Dir.mktmpdir("devise-haml.") do |haml_root|
Dir["#{html_root}/**/*"].each do |path|
relative_path = path.sub(html_root, "")
source_path = (haml_root + relative_path).sub(/erb$/, "haml")
if File.directory?(path)
FileUtils.mkdir_p(source_path)
else
`html2haml -r #{path} #{source_path}`
end
end
directory haml_root, "app/views/#{scope || :devise}"
end
end
end
end
end

View File

@ -0,0 +1,2 @@
# Remove this file on next rails release
require "generators/devise/devise/devise_generator"

View File

@ -1,29 +0,0 @@
require 'active_support/secure_random'
class DeviseInstallGenerator < Rails::Generators::Base
desc "Creates a Devise initializer and copy locale files to your application."
class_option :orm
def self.source_root
@_devise_source_root ||= File.expand_path("../templates", __FILE__)
end
def copy_initializer
template "devise.rb", "config/initializers/devise.rb"
end
def copy_locale
copy_file "../../../../config/locales/en.yml", "config/locales/devise.en.yml"
end
def show_readme
readme "README"
end
protected
def readme(path)
say File.read(File.expand_path(path, self.class.source_root))
end
end

View File

@ -0,0 +1,2 @@
# Remove this file after deprecation
warn "[WARNING] `rails g devise_install` is deprecated, please use `rails g devise:install` instead."

View File

@ -1,62 +0,0 @@
class DeviseViewsGenerator < Rails::Generators::Base
desc "Copies all Devise views to your application."
argument :scope, :required => false, :default => nil,
:desc => "The scope to copy views to"
class_option :template_engine, :type => :string, :aliases => "-t", :default => "erb",
:desc => "Template engine for the views. Available options are 'erb' and 'haml'."
def self.source_root
@_devise_source_root ||= File.expand_path("../../../../app/views", __FILE__)
end
def copy_views
case options[:template_engine]
when "haml"
verify_haml_existence
verify_haml_version
create_and_copy_haml_views
else
directory "devise", "app/views/#{scope || 'devise'}"
end
end
protected
def verify_haml_existence
begin
require 'haml'
rescue LoadError
say "HAML is not installed, or it is not specified in your Gemfile."
exit
end
end
def verify_haml_version
unless Haml.version[:major] == 2 and Haml.version[:minor] >= 3 or Haml.version[:major] >= 3
say "To generate HAML templates, you need to install HAML 2.3 or above."
exit
end
end
def create_and_copy_haml_views
require 'tmpdir'
html_root = "#{self.class.source_root}/devise"
Dir.mktmpdir("devise-haml.") do |haml_root|
Dir["#{html_root}/**/*"].each do |path|
relative_path = path.sub(html_root, "")
source_path = (haml_root + relative_path).sub(/erb$/, "haml")
if File.directory?(path)
FileUtils.mkdir_p(source_path)
else
`html2haml -r #{path} #{source_path}`
end
end
directory haml_root, "app/views/#{scope || 'devise'}"
end
end
end

View File

@ -0,0 +1,2 @@
# Remove this file after deprecation
warn "[WARNING] `rails g devise_views` is deprecated, please use `rails g devise:views` instead."