Skip to content

Commit

Permalink
added registrations_for option to devise:users generator
Browse files Browse the repository at this point in the history
  • Loading branch information
kristianmandrup committed Feb 13, 2011
1 parent e689975 commit a4dae2e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
4 changes: 4 additions & 0 deletions README.textile
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ Cream comes with a suite of specialized generators that should let you configure

Note: Lately Cream also has options for some common customizations such as allowing login using either username or email.

h2. Status update (Feb 13)

Just added a @registrations_for@ option to the @devise:users@ generator. This allows you to specify for which user types to create individual registration controllers and pages (if needed). I need to fix the current routes_helper to work with user_types instead of roles. Stay tuned. (all edge)

h2. Objectives

* Integrate a set of widely accepted sub-systems for a fully integrated solution
Expand Down
4 changes: 2 additions & 2 deletions lib/generators/devise/users/routes_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ def roles_routes
logger.debug "performing devise role routing for: #{roles_to_route}"
roles_to_route.map do |role|
%Q{
devise_for :#{role.pluralize}, :class_name => '#{role.classify}'
as :#{role} do
devise_for :#{role.pluralize}, :class_name => '#{role.classify}', :controllers => {:registrations => 'devise/registrations', :sessions => 'main'} do
match "/#{role.pluralize}/sign_in" => "devise/signin#new", :as => :#{role}_signin
match "/#{role.pluralize}/sign_up" => "devise/registrations#new", :as => :#{role}_signup
end
}
Expand Down
25 changes: 25 additions & 0 deletions lib/generators/devise/users/users_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class UsersGenerator < Rails::Generators::Base

class_option :user_class, :type => :string, :default => 'User', :desc => "User class"

class_option :registrations_for, :type => :array, :default => [], :desc => "User types individual registrations"

# ORM to use
class_option :orm, :type => :string, :default => 'active_record', :desc => "ORM to use"
class_option :roles, :type => :array, :default => [], :desc => "Roles"
Expand Down Expand Up @@ -48,6 +50,17 @@ def configure_devise_users
end

routes_configure!

registrations.each do |reg|
if user_type? reg
# create controller
controller = "#{reg}::Registrations".camelcase
rgen "controller #{controller} new"
rgen "view #{controller} new"

# make controller a devise controller
replace_controller_inheritance controller.underscore, 'Devise::RegistrationsController'
end
end

protected
Expand All @@ -62,6 +75,18 @@ def configure_devise_users

use_helpers :model, :app, :special, :file

def remove_controller_inheritance name
File.remove_content_from controller_file_name(name.as_filename), :where => /<\s*ApplicationController/
end

def replace_controller_inheritance name, replace_controller
File.replace_content_from controller_file_name(name.as_filename), :where => /<\s*ApplicationController/, :with => replace_controller
end

def registrations
options[:registrations_for]
end

# creates a new user model of a given name without devise strategies, instead inheriting from the base User
# Never create a Guest user model as this should always be a "fake" (suggestions!?)
def create_user name
Expand Down

0 comments on commit a4dae2e

Please sign in to comment.