Skip to content

Commit

Permalink
update for rails 2.0: generate routes, use current template names [Pa…
Browse files Browse the repository at this point in the history
…ul Mucur]
  • Loading branch information
technoweenie committed Jan 4, 2008
1 parent 1a50c04 commit 0e31b9f
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 25 deletions.
14 changes: 1 addition & 13 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -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

Expand Down
22 changes: 11 additions & 11 deletions generators/authenticated/authenticated_generator.rb
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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>

Expand Down
29 changes: 29 additions & 0 deletions lib/restful_authentication/rails_commands.rb
Original file line number Diff line number Diff line change
@@ -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.