Skip to content

Commit

Permalink
Fixed the Rails 2 Devise based Rich-CMS admin generator (woot!)
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Engel committed Feb 5, 2011
1 parent e405d1b commit 5020b0f
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 9 deletions.
32 changes: 32 additions & 0 deletions rails_generators/rich_cms_admin/lib/devise/route_devise.rb
@@ -0,0 +1,32 @@
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
21 changes: 12 additions & 9 deletions rails_generators/rich_cms_admin/rich_cms_admin_generator.rb
@@ -1,21 +1,21 @@
require File.expand_path("../lib/devise/route_devise.rb", __FILE__)

class RichCmsAdminGenerator < Rails::Generator::Base
def initialize(runtime_args, runtime_options = {})
super
@name = @args.first || "user"
end

def manifest
unless defined?(Devise) || options[:logic].to_s.underscore != "devise"
puts <<-WARNING
Please install the Devise 1.0.9 gem first. Aborting...
unless defined?(Devise) || options[:logic].to_s.underscore != "devise"
puts <<-WARNING.gsub(/^ {9}/, "")
Don't forget to install Devise 1.0.9!
WARNING
return
end
unless defined?(Authlogic) || options[:logic].to_s.underscore != "authlogic"
puts <<-WARNING
puts <<-WARNING.gsub(/^ {9}/, "")
Don't forget to install Authlogic 2.1.6!
WARNING
return
end
record do |m|
send :"generate_#{options[:logic].underscore}_assets", m if options[:logic]
Expand Down Expand Up @@ -71,8 +71,8 @@ def add_options!(opt)
end

def banner
<<-BANNER
Creates Devisie / Authlogic model and configures your Rails application for Rich-CMS authentication.
<<-BANNER.gsub(/^ {7}/, "")
Creates Devise / Authlogic model and configures your Rails application for Rich-CMS authentication.
USAGE: #{$0} #{spec.name} [model_name]
BANNER
Expand All @@ -81,7 +81,10 @@ def banner
private

def generate_devise_assets(m)
system "script/generate devise #{model_class_name}"
m.directory "app/models"
m.template "devise/model.rb" , "app/models/#{model_file_name}.rb"
m.migration_template "devise/migration.rb", "db/migrate", :migration_file_name => "devise_#{migration_file_name}"
m.route_devise table_name
end

def generate_authlogic_assets(m)
Expand Down
23 changes: 23 additions & 0 deletions rails_generators/rich_cms_admin/templates/devise/migration.rb
@@ -0,0 +1,23 @@
class Devise<%= migration_class_name %> < ActiveRecord::Migration
def self.up
create_table(:<%= table_name %>) do |t|
t.database_authenticatable :null => false
t.confirmable
t.recoverable
t.rememberable
t.trackable
# t.lockable

t.timestamps
end

add_index :<%= table_name %>, :email, :unique => true
add_index :<%= table_name %>, :confirmation_token, :unique => true
add_index :<%= table_name %>, :reset_password_token, :unique => true
# add_index :<%= table_name %>, :unlock_token, :unique => true
end

def self.down
drop_table :<%= table_name %>
end
end
9 changes: 9 additions & 0 deletions rails_generators/rich_cms_admin/templates/devise/model.rb
@@ -0,0 +1,9 @@
class <%= model_class_name %> < ActiveRecord::Base
# Include default devise modules. Others available are:
# :http_authenticatable, :token_authenticatable, :confirmable, :lockable, :timeoutable and :activatable
devise :registerable, :database_authenticatable, :recoverable,
:rememberable, :trackable, :validatable

# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation
end

0 comments on commit 5020b0f

Please sign in to comment.