Skip to content

Commit

Permalink
Update generators to use Rails 3 syntax, i.e devise:install instead o…
Browse files Browse the repository at this point in the history
…f devise_install.
  • Loading branch information
josevalim committed Jun 13, 2010
1 parent 31910b8 commit 1f4a31f
Show file tree
Hide file tree
Showing 14 changed files with 181 additions and 175 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.rdoc
Expand Up @@ -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

Expand Down
6 changes: 3 additions & 3 deletions README.rdoc
Expand Up @@ -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:

Expand Down Expand Up @@ -176,15 +176,15 @@ 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

We built Devise to help you quickly develop an application that uses authentication. However, we don't want to be in your way when you need to customize it.

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".

Expand Down
83 changes: 83 additions & 0 deletions lib/generators/devise/devise/devise_generator.rb
@@ -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
81 changes: 0 additions & 81 deletions lib/generators/devise/devise_generator.rb

This file was deleted.

24 changes: 24 additions & 0 deletions lib/generators/devise/install/install_generator.rb
@@ -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
63 changes: 63 additions & 0 deletions lib/generators/devise/views/views_generator.rb
@@ -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
2 changes: 2 additions & 0 deletions lib/generators/devise_generator.rb
@@ -0,0 +1,2 @@
# Remove this file on next rails release
require "generators/devise/devise/devise_generator"
29 changes: 0 additions & 29 deletions lib/generators/devise_install/devise_install_generator.rb

This file was deleted.

2 changes: 2 additions & 0 deletions lib/generators/devise_install_generator.rb
@@ -0,0 +1,2 @@
# Remove this file after deprecation
warn "[WARNING] `rails g devise_install` is deprecated, please use `rails g devise:install` instead."

0 comments on commit 1f4a31f

Please sign in to comment.