mirror of
https://github.com/heartcombo/devise.git
synced 2022-11-09 12:18:31 -05:00
Add flexibility to devise generators by using ORM hooks.
This commit is contained in:
parent
7113dfe93a
commit
6c49b428b3
11 changed files with 85 additions and 90 deletions
26
lib/generators/active_record/devise_generator.rb
Normal file
26
lib/generators/active_record/devise_generator.rb
Normal file
|
@ -0,0 +1,26 @@
|
|||
require 'rails/generators/active_record'
|
||||
require 'generators/devise/orm_helpers'
|
||||
|
||||
module ActiveRecord
|
||||
module Generators
|
||||
class DeviseGenerator < ActiveRecord::Generators::Base
|
||||
include Devise::Generators::OrmHelpers
|
||||
source_root File.expand_path("../templates", __FILE__)
|
||||
|
||||
def generate_model
|
||||
invoke "active_record:model", [name], :migration => false unless model_exists?
|
||||
end
|
||||
|
||||
def copy_devise_migration
|
||||
migration_template "migration.rb", "db/migrate/devise_create_#{table_name}"
|
||||
end
|
||||
|
||||
def inject_devise_content
|
||||
inject_into_class model_path, class_name, model_contents + <<-CONTENT
|
||||
# Setup accessible (or protected) attributes for your model
|
||||
attr_accessible :email, :password, :password_confirmation, :remember_me
|
||||
CONTENT
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,86 +0,0 @@
|
|||
require 'rails/generators/migration'
|
||||
|
||||
module Devise
|
||||
module Generators
|
||||
class DeviseGenerator < Rails::Generators::NamedBase
|
||||
include Rails::Generators::Migration
|
||||
|
||||
source_root File.expand_path("../templates", __FILE__)
|
||||
namespace "devise"
|
||||
|
||||
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
|
||||
return unless behavior == :invoke
|
||||
|
||||
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, :remember_me
|
||||
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
|
17
lib/generators/devise/devise_generator.rb
Normal file
17
lib/generators/devise/devise_generator.rb
Normal file
|
@ -0,0 +1,17 @@
|
|||
module Devise
|
||||
module Generators
|
||||
class DeviseGenerator < Rails::Generators::NamedBase
|
||||
namespace "devise"
|
||||
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."
|
||||
|
||||
hook_for :orm
|
||||
|
||||
def add_devise_routes
|
||||
route "devise_for :#{table_name}"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -13,7 +13,7 @@ module Devise
|
|||
end
|
||||
|
||||
def copy_locale
|
||||
copy_file "../../../../../config/locales/en.yml", "config/locales/devise.en.yml"
|
||||
copy_file "../../../../config/locales/en.yml", "config/locales/devise.en.yml"
|
||||
end
|
||||
|
||||
def show_readme
|
23
lib/generators/devise/orm_helpers.rb
Normal file
23
lib/generators/devise/orm_helpers.rb
Normal file
|
@ -0,0 +1,23 @@
|
|||
module Devise
|
||||
module Generators
|
||||
module OrmHelpers
|
||||
def model_contents
|
||||
<<-CONTENT
|
||||
# Include default devise modules. Others available are:
|
||||
# :token_authenticatable, :confirmable, :lockable and :timeoutable
|
||||
devise :database_authenticatable, :registerable,
|
||||
:recoverable, :rememberable, :trackable, :validatable
|
||||
|
||||
CONTENT
|
||||
end
|
||||
|
||||
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
|
|
@ -1,7 +1,7 @@
|
|||
module Devise
|
||||
module Generators
|
||||
class ViewsGenerator < Rails::Generators::Base
|
||||
source_root File.expand_path("../../../../../app/views", __FILE__)
|
||||
source_root File.expand_path("../../../../app/views", __FILE__)
|
||||
desc "Copies all Devise views to your application."
|
||||
|
||||
argument :scope, :required => false, :default => nil,
|
|
@ -1,2 +0,0 @@
|
|||
# Remove this file on next rails release
|
||||
require "generators/devise/devise/devise_generator"
|
17
lib/generators/mongoid/devise_generator.rb
Normal file
17
lib/generators/mongoid/devise_generator.rb
Normal file
|
@ -0,0 +1,17 @@
|
|||
require 'generators/devise/orm_helpers'
|
||||
|
||||
module Mongoid
|
||||
module Generators
|
||||
class DeviseGenerator < Rails::Generators::NamedBase
|
||||
include Devise::Generators::OrmHelpers
|
||||
|
||||
def generate_model
|
||||
invoke "mongoid:model", [name] unless model_exists?
|
||||
end
|
||||
|
||||
def inject_devise_content
|
||||
inject_into_file model_path, model_contents, :after => "include Mongoid::Document\n"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue