Permalink
Browse files

update for rails 2.0: generate routes, use current template names [Pa…

…ul Mucur]
  • Loading branch information...
1 parent 1a50c04 commit 0e31b9f417d1f48e45701a5d4cb0f80a90ef7b16 @technoweenie committed Jan 1, 2008
View
14 README
@@ -2,8 +2,7 @@ Restful Authentication Generator
====
This is a basic restful authentication generator for rails, taken
-from acts as authenticated. Currently it requires Rails 1.2 (or
-edge).
+from acts as authenticated. Currently it requires Rails 1.2.6 or above.
To use:
@@ -29,23 +28,12 @@ http://www.vaporbase.com/postings/stateful_authentication
You can pass --skip-migration to skip the user migration.
-From here, you will need to add the resource routes in
-config/routes.rb.
-
- map.resources :users
- map.resource :session
-
If you're using acts_as_state_machine, define your users resource like this:
map.resources :users, :member => { :suspend => :put,
:unsuspend => :put,
:purge => :delete }
-If you're on rails 1.2.3 you may need to specify the controller name
-for the session singular resource:
-
- map.resource :session, :controller => 'sessions'
-
Also, add an observer to config/environment.rb if you chose the
--include-activation option
@@ -1,4 +1,8 @@
+require 'restful_authentication/rails_commands'
class AuthenticatedGenerator < Rails::Generator::NamedBase
+ default_options :skip_migration => false,
+ :include_activation => false
+
attr_reader :controller_name,
:controller_class_path,
:controller_file_path,
@@ -160,14 +164,14 @@ def manifest
# Controller templates
- m.template 'login.rhtml', File.join('app/views', controller_class_path, controller_file_name, "new.rhtml")
- m.template 'signup.rhtml', File.join('app/views', model_controller_class_path, model_controller_file_name, "new.rhtml")
+ m.template 'login.html.erb', File.join('app/views', controller_class_path, controller_file_name, "new.html.erb")
+ m.template 'signup.html.erb', File.join('app/views', model_controller_class_path, model_controller_file_name, "new.html.erb")
if options[:include_activation]
# Mailer templates
%w( activation signup_notification ).each do |action|
- m.template "#{action}.rhtml",
- File.join('app/views', "#{file_name}_mailer", "#{action}.rhtml")
+ m.template "#{action}.html.erb",
+ File.join('app/views', "#{file_name}_mailer", "#{action}.html.erb")
end
end
@@ -176,6 +180,9 @@ def manifest
:migration_name => "Create#{class_name.pluralize.gsub(/::/, '')}"
}, :migration_file_name => "create_#{file_path.gsub(/\//, '_').pluralize}"
end
+
+ m.route_resource controller_singular_name
+ m.route_resources model_controller_plural_name
end
action = nil
@@ -186,13 +193,6 @@ def manifest
puts ("-" * 70)
puts "Don't forget to:"
puts
- puts " - add restful routes in config/routes.rb"
- puts " map.resources :#{model_controller_file_name}"
- puts " map.resource :#{controller_singular_name.singularize}"
- puts
- puts " Rails 1.2.3 may need a :controller option for the singular resource:"
- puts " - map.resource :#{controller_singular_name.singularize}, :controller => '#{controller_file_name}'"
- puts
if options[:include_activation]
puts " map.activate '/activate/:activation_code', :controller => '#{model_controller_file_name}', :action => 'activate'"
puts
@@ -1,4 +1,4 @@
-<%% form_tag <%= controller_singular_name.singularize %>_path do -%>
+<%% form_tag <%= controller_singular_name %>_path do -%>
<p><label for="login">Login</label><br/>
<%%= text_field_tag 'login' %></p>
@@ -0,0 +1,29 @@
+Rails::Generator::Commands::Create.class_eval do
+ def route_resource(*resources)
+ resource_list = resources.map { |r| r.to_sym.inspect }.join(', ')
+ sentinel = 'ActionController::Routing::Routes.draw do |map|'
+
+ logger.route "map.resource #{resource_list}"
+ unless options[:pretend]
+ gsub_file 'config/routes.rb', /(#{Regexp.escape(sentinel)})/mi do |match|
+ "#{match}\n map.resource #{resource_list}\n"
+ end
+ end
+ end
+end
+
+Rails::Generator::Commands::Destroy.class_eval do
+ def route_resource(*resources)
+ resource_list = resources.map { |r| r.to_sym.inspect }.join(', ')
+ look_for = "\n map.resource #{resource_list}\n"
+ logger.route "map.resource #{resource_list}"
+ gsub_file 'config/routes.rb', /(#{look_for})/mi, ''
+ end
+end
+
+Rails::Generator::Commands::List.class_eval do
+ def route_resource(*resources)
+ resource_list = resources.map { |r| r.to_sym.inspect }.join(', ')
+ logger.route "map.resource #{resource_list}"
+ end
+end

0 comments on commit 0e31b9f

Please sign in to comment.