Permalink
Browse files

updated routes to reflect user_types and better handle customization

  • Loading branch information...
1 parent a4dae2e commit 3a584f5c85a3531a3d883f1db4fadc9fb82f1341 @kristianmandrup committed Feb 13, 2011
Showing with 42 additions and 66 deletions.
  1. +1 −1 README.textile
  2. +41 −41 lib/generators/devise/users/routes_helper.rb
  3. +0 −24 lib/generators/devise/users/users_generator.rb
View
@@ -26,7 +26,7 @@ Note: Lately Cream also has options for some common customizations such as allow
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)
+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). The routes_helper has been updated in order to be aligned with this approach. Please try out edge and let me know how well it works.
h2. Objectives
@@ -7,64 +7,64 @@ def model_routes
end
def routes_configure!
- routes_content = routes_for_roles
+ routes_content = default_signin_routes << user_type_routes
puts "Insert into routes: #{routes_content}"
insert_into_routes do
routes_content
end
end
-
- def routes_for_roles
- say customize_note, :green
- routes_customize_comment << roles_routes
+ def user_types_to_route
+ @user_types_to_route ||= user_types_except(:guest).delete_if do |user_type|
+ name_user_types = user_type.pluralize
+ is_there = !(read_routes_file =~ /devise_for :#{name_user_types}, :class_name =>/).nil?
+ logger.debug "not doing devise routing for #{name_user_types} as it is already there" if is_there
+ is_there
+ end
end
- def roles_except *names
- roles.reject {|r| names.include? r.to_sym }
+ def default_signin_routes
+ name_default_users = user_class.underscore.gsub('/', '_').pluralize
+ default_user_class = user_class.classify
+ %Q{
+ match '/sign_in' => 'main#index', :as => :new_user_session
+
+ devise_for :#{name_default_users}, :class_name => '#{default_user_class}', :controllers => {:sessions => 'main'} do
+ get '/sign_in' => 'main#index', :as => :new_user_session
+ end}
end
- def roles_routes
- roles_to_route = roles_except(:guest).delete_if do |role|
- is_there = !(read_routes_file =~ /devise_for :#{role.pluralize}, :class_name =>/).nil?
- logger.debug "not doing devise routing for #{role.pluralize} as it is already there" if is_there
- is_there
- end
-
- roles_to_route.each do |role|
- if read_routes_file =~ /devise_for :#{role.pluralize}/
- logger.debug "removing old devise routing for: #{role}"
- File.remove_content_from routes_file, :where => /devise_for :#{role.pluralize}/
+ def user_type_routes
+ user_types_to_route.each do |user_type|
+ name_user_types = user_type.pluralize
+ if read_routes_file =~ /devise_for :#{name_user_types}/
+ logger.debug "removing old devise routing for: #{name_user_types}"
+ File.remove_content_from routes_file, :where => /devise_for :#{name_user_types}/
end
end
- 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}', :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
+ logger.debug "performing devise role routing for: #{user_types_to_route}"
+
+ user_types_to_route.map do |user_type|
+ name_user_types = user_type.pluralize
+ name_user_type = user_type.underscore.gsub('/', '_')
+ %Q{
+ devise_for :#{name_user_types}, :class_name => '#{user_type.classify}', :controllers => {:registrations => 'devise/registrations', :sessions => 'main'} do
+ match "/#{name_user_types}/sign_in" => "devise/sessions#new", :as => :#{name_user_type}_signin
+ match "/#{name_user_types}/sign_up" => "devise/registrations#new", :as => :#{name_user_type}_signup
end
}
end.join("\n")
- end
+ end
-
- def routes_customize_comment
- return "" if read_routes_file =~ /customize controller action/
- %q{
-# customize controller action if needed to render individual registration form for each role
-# Example:
-# match "/editors/sign_up" => "user_registrations#new_editor", :as => :editor_signup
-
-# 'user_registrations_controller.rb' with the action #new_editor
-# def new_editor
-# build_resource({})
-# end
-#
-# and the registration form in 'views/user_registrations/new_editor.html.erb'
-#
-}
+ protected
+
+ def roles_except *names
+ roles.reject {|r| names.include? r.to_sym }
end
+
+ def user_types_except *names
+ user_types.reject {|r| user_types.include? r.to_sym }
+ end
end
end
@@ -127,30 +127,6 @@ def devise_gems
def devise_base_user
create_devise_model user_class
end
-
- private
-
- def customize_note
- %q{
-Customize controller action if needed to render an individual registration form for each devise User type
-
-Example:
-
-- routes.rb:
-
- match "/editors/sign_up" => "user_registrations#new_editor", :as => :editor_signup
-
-- user_registrations_controller.rb:
-
- def new_editor
- build_resource({})
- end
-
-Then create the registration form (view) for the :editor user in 'views/user_registrations/new_editor.html.erb'
-See comments in routes.rb for more info. There is also a page on the wiki about advanced routes customization.
-}
- end
-
end
end
end

0 comments on commit 3a584f5

Please sign in to comment.