2010-02-17 04:53:11 -05:00
|
|
|
require 'rails/generators/migration'
|
|
|
|
|
|
|
|
class DeviseGenerator < Rails::Generators::NamedBase
|
|
|
|
include Rails::Generators::Migration
|
|
|
|
|
2010-02-17 05:10:24 -05:00
|
|
|
desc "Generates a model with the given NAME (if one does not exist) with devise " <<
|
|
|
|
"configuration plus a migration file and devise routes."
|
|
|
|
|
2010-02-17 04:53:11 -05:00
|
|
|
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
|
|
|
|
|
2010-03-26 05:35:15 -04:00
|
|
|
class_option :orm
|
2010-02-17 04:53:11 -05:00
|
|
|
class_option :migration, :type => :boolean, :default => orm_has_migration?
|
|
|
|
|
|
|
|
def invoke_orm_model
|
2010-03-26 05:35:15 -04:00
|
|
|
if model_exists?
|
2010-02-17 04:53:11 -05:00
|
|
|
say "* Model already exists. Adding Devise behavior."
|
|
|
|
else
|
2010-03-26 05:35:15 -04:00
|
|
|
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
|
2010-02-17 04:53:11 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def inject_devise_config_into_model
|
|
|
|
inject_into_class model_path, class_name, <<-CONTENT
|
|
|
|
# Include default devise modules. Others available are:
|
2010-03-29 10:13:19 -04:00
|
|
|
# :token_authenticatable, :lockable, :timeoutable and :activatable
|
|
|
|
devise :database_authenticatable, :registerable, :confirmable,
|
|
|
|
:recoverable, :rememberable, :trackable, :validatable
|
2010-02-17 04:53:11 -05:00
|
|
|
|
|
|
|
# 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
|
|
|
|
|
2010-03-26 05:35:15 -04:00
|
|
|
def model_exists?
|
|
|
|
File.exists?(File.join(destination_root, model_path))
|
|
|
|
end
|
|
|
|
|
2010-02-17 04:53:11 -05:00
|
|
|
def model_path
|
|
|
|
@model_path ||= File.join("app", "models", "#{file_path}.rb")
|
|
|
|
end
|
|
|
|
end
|