Skip to content

Commit

Permalink
Added a --skip-routes flag for repeated applications of the generator…
Browse files Browse the repository at this point in the history
…. Took out mailer lines from test helper that don't ge there
  • Loading branch information
Philip (flip) Kromer committed May 17, 2008
1 parent 707fa38 commit 6221678
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 18 deletions.
19 changes: 14 additions & 5 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ To use:
./script/generate authenticated user sessions \
--include-activation \
--stateful \
--rspec \
--skip-migration
--rspec \
--skip-migration \
--skip-routes

The first parameter specifies the model that gets created in signup
(typically a user or account model). A model with migration is
Expand All @@ -29,13 +30,12 @@ This was taken from:

http://www.vaporbase.com/postings/stateful_authentication

Passing --skip-migration will skip the user migration, useful if
you've already run this generator.
Passing --skip-migration will skip the user migration, and --skip-routes will
skip resource generation -- both useful if you've already run this generator.

Pass --rspec to generate a (more thorough) set of RSpec tests. This
requires the RSpec and Rspec-on-rails plugins: http://rspec.info/


***************************************************************************
Post-install

Expand Down Expand Up @@ -75,3 +75,12 @@ combat session fixation attacks. However, this resets the Form
Authentication token used by Request Forgery Protection. I've left
it out now, since Rails 1.2.6 and Rails 2.0 will both stop session
fixation attacks anyway.

***************************************************************************
Stories

There are now RSpec stories that allow expressive enjoyable tests for the
authentication code.

The flexible code for resource testing came out of code from Ben Mabey
http://www.benmabey.com/2008/02/04/rspec-plain-text-stories-webrat-chunky-bacon/
12 changes: 9 additions & 3 deletions generators/authenticated/authenticated_generator.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'restful_authentication/rails_commands'
class AuthenticatedGenerator < Rails::Generator::NamedBase
default_options :skip_migration => false,
:skip_routes => false,
:include_activation => false

attr_reader :controller_name,
Expand Down Expand Up @@ -235,8 +236,11 @@ def manifest
}, :migration_file_name => "create_#{file_path.gsub(/\//, '_').pluralize}"
end

m.route_resource controller_singular_name
m.route_resources model_controller_plural_name
unless options[:skip_routes]
m.route_resource controller_singular_name
m.route_resources model_controller_plural_name
end

end

action = nil
Expand Down Expand Up @@ -305,12 +309,14 @@ def add_options!(opt)
opt.separator ''
opt.separator 'Options:'
opt.on("--skip-migration",
"Don't generate a migration file for this model") { |v| options[:skip_migration] = v }
"Don't generate a migration file for this model") { |v| options[:skip_migration] = v }
opt.on("--include-activation",
"Generate signup 'activation code' confirmation via email") { |v| options[:include_activation] = true }
opt.on("--stateful",
"Use acts_as_state_machine. Assumes --include-activation") { |v| options[:include_activation] = options[:stateful] = true }
opt.on("--rspec",
"Force rspec mode (checks for RAILS_ROOT/spec by default)") { |v| options[:rspec] = true }
opt.on("--skip-routes",
"Don't generate a resource line in config/routes.rb") { |v| options[:skip_routes] = v }
end
end
11 changes: 1 addition & 10 deletions generators/authenticated/templates/authenticated_test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,13 @@ def authorize_as(user)
@request.env["HTTP_AUTHORIZATION"] = user ? ActionController::HttpAuthentication::Basic.encode_credentials(users(user).login, 'test') : nil
end
<% if options[:include_activation] -%>
# For tests that include a mailer
def set_mailer_in_test
ActionMailer::Base.delivery_method = :test
ActionMailer::Base.perform_deliveries = true
ActionMailer::Base.deliveries = []
end
<% end -%>
<% if options[:rspec] -%>
# rspec
def mock_<%= file_name %>
<%= file_name %> = mock_model(<%= class_name %>, :id => 1,
:login => 'user_name',
:name => 'U. Surname',
:to_xml => "XML", :to_json => "JSON",
:to_xml => "<%= class_name %>-in-XML", :to_json => "<%= class_name %>-in-JSON",
:errors => [])
<%= file_name %>
end
Expand Down

0 comments on commit 6221678

Please sign in to comment.