nifty-generators 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (29) hide show
  1. data/CHANGELOG +4 -0
  2. data/Manifest +25 -0
  3. data/Rakefile +1 -1
  4. data/nifty-generators.gemspec +28 -3
  5. data/rails_generators/nifty_authentication/USAGE +46 -0
  6. data/rails_generators/nifty_authentication/lib/insert_commands.rb +74 -0
  7. data/rails_generators/nifty_authentication/nifty_authentication_generator.rb +114 -0
  8. data/rails_generators/nifty_authentication/templates/authentication.rb +36 -0
  9. data/rails_generators/nifty_authentication/templates/fixtures.yml +12 -0
  10. data/rails_generators/nifty_authentication/templates/migration.rb +15 -0
  11. data/rails_generators/nifty_authentication/templates/sessions_controller.rb +22 -0
  12. data/rails_generators/nifty_authentication/templates/sessions_helper.rb +2 -0
  13. data/rails_generators/nifty_authentication/templates/tests/rspec/sessions_controller.rb +25 -0
  14. data/rails_generators/nifty_authentication/templates/tests/rspec/user.rb +62 -0
  15. data/rails_generators/nifty_authentication/templates/tests/rspec/users_controller.rb +24 -0
  16. data/rails_generators/nifty_authentication/templates/tests/shoulda/sessions_controller.rb +26 -0
  17. data/rails_generators/nifty_authentication/templates/tests/shoulda/user.rb +64 -0
  18. data/rails_generators/nifty_authentication/templates/tests/shoulda/users_controller.rb +25 -0
  19. data/rails_generators/nifty_authentication/templates/tests/testunit/sessions_controller.rb +22 -0
  20. data/rails_generators/nifty_authentication/templates/tests/testunit/user.rb +64 -0
  21. data/rails_generators/nifty_authentication/templates/tests/testunit/users_controller.rb +21 -0
  22. data/rails_generators/nifty_authentication/templates/user.rb +33 -0
  23. data/rails_generators/nifty_authentication/templates/users_controller.rb +16 -0
  24. data/rails_generators/nifty_authentication/templates/users_helper.rb +2 -0
  25. data/rails_generators/nifty_authentication/templates/views/erb/login.html.erb +15 -0
  26. data/rails_generators/nifty_authentication/templates/views/erb/signup.html.erb +24 -0
  27. data/rails_generators/nifty_authentication/templates/views/haml/login.html.haml +15 -0
  28. data/rails_generators/nifty_authentication/templates/views/haml/signup.html.haml +24 -0
  29. metadata +27 -2
data/CHANGELOG CHANGED
@@ -1,3 +1,7 @@
1
+ 0.2.1 (November 10th, 2008)
2
+
3
+ * adding missing nifty_authentication files
4
+
1
5
  0.2.0 (November 4th, 2008)
2
6
 
3
7
  * adding nifty_authentication
data/Manifest CHANGED
@@ -2,6 +2,30 @@ CHANGELOG
2
2
  lib/nifty_generators.rb
3
3
  LICENSE
4
4
  Manifest
5
+ rails_generators/nifty_authentication/lib/insert_commands.rb
6
+ rails_generators/nifty_authentication/nifty_authentication_generator.rb
7
+ rails_generators/nifty_authentication/templates/authentication.rb
8
+ rails_generators/nifty_authentication/templates/fixtures.yml
9
+ rails_generators/nifty_authentication/templates/migration.rb
10
+ rails_generators/nifty_authentication/templates/sessions_controller.rb
11
+ rails_generators/nifty_authentication/templates/sessions_helper.rb
12
+ rails_generators/nifty_authentication/templates/tests/rspec/sessions_controller.rb
13
+ rails_generators/nifty_authentication/templates/tests/rspec/user.rb
14
+ rails_generators/nifty_authentication/templates/tests/rspec/users_controller.rb
15
+ rails_generators/nifty_authentication/templates/tests/shoulda/sessions_controller.rb
16
+ rails_generators/nifty_authentication/templates/tests/shoulda/user.rb
17
+ rails_generators/nifty_authentication/templates/tests/shoulda/users_controller.rb
18
+ rails_generators/nifty_authentication/templates/tests/testunit/sessions_controller.rb
19
+ rails_generators/nifty_authentication/templates/tests/testunit/user.rb
20
+ rails_generators/nifty_authentication/templates/tests/testunit/users_controller.rb
21
+ rails_generators/nifty_authentication/templates/user.rb
22
+ rails_generators/nifty_authentication/templates/users_controller.rb
23
+ rails_generators/nifty_authentication/templates/users_helper.rb
24
+ rails_generators/nifty_authentication/templates/views/erb/login.html.erb
25
+ rails_generators/nifty_authentication/templates/views/erb/signup.html.erb
26
+ rails_generators/nifty_authentication/templates/views/haml/login.html.haml
27
+ rails_generators/nifty_authentication/templates/views/haml/signup.html.haml
28
+ rails_generators/nifty_authentication/USAGE
5
29
  rails_generators/nifty_config/nifty_config_generator.rb
6
30
  rails_generators/nifty_config/templates/config.yml
7
31
  rails_generators/nifty_config/templates/load_config.rb
@@ -68,6 +92,7 @@ Rakefile
68
92
  README.rdoc
69
93
  tasks/deployment.rake
70
94
  test/test_helper.rb
95
+ test/test_nifty_authentication_generator.rb
71
96
  test/test_nifty_config_generator.rb
72
97
  test/test_nifty_layout_generator.rb
73
98
  test/test_nifty_scaffold_generator.rb
data/Rakefile CHANGED
@@ -2,7 +2,7 @@ require 'rubygems'
2
2
  require 'rake'
3
3
  require 'echoe'
4
4
 
5
- Echoe.new('nifty-generators', '0.2.0') do |p|
5
+ Echoe.new('nifty-generators', '0.2.1') do |p|
6
6
  p.project = "niftygenerators"
7
7
  p.summary = "A collection of useful generator scripts for Rails."
8
8
  p.description = "A collection of useful generator scripts for Rails."
@@ -1,18 +1,18 @@
1
1
 
2
- # Gem::Specification for Nifty-generators-0.2.0
2
+ # Gem::Specification for Nifty-generators-0.2.1
3
3
  # Originally generated by Echoe
4
4
 
5
5
  --- !ruby/object:Gem::Specification
6
6
  name: nifty-generators
7
7
  version: !ruby/object:Gem::Version
8
- version: 0.2.0
8
+ version: 0.2.1
9
9
  platform: ruby
10
10
  authors:
11
11
  - Ryan Bates
12
12
  autorequire:
13
13
  bindir: bin
14
14
 
15
- date: 2008-11-04 00:00:00 -08:00
15
+ date: 2008-11-10 00:00:00 -08:00
16
16
  default_executable:
17
17
  dependencies: []
18
18
 
@@ -34,6 +34,30 @@ files:
34
34
  - lib/nifty_generators.rb
35
35
  - LICENSE
36
36
  - Manifest
37
+ - rails_generators/nifty_authentication/lib/insert_commands.rb
38
+ - rails_generators/nifty_authentication/nifty_authentication_generator.rb
39
+ - rails_generators/nifty_authentication/templates/authentication.rb
40
+ - rails_generators/nifty_authentication/templates/fixtures.yml
41
+ - rails_generators/nifty_authentication/templates/migration.rb
42
+ - rails_generators/nifty_authentication/templates/sessions_controller.rb
43
+ - rails_generators/nifty_authentication/templates/sessions_helper.rb
44
+ - rails_generators/nifty_authentication/templates/tests/rspec/sessions_controller.rb
45
+ - rails_generators/nifty_authentication/templates/tests/rspec/user.rb
46
+ - rails_generators/nifty_authentication/templates/tests/rspec/users_controller.rb
47
+ - rails_generators/nifty_authentication/templates/tests/shoulda/sessions_controller.rb
48
+ - rails_generators/nifty_authentication/templates/tests/shoulda/user.rb
49
+ - rails_generators/nifty_authentication/templates/tests/shoulda/users_controller.rb
50
+ - rails_generators/nifty_authentication/templates/tests/testunit/sessions_controller.rb
51
+ - rails_generators/nifty_authentication/templates/tests/testunit/user.rb
52
+ - rails_generators/nifty_authentication/templates/tests/testunit/users_controller.rb
53
+ - rails_generators/nifty_authentication/templates/user.rb
54
+ - rails_generators/nifty_authentication/templates/users_controller.rb
55
+ - rails_generators/nifty_authentication/templates/users_helper.rb
56
+ - rails_generators/nifty_authentication/templates/views/erb/login.html.erb
57
+ - rails_generators/nifty_authentication/templates/views/erb/signup.html.erb
58
+ - rails_generators/nifty_authentication/templates/views/haml/login.html.haml
59
+ - rails_generators/nifty_authentication/templates/views/haml/signup.html.haml
60
+ - rails_generators/nifty_authentication/USAGE
37
61
  - rails_generators/nifty_config/nifty_config_generator.rb
38
62
  - rails_generators/nifty_config/templates/config.yml
39
63
  - rails_generators/nifty_config/templates/load_config.rb
@@ -100,6 +124,7 @@ files:
100
124
  - README.rdoc
101
125
  - tasks/deployment.rake
102
126
  - test/test_helper.rb
127
+ - test/test_nifty_authentication_generator.rb
103
128
  - test/test_nifty_config_generator.rb
104
129
  - test/test_nifty_layout_generator.rb
105
130
  - test/test_nifty_scaffold_generator.rb
@@ -0,0 +1,46 @@
1
+ Description:
2
+ Generates a user model, users controller, and sessions controller. The
3
+ users controller handles the registration and the sessions controller
4
+ handles authentication. This is similar to restful_authentication, but
5
+ simpler.
6
+
7
+ IMPORTANT: This generator uses the "title" helper method which is generated
8
+ by the nifty_layout generator. You may want to run that generator first.
9
+
10
+ Usage:
11
+ If you do not pass any arguments, the model name will default to "user", and
12
+ the authentication controller will default to "sessions". You can override
13
+ each of these respectively by passing one or two arguments. Either name can
14
+ be CamelCased or under_scored.
15
+
16
+ Examples:
17
+ script/generate nifty_authentication
18
+
19
+ Creates user model, users_controller, and sessions_controller.
20
+
21
+ script/generate nifty_authentication account
22
+
23
+ Creates account model, accounts_controller, and sessions_controller.
24
+
25
+ script/generate nifty_authentication Account CurrentSession
26
+
27
+ Creates account model, accounts_controller, and current_sessions_controller.
28
+
29
+ Methods:
30
+ There are several methods generated which you can use in your application.
31
+ Here's a common example of what you might add to your layout.
32
+
33
+ <% if logged_in? %>
34
+ Welcome <%= current_user.username %>! Not you?
35
+ <%= link_to "Log out", logout_path %>
36
+ <% else %>
37
+ <%= link_to "Sign up", signup_path %> or
38
+ <%= link_to "log in", login_path %>.
39
+ <% end %>
40
+
41
+ You can also restrict unregistered users from accessing a controller using
42
+ a before filter. For example.
43
+
44
+ before_filter :login_required, :except => [:index, :show]
45
+
46
+ See the generated file lib/authentication.rb for details.
@@ -0,0 +1,74 @@
1
+ Rails::Generator::Commands::Create.class_eval do
2
+ def route_resource(*resources)
3
+ resource_list = resources.map { |r| r.to_sym.inspect }.join(', ')
4
+ sentinel = 'ActionController::Routing::Routes.draw do |map|'
5
+
6
+ logger.route "map.resource #{resource_list}"
7
+ unless options[:pretend]
8
+ gsub_file 'config/routes.rb', /(#{Regexp.escape(sentinel)})/mi do |match|
9
+ "#{match}\n map.resource #{resource_list}\n"
10
+ end
11
+ end
12
+ end
13
+
14
+ def route_name(name, path, route_options = {})
15
+ sentinel = 'ActionController::Routing::Routes.draw do |map|'
16
+
17
+ logger.route "map.#{name} '#{path}', :controller => '#{route_options[:controller]}', :action => '#{route_options[:action]}'"
18
+ unless options[:pretend]
19
+ gsub_file 'config/routes.rb', /(#{Regexp.escape(sentinel)})/mi do |match|
20
+ "#{match}\n map.#{name} '#{path}', :controller => '#{route_options[:controller]}', :action => '#{route_options[:action]}'"
21
+ end
22
+ end
23
+ end
24
+
25
+ def insert_into(file, line)
26
+ logger.insert "#{line} into #{file}"
27
+ unless options[:pretend]
28
+ gsub_file file, /^(class|module) .+$/ do |match|
29
+ "#{match}\n #{line}"
30
+ end
31
+ end
32
+ end
33
+ end
34
+
35
+ Rails::Generator::Commands::Destroy.class_eval do
36
+ def route_resource(*resources)
37
+ resource_list = resources.map { |r| r.to_sym.inspect }.join(', ')
38
+ look_for = "\n map.resource #{resource_list}\n"
39
+ logger.route "map.resource #{resource_list}"
40
+ unless options[:pretend]
41
+ gsub_file 'config/routes.rb', /(#{look_for})/mi, ''
42
+ end
43
+ end
44
+
45
+ def route_name(name, path, route_options = {})
46
+ look_for = "\n map.#{name} '#{path}', :controller => '#{route_options[:controller]}', :action => '#{route_options[:action]}'"
47
+ logger.route "map.#{name} '#{path}', :controller => '#{route_options[:controller]}', :action => '#{route_options[:action]}'"
48
+ unless options[:pretend]
49
+ gsub_file 'config/routes.rb', /(#{look_for})/mi, ''
50
+ end
51
+ end
52
+
53
+ def insert_into(file, line)
54
+ logger.remove "#{line} from #{file}"
55
+ unless options[:pretend]
56
+ gsub_file file, "\n #{line}", ''
57
+ end
58
+ end
59
+ end
60
+
61
+ Rails::Generator::Commands::List.class_eval do
62
+ def route_resource(*resources)
63
+ resource_list = resources.map { |r| r.to_sym.inspect }.join(', ')
64
+ logger.route "map.resource #{resource_list}"
65
+ end
66
+
67
+ def route_name(name, path, options = {})
68
+ logger.route "map.#{name} '#{path}', :controller => '{options[:controller]}', :action => '#{options[:action]}'"
69
+ end
70
+
71
+ def insert_into(file, line)
72
+ logger.insert "#{line} into #{file}"
73
+ end
74
+ end
@@ -0,0 +1,114 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/lib/insert_commands.rb")
2
+ class NiftyAuthenticationGenerator < Rails::Generator::Base
3
+ attr_accessor :user_name, :sessions_name
4
+
5
+ def initialize(runtime_args, runtime_options = {})
6
+ super
7
+
8
+ @user_name = @args[0] || 'user'
9
+ @sessions_name = @args[1] || 'sessions'
10
+ end
11
+
12
+ def manifest
13
+ record do |m|
14
+ m.directory "app/models"
15
+ m.directory "app/controllers"
16
+ m.directory "app/helpers"
17
+ m.directory "app/views"
18
+ m.directory "lib"
19
+
20
+ m.directory "app/views/#{user_plural_name}"
21
+ m.template "user.rb", "app/models/#{user_singular_name}.rb"
22
+ m.template "users_controller.rb", "app/controllers/#{user_plural_name}_controller.rb"
23
+ m.template "users_helper.rb", "app/helpers/#{user_plural_name}_helper.rb"
24
+ m.template "views/#{view_language}/signup.html.#{view_language}", "app/views/#{user_plural_name}/new.html.#{view_language}"
25
+
26
+ m.directory "app/views/#{sessions_underscore_name}"
27
+ m.template "sessions_controller.rb", "app/controllers/#{sessions_underscore_name}_controller.rb"
28
+ m.template "sessions_helper.rb", "app/helpers/#{sessions_underscore_name}_helper.rb"
29
+ m.template "views/#{view_language}/login.html.#{view_language}", "app/views/#{sessions_underscore_name}/new.html.#{view_language}"
30
+
31
+ m.template "authentication.rb", "lib/authentication.rb"
32
+ m.migration_template "migration.rb", "db/migrate", :migration_file_name => "create_#{user_plural_name}"
33
+
34
+ m.route_resources user_plural_name
35
+ m.route_resources sessions_underscore_name
36
+ m.route_name :login, 'login', :controller => sessions_underscore_name, :action => 'new'
37
+ m.route_name :logout, 'logout', :controller => sessions_underscore_name, :action => 'destroy'
38
+ m.route_name :signup, 'signup', :controller => user_plural_name, :action => 'new'
39
+
40
+ m.insert_into 'app/controllers/application.rb', 'include Authentication'
41
+
42
+ if test_framework == :rspec
43
+ m.directory "spec"
44
+ m.directory "spec/fixtures"
45
+ m.directory "spec/controllers"
46
+ m.directory "spec/models"
47
+ m.template "fixtures.yml", "spec/fixtures/#{user_plural_name}.yml"
48
+ m.template "tests/rspec/user.rb", "spec/models/#{user_singular_name}_spec.rb"
49
+ m.template "tests/rspec/users_controller.rb", "spec/controllers/#{user_plural_name}_controller_spec.rb"
50
+ m.template "tests/rspec/sessions_controller.rb", "spec/controllers/#{sessions_underscore_name}_controller_spec.rb"
51
+ else
52
+ m.directory "test"
53
+ m.directory "test/fixtures"
54
+ m.directory "test/functional"
55
+ m.directory "test/unit"
56
+ m.template "fixtures.yml", "test/fixtures/#{user_plural_name}.yml"
57
+ m.template "tests/#{test_framework}/user.rb", "test/unit/#{user_singular_name}_test.rb"
58
+ m.template "tests/#{test_framework}/users_controller.rb", "test/functional/#{user_plural_name}_controller_test.rb"
59
+ m.template "tests/#{test_framework}/sessions_controller.rb", "test/functional/#{sessions_underscore_name}_controller_test.rb"
60
+ end
61
+ end
62
+ end
63
+
64
+ def user_singular_name
65
+ user_name.underscore
66
+ end
67
+
68
+ def user_plural_name
69
+ user_singular_name.pluralize
70
+ end
71
+
72
+ def user_class_name
73
+ user_name.camelize
74
+ end
75
+
76
+ def user_plural_class_name
77
+ user_plural_name.camelize
78
+ end
79
+
80
+ def sessions_underscore_name
81
+ sessions_name.underscore
82
+ end
83
+
84
+ def sessions_class_name
85
+ sessions_name.camelize
86
+ end
87
+
88
+ protected
89
+
90
+ def view_language
91
+ options[:haml] ? 'haml' : 'erb'
92
+ end
93
+
94
+ def test_framework
95
+ options[:test_framework] ||= File.exist?(destination_path("spec")) ? :rspec : :testunit
96
+ end
97
+
98
+ def add_options!(opt)
99
+ opt.separator ''
100
+ opt.separator 'Options:'
101
+ opt.on("--testunit", "Use test/unit for test files.") { options[:test_framework] = :testunit }
102
+ opt.on("--rspec", "Use RSpec for test files.") { options[:test_framework] = :rspec }
103
+ opt.on("--shoulda", "Use RSpec for test files.") { options[:test_framework] = :shoulda }
104
+ opt.on("--haml", "Generate HAML views instead of ERB.") { |v| options[:haml] = true }
105
+ end
106
+
107
+ def banner
108
+ <<-EOS
109
+ Creates user model and controllers to handle registration and authentication.
110
+
111
+ USAGE: #{$0} #{spec.name} [user_name] [sessions_controller_name]
112
+ EOS
113
+ end
114
+ end
@@ -0,0 +1,36 @@
1
+ # This module is included in your application controller which makes
2
+ # several methods available to all controllers and views. Here's a
3
+ # common example you might add to your application layout file.
4
+ #
5
+ # <%% if logged_in? %>
6
+ # Welcome <%%= current_user.username %>! Not you?
7
+ # <%%= link_to "Log out", logout_path %>
8
+ # <%% else %>
9
+ # <%%= link_to "Sign up", signup_path %> or
10
+ # <%%= link_to "log in", login_path %>.
11
+ # <%% end %>
12
+ #
13
+ # You can also restrict unregistered users from accessing a controller using
14
+ # a before filter. For example.
15
+ #
16
+ # before_filter :login_required, :except => [:index, :show]
17
+ module Authentication
18
+ def self.included(controller)
19
+ controller.send :helper_method, :current_<%= user_singular_name %>, :logged_in?
20
+ end
21
+
22
+ def current_<%= user_singular_name %>
23
+ @current_<%= user_singular_name %> ||= <%= user_class_name %>.find(session[:<%= user_singular_name %>_id]) if session[:<%= user_singular_name %>_id]
24
+ end
25
+
26
+ def logged_in?
27
+ current_<%= user_singular_name %>
28
+ end
29
+
30
+ def login_required
31
+ unless logged_in?
32
+ flash[:error] = "You must first log in or sign up before accessing this page."
33
+ redirect_to login_url
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,12 @@
1
+ # password: "secret"
2
+ one:
3
+ username: foo
4
+ email: foo@example.com
5
+ password_hash: 3488f5f7efecab14b91eb96169e5e1ee518a569f
6
+ password_salt: bef65e058905c379436d80d1a32e7374b139e7b0
7
+
8
+ two:
9
+ username: bar
10
+ email: bar@example.com
11
+ password_hash: 3488f5f7efecab14b91eb96169e5e1ee518a569f
12
+ password_salt: bef65e058905c379436d80d1a32e7374b139e7b0
@@ -0,0 +1,15 @@
1
+ class Create<%= user_plural_class_name %> < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :<%= user_plural_name %> do |t|
4
+ t.column :username, :string
5
+ t.column :email, :string
6
+ t.column :password_hash, :string
7
+ t.column :password_salt, :string
8
+ t.timestamps
9
+ end
10
+ end
11
+
12
+ def self.down
13
+ drop_table :<%= user_plural_name %>
14
+ end
15
+ end
@@ -0,0 +1,22 @@
1
+ class <%= sessions_class_name %>Controller < ApplicationController
2
+ def new
3
+ end
4
+
5
+ def create
6
+ <%= user_singular_name %> = <%= user_class_name %>.authenticate(params[:login], params[:password])
7
+ if <%= user_singular_name %>
8
+ session[:<%= user_singular_name %>_id] = <%= user_singular_name %>.id
9
+ flash[:notice] = "Logged in successfully."
10
+ redirect_to root_url
11
+ else
12
+ flash.now[:error] = "Invalid login or password."
13
+ render :action => 'new'
14
+ end
15
+ end
16
+
17
+ def destroy
18
+ session[:<%= user_singular_name %>_id] = nil
19
+ flash[:notice] = "You have been logged out."
20
+ redirect_to root_url
21
+ end
22
+ end
@@ -0,0 +1,2 @@
1
+ module <%= sessions_class_name %>Helper
2
+ end
@@ -0,0 +1,25 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe <%= sessions_class_name %>Controller do
4
+ fixtures :all
5
+ integrate_views
6
+
7
+ it "new action should render new template" do
8
+ get :new
9
+ response.should render_template(:new)
10
+ end
11
+
12
+ it "create action should render new template when authentication is invalid" do
13
+ <%= user_class_name %>.stubs(:authenticate).returns(nil)
14
+ post :create
15
+ response.should render_template(:new)
16
+ session['<%= user_singular_name %>_id'].should be_nil
17
+ end
18
+
19
+ it "create action should redirect when authentication is valid" do
20
+ <%= user_class_name %>.stubs(:authenticate).returns(<%= user_class_name %>.first)
21
+ post :create
22
+ response.should redirect_to(root_url)
23
+ session['<%= user_singular_name %>_id'].should == <%= user_class_name %>.first.id
24
+ end
25
+ end
@@ -0,0 +1,62 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe <%= user_class_name %> do
4
+ def new_<%= user_singular_name %>(attributes = {})
5
+ attributes[:username] ||= 'foo'
6
+ attributes[:email] ||= 'foo@example.com'
7
+ attributes[:password] ||= 'abc123'
8
+ attributes[:password_confirmation] ||= attributes[:password]
9
+ <%= user_class_name %>.new(attributes)
10
+ end
11
+
12
+ it "should be valid" do
13
+ new_<%= user_singular_name %>.should be_valid
14
+ end
15
+
16
+ it "should require username" do
17
+ new_<%= user_singular_name %>(:username => '').should have(1).error_on(:username)
18
+ end
19
+
20
+ it "should require password" do
21
+ new_<%= user_singular_name %>(:password => '').should have(1).error_on(:password)
22
+ end
23
+
24
+ it "should require well formed email" do
25
+ new_<%= user_singular_name %>(:email => 'foo@bar@example.com').should have(1).error_on(:email)
26
+ end
27
+
28
+ it "should require matching password confirmation" do
29
+ new_<%= user_singular_name %>(:password_confirmation => 'nonmatching').should have(1).error_on(:password)
30
+ end
31
+
32
+ it "should generate password hash and salt on create" do
33
+ <%= user_singular_name %> = new_<%= user_singular_name %>
34
+ <%= user_singular_name %>.save!
35
+ <%= user_singular_name %>.password_hash.should_not be_nil
36
+ <%= user_singular_name %>.password_salt.should_not be_nil
37
+ end
38
+
39
+ it "should authenticate by username" do
40
+ <%= user_class_name %>.delete_all
41
+ <%= user_singular_name %> = new_<%= user_singular_name %>(:username => 'foobar', :password => 'secret')
42
+ <%= user_singular_name %>.save!
43
+ <%= user_class_name %>.authenticate('foobar', 'secret').should == <%= user_singular_name %>
44
+ end
45
+
46
+ it "should authenticate by email" do
47
+ <%= user_class_name %>.delete_all
48
+ <%= user_singular_name %> = new_<%= user_singular_name %>(:email => 'foo@bar.com', :password => 'secret')
49
+ <%= user_singular_name %>.save!
50
+ <%= user_class_name %>.authenticate('foo@bar.com', 'secret').should == <%= user_singular_name %>
51
+ end
52
+
53
+ it "should not authenticate bad username" do
54
+ <%= user_class_name %>.authenticate('nonexisting', 'secret').should be_nil
55
+ end
56
+
57
+ it "should not authenticate bad password" do
58
+ <%= user_class_name %>.delete_all
59
+ new_<%= user_singular_name %>(:username => 'foobar', :password => 'secret').save!
60
+ <%= user_class_name %>.authenticate('foobar', 'badpassword').should be_nil
61
+ end
62
+ end
@@ -0,0 +1,24 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe <%= user_plural_class_name %>Controller do
4
+ fixtures :all
5
+ integrate_views
6
+
7
+ it "new action should render new template" do
8
+ get :new
9
+ response.should render_template(:new)
10
+ end
11
+
12
+ it "create action should render new template when model is invalid" do
13
+ <%= user_class_name %>.any_instance.stubs(:valid?).returns(false)
14
+ post :create
15
+ response.should render_template(:new)
16
+ end
17
+
18
+ it "create action should redirect when model is valid" do
19
+ <%= user_class_name %>.any_instance.stubs(:valid?).returns(true)
20
+ post :create
21
+ response.should redirect_to(root_url)
22
+ session['<%= user_singular_name %>_id'].should == assigns['<%= user_singular_name %>'].id
23
+ end
24
+ end
@@ -0,0 +1,26 @@
1
+ require 'test_helper'
2
+
3
+ class <%= sessions_class_name %>ControllerTest < ActionController::TestCase
4
+ context "new action" do
5
+ should "render new template" do
6
+ get :new
7
+ assert_template 'new'
8
+ end
9
+ end
10
+
11
+ context "create action" do
12
+ should "render new template when authentication is invalid" do
13
+ <%= user_class_name %>.stubs(:authenticate).returns(nil)
14
+ post :create
15
+ assert_template 'new'
16
+ assert_nil session['<%= user_singular_name %>_id']
17
+ end
18
+
19
+ should "redirect when authentication is valid" do
20
+ <%= user_class_name %>.stubs(:authenticate).returns(<%= user_class_name %>.first)
21
+ post :create
22
+ assert_redirected_to root_url
23
+ assert_equal <%= user_class_name %>.first.id, session['<%= user_singular_name %>_id']
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,64 @@
1
+ require 'test_helper'
2
+
3
+ class <%= user_class_name %>Test < ActiveSupport::TestCase
4
+ def new_<%= user_singular_name %>(attributes = {})
5
+ attributes[:username] ||= 'foo'
6
+ attributes[:email] ||= 'foo@example.com'
7
+ attributes[:password] ||= 'abc123'
8
+ attributes[:password_confirmation] ||= attributes[:password]
9
+ <%= user_singular_name %> = <%= user_class_name %>.new(attributes)
10
+ <%= user_singular_name %>.valid? # run validations
11
+ <%= user_singular_name %>
12
+ end
13
+
14
+ should "be valid" do
15
+ assert new_<%= user_singular_name %>.valid?
16
+ end
17
+
18
+ should "require username" do
19
+ assert new_<%= user_singular_name %>(:username => '').errors.on(:username)
20
+ end
21
+
22
+ should "require password" do
23
+ assert new_<%= user_singular_name %>(:password => '').errors.on(:password)
24
+ end
25
+
26
+ should "require well formed email" do
27
+ assert new_<%= user_singular_name %>(:email => 'foo@bar@example.com').errors.on(:email)
28
+ end
29
+
30
+ should "require matching password confirmation" do
31
+ assert new_<%= user_singular_name %>(:password_confirmation => 'nonmatching').errors.on(:password)
32
+ end
33
+
34
+ should "generate password hash and salt on create" do
35
+ <%= user_singular_name %> = new_<%= user_singular_name %>
36
+ <%= user_singular_name %>.save!
37
+ assert <%= user_singular_name %>.password_hash
38
+ assert <%= user_singular_name %>.password_salt
39
+ end
40
+
41
+ should "authenticate by username" do
42
+ <%= user_class_name %>.delete_all
43
+ <%= user_singular_name %> = new_<%= user_singular_name %>(:username => 'foobar', :password => 'secret')
44
+ <%= user_singular_name %>.save!
45
+ assert_equal <%= user_singular_name %>, <%= user_class_name %>.authenticate('foobar', 'secret')
46
+ end
47
+
48
+ should "authenticate by email" do
49
+ <%= user_class_name %>.delete_all
50
+ <%= user_singular_name %> = new_<%= user_singular_name %>(:email => 'foo@bar.com', :password => 'secret')
51
+ <%= user_singular_name %>.save!
52
+ assert_equal <%= user_singular_name %>, <%= user_class_name %>.authenticate('foo@bar.com', 'secret')
53
+ end
54
+
55
+ should "not authenticate bad username" do
56
+ assert_nil <%= user_class_name %>.authenticate('nonexisting', 'secret')
57
+ end
58
+
59
+ should "not authenticate bad password" do
60
+ <%= user_class_name %>.delete_all
61
+ new_<%= user_singular_name %>(:username => 'foobar', :password => 'secret').save!
62
+ assert_nil <%= user_class_name %>.authenticate('foobar', 'badpassword')
63
+ end
64
+ end
@@ -0,0 +1,25 @@
1
+ require 'test_helper'
2
+
3
+ class <%= user_plural_class_name %>ControllerTest < ActionController::TestCase
4
+ context "new action" do
5
+ should "render new template" do
6
+ get :new
7
+ assert_template 'new'
8
+ end
9
+ end
10
+
11
+ context "create action" do
12
+ should "render new template when model is invalid" do
13
+ <%= user_class_name %>.any_instance.stubs(:valid?).returns(false)
14
+ post :create
15
+ assert_template 'new'
16
+ end
17
+
18
+ should "redirect when model is valid" do
19
+ <%= user_class_name %>.any_instance.stubs(:valid?).returns(true)
20
+ post :create
21
+ assert_redirected_to root_url
22
+ assert_equal assigns['<%= user_singular_name %>'].id, session['<%= user_singular_name %>_id']
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,22 @@
1
+ require 'test_helper'
2
+
3
+ class <%= sessions_class_name %>ControllerTest < ActionController::TestCase
4
+ def test_new
5
+ get :new
6
+ assert_template 'new'
7
+ end
8
+
9
+ def test_create_invalid
10
+ <%= user_class_name %>.stubs(:authenticate).returns(nil)
11
+ post :create
12
+ assert_template 'new'
13
+ assert_nil session['<%= user_singular_name %>_id']
14
+ end
15
+
16
+ def test_create_valid
17
+ <%= user_class_name %>.stubs(:authenticate).returns(<%= user_class_name %>.first)
18
+ post :create
19
+ assert_redirected_to root_url
20
+ assert_equal <%= user_class_name %>.first.id, session['<%= user_singular_name %>_id']
21
+ end
22
+ end
@@ -0,0 +1,64 @@
1
+ require 'test_helper'
2
+
3
+ class <%= user_class_name %>Test < ActiveSupport::TestCase
4
+ def new_<%= user_singular_name %>(attributes = {})
5
+ attributes[:username] ||= 'foo'
6
+ attributes[:email] ||= 'foo@example.com'
7
+ attributes[:password] ||= 'abc123'
8
+ attributes[:password_confirmation] ||= attributes[:password]
9
+ <%= user_singular_name %> = <%= user_class_name %>.new(attributes)
10
+ <%= user_singular_name %>.valid? # run validations
11
+ <%= user_singular_name %>
12
+ end
13
+
14
+ def test_valid
15
+ assert new_<%= user_singular_name %>.valid?
16
+ end
17
+
18
+ def test_require_username
19
+ assert new_<%= user_singular_name %>(:username => '').errors.on(:username)
20
+ end
21
+
22
+ def test_require_password
23
+ assert new_<%= user_singular_name %>(:password => '').errors.on(:password)
24
+ end
25
+
26
+ def test_require_well_formed_email
27
+ assert new_<%= user_singular_name %>(:email => 'foo@bar@example.com').errors.on(:email)
28
+ end
29
+
30
+ def test_require_matching_password_confirmation
31
+ assert new_<%= user_singular_name %>(:password_confirmation => 'nonmatching').errors.on(:password)
32
+ end
33
+
34
+ def test_generate_password_hash_and_salt_on_create
35
+ <%= user_singular_name %> = new_<%= user_singular_name %>
36
+ <%= user_singular_name %>.save!
37
+ assert <%= user_singular_name %>.password_hash
38
+ assert <%= user_singular_name %>.password_salt
39
+ end
40
+
41
+ def test_authenticate_by_username
42
+ <%= user_class_name %>.delete_all
43
+ <%= user_singular_name %> = new_<%= user_singular_name %>(:username => 'foobar', :password => 'secret')
44
+ <%= user_singular_name %>.save!
45
+ assert_equal <%= user_singular_name %>, <%= user_class_name %>.authenticate('foobar', 'secret')
46
+ end
47
+
48
+ def test_authenticate_by_email
49
+ <%= user_class_name %>.delete_all
50
+ <%= user_singular_name %> = new_<%= user_singular_name %>(:email => 'foo@bar.com', :password => 'secret')
51
+ <%= user_singular_name %>.save!
52
+ assert_equal <%= user_singular_name %>, <%= user_class_name %>.authenticate('foo@bar.com', 'secret')
53
+ end
54
+
55
+ def test_authenticate_bad_username
56
+ assert_nil <%= user_class_name %>.authenticate('nonexisting', 'secret')
57
+ end
58
+
59
+ def test_authenticate_bad_password
60
+ <%= user_class_name %>.delete_all
61
+ new_<%= user_singular_name %>(:username => 'foobar', :password => 'secret').save!
62
+ assert_nil <%= user_class_name %>.authenticate('foobar', 'badpassword')
63
+ end
64
+ end
@@ -0,0 +1,21 @@
1
+ require 'test_helper'
2
+
3
+ class <%= user_plural_class_name %>ControllerTest < ActionController::TestCase
4
+ def test_new
5
+ get :new
6
+ assert_template 'new'
7
+ end
8
+
9
+ def test_create_invalid
10
+ <%= user_class_name %>.any_instance.stubs(:valid?).returns(false)
11
+ post :create
12
+ assert_template 'new'
13
+ end
14
+
15
+ def test_create_valid
16
+ <%= user_class_name %>.any_instance.stubs(:valid?).returns(true)
17
+ post :create
18
+ assert_redirected_to root_url
19
+ assert_equal assigns['<%= user_singular_name %>'].id, session['<%= user_singular_name %>_id']
20
+ end
21
+ end
@@ -0,0 +1,33 @@
1
+ class <%= user_class_name %> < ActiveRecord::Base
2
+ # new columns need to be added here to be writable through mass assignment
3
+ attr_accessible :username, :email, :password, :password_confirmation
4
+
5
+ attr_accessor :password
6
+ before_create :prepare_password
7
+
8
+ validates_presence_of :username
9
+ validates_format_of :email, :with => /^[-a-z0-9_+\.]+\@([-a-z0-9]+\.)+[a-z0-9]{2,4}$/i
10
+ validates_presence_of :password, :on => :create
11
+ validates_confirmation_of :password
12
+
13
+ # login can be either username or email address
14
+ def self.authenticate(login, pass)
15
+ <%= user_singular_name %> = find_by_username(login) || find_by_email(login)
16
+ return <%= user_singular_name %> if <%= user_singular_name %> && <%= user_singular_name %>.matching_password?(pass)
17
+ end
18
+
19
+ def matching_password?(pass)
20
+ self.password_hash == encrypt_password(pass)
21
+ end
22
+
23
+ private
24
+
25
+ def prepare_password
26
+ self.password_salt = Digest::SHA1.hexdigest([Time.now, rand].join)
27
+ self.password_hash = encrypt_password(password)
28
+ end
29
+
30
+ def encrypt_password(pass)
31
+ Digest::SHA1.hexdigest([pass, password_salt].join)
32
+ end
33
+ end
@@ -0,0 +1,16 @@
1
+ class <%= user_plural_class_name %>Controller < ApplicationController
2
+ def new
3
+ @<%= user_singular_name %> = <%= user_class_name %>.new
4
+ end
5
+
6
+ def create
7
+ @<%= user_singular_name %> = <%= user_class_name %>.new(params[:<%= user_singular_name %>])
8
+ if @<%= user_singular_name %>.save
9
+ session[:<%= user_singular_name %>_id] = @<%= user_singular_name %>.id
10
+ flash[:notice] = "Thank you for signing up! You are now logged in."
11
+ redirect_to root_url
12
+ else
13
+ render :action => 'new'
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,2 @@
1
+ module <%= user_plural_class_name %>Helper
2
+ end
@@ -0,0 +1,15 @@
1
+ <%% title "Log in" %>
2
+
3
+ <p>Don't have an account? <%%= link_to "Sign up!", signup_path %></p>
4
+
5
+ <%% form_tag sessions_path do %>
6
+ <p>
7
+ <%%= label_tag :login, "Username or Email Address" %><br />
8
+ <%%= text_field_tag :login, params[:login] %>
9
+ </p>
10
+ <p>
11
+ <%%= label_tag :password %><br />
12
+ <%%= password_field_tag :password %>
13
+ </p>
14
+ <p><%%= submit_tag "Log in" %></p>
15
+ <%% end %>
@@ -0,0 +1,24 @@
1
+ <%% title "Sign up" %>
2
+
3
+ <p>Already have an account? <%%= link_to "Log in", login_path %>.</p>
4
+
5
+ <%% form_for @<%= user_singular_name %> do |f| %>
6
+ <%%= f.error_messages %>
7
+ <p>
8
+ <%%= f.label :username %><br />
9
+ <%%= f.text_field :username %>
10
+ </p>
11
+ <p>
12
+ <%%= f.label :email, "Email Address" %><br />
13
+ <%%= f.text_field :email %>
14
+ </p>
15
+ <p>
16
+ <%%= f.label :password %><br />
17
+ <%%= f.password_field :password %>
18
+ </p>
19
+ <p>
20
+ <%%= f.label :password_confirmation, "Confirm Password" %><br />
21
+ <%%= f.password_field :password_confirmation %>
22
+ </p>
23
+ <p><%%= f.submit "Sign up" %></p>
24
+ <%% end %>
@@ -0,0 +1,15 @@
1
+ - title "Log in"
2
+
3
+ %p== Don't have an account? #{link_to "Sign up!", signup_path}
4
+
5
+ - form_tag sessions_path do
6
+ %p
7
+ = label_tag :login, "Username or Email Address"
8
+ %br
9
+ = text_field_tag :login, params[:login]
10
+ %p
11
+ = label_tag :password
12
+ %br
13
+ = password_field_tag :password
14
+ %p
15
+ = submit_tag "Log in"
@@ -0,0 +1,24 @@
1
+ - title "Sign up"
2
+
3
+ %p== Already have an account? #{link_to "Log in", login_path}.
4
+
5
+ - form_for @<%= user_singular_name %> do |f|
6
+ = f.error_messages
7
+ %p
8
+ = f.label :username
9
+ %br
10
+ = f.text_field :username
11
+ %p
12
+ = f.label :email, "Email Address"
13
+ %br
14
+ = f.text_field :email
15
+ %p
16
+ = f.label :password
17
+ %br
18
+ = f.password_field :email
19
+ %p
20
+ = f.label :password_confirmation, "Confirm Password"
21
+ %br
22
+ = f.password_field :password_confirmation
23
+ %p
24
+ = f.submit "Sign up"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nifty-generators
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Bates
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-11-04 00:00:00 -08:00
12
+ date: 2008-11-10 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -31,6 +31,30 @@ files:
31
31
  - lib/nifty_generators.rb
32
32
  - LICENSE
33
33
  - Manifest
34
+ - rails_generators/nifty_authentication/lib/insert_commands.rb
35
+ - rails_generators/nifty_authentication/nifty_authentication_generator.rb
36
+ - rails_generators/nifty_authentication/templates/authentication.rb
37
+ - rails_generators/nifty_authentication/templates/fixtures.yml
38
+ - rails_generators/nifty_authentication/templates/migration.rb
39
+ - rails_generators/nifty_authentication/templates/sessions_controller.rb
40
+ - rails_generators/nifty_authentication/templates/sessions_helper.rb
41
+ - rails_generators/nifty_authentication/templates/tests/rspec/sessions_controller.rb
42
+ - rails_generators/nifty_authentication/templates/tests/rspec/user.rb
43
+ - rails_generators/nifty_authentication/templates/tests/rspec/users_controller.rb
44
+ - rails_generators/nifty_authentication/templates/tests/shoulda/sessions_controller.rb
45
+ - rails_generators/nifty_authentication/templates/tests/shoulda/user.rb
46
+ - rails_generators/nifty_authentication/templates/tests/shoulda/users_controller.rb
47
+ - rails_generators/nifty_authentication/templates/tests/testunit/sessions_controller.rb
48
+ - rails_generators/nifty_authentication/templates/tests/testunit/user.rb
49
+ - rails_generators/nifty_authentication/templates/tests/testunit/users_controller.rb
50
+ - rails_generators/nifty_authentication/templates/user.rb
51
+ - rails_generators/nifty_authentication/templates/users_controller.rb
52
+ - rails_generators/nifty_authentication/templates/users_helper.rb
53
+ - rails_generators/nifty_authentication/templates/views/erb/login.html.erb
54
+ - rails_generators/nifty_authentication/templates/views/erb/signup.html.erb
55
+ - rails_generators/nifty_authentication/templates/views/haml/login.html.haml
56
+ - rails_generators/nifty_authentication/templates/views/haml/signup.html.haml
57
+ - rails_generators/nifty_authentication/USAGE
34
58
  - rails_generators/nifty_config/nifty_config_generator.rb
35
59
  - rails_generators/nifty_config/templates/config.yml
36
60
  - rails_generators/nifty_config/templates/load_config.rb
@@ -97,6 +121,7 @@ files:
97
121
  - README.rdoc
98
122
  - tasks/deployment.rake
99
123
  - test/test_helper.rb
124
+ - test/test_nifty_authentication_generator.rb
100
125
  - test/test_nifty_config_generator.rb
101
126
  - test/test_nifty_layout_generator.rb
102
127
  - test/test_nifty_scaffold_generator.rb