niftier-generators 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (106) hide show
  1. data/CHANGELOG +7 -0
  2. data/LICENSE +20 -0
  3. data/README.rdoc +89 -0
  4. data/Rakefile +18 -0
  5. data/features/niftier_authentication.feature +59 -0
  6. data/features/niftier_config.feature +17 -0
  7. data/features/niftier_layout.feature +19 -0
  8. data/features/niftier_scaffold.feature +22 -0
  9. data/features/step_definitions/common_steps.rb +37 -0
  10. data/features/step_definitions/rails_setup_steps.rb +6 -0
  11. data/features/support/env.rb +6 -0
  12. data/features/support/matchers.rb +7 -0
  13. data/lib/generators/niftier/authentication/USAGE +55 -0
  14. data/lib/generators/niftier/authentication/authentication_generator.rb +145 -0
  15. data/lib/generators/niftier/authentication/templates/authentication.rb +60 -0
  16. data/lib/generators/niftier/authentication/templates/authlogic_session.rb +2 -0
  17. data/lib/generators/niftier/authentication/templates/fixtures.yml +24 -0
  18. data/lib/generators/niftier/authentication/templates/migration.rb +20 -0
  19. data/lib/generators/niftier/authentication/templates/sessions_controller.rb +45 -0
  20. data/lib/generators/niftier/authentication/templates/sessions_helper.rb +2 -0
  21. data/lib/generators/niftier/authentication/templates/tests/rspec/sessions_controller.rb +39 -0
  22. data/lib/generators/niftier/authentication/templates/tests/rspec/user.rb +83 -0
  23. data/lib/generators/niftier/authentication/templates/tests/rspec/users_controller.rb +26 -0
  24. data/lib/generators/niftier/authentication/templates/tests/shoulda/sessions_controller.rb +40 -0
  25. data/lib/generators/niftier/authentication/templates/tests/shoulda/user.rb +85 -0
  26. data/lib/generators/niftier/authentication/templates/tests/shoulda/users_controller.rb +27 -0
  27. data/lib/generators/niftier/authentication/templates/tests/testunit/sessions_controller.rb +36 -0
  28. data/lib/generators/niftier/authentication/templates/tests/testunit/user.rb +88 -0
  29. data/lib/generators/niftier/authentication/templates/tests/testunit/users_controller.rb +23 -0
  30. data/lib/generators/niftier/authentication/templates/user.rb +42 -0
  31. data/lib/generators/niftier/authentication/templates/users_controller.rb +18 -0
  32. data/lib/generators/niftier/authentication/templates/users_helper.rb +2 -0
  33. data/lib/generators/niftier/authentication/templates/views/erb/login.html.erb +30 -0
  34. data/lib/generators/niftier/authentication/templates/views/erb/signup.html.erb +24 -0
  35. data/lib/generators/niftier/authentication/templates/views/haml/login.html.haml +30 -0
  36. data/lib/generators/niftier/authentication/templates/views/haml/signup.html.haml +24 -0
  37. data/lib/generators/niftier/config/USAGE +23 -0
  38. data/lib/generators/niftier/config/config_generator.rb +24 -0
  39. data/lib/generators/niftier/config/templates/config.yml +14 -0
  40. data/lib/generators/niftier/config/templates/load_config.rb +2 -0
  41. data/lib/generators/niftier/layout/USAGE +25 -0
  42. data/lib/generators/niftier/layout/layout_generator.rb +29 -0
  43. data/lib/generators/niftier/layout/templates/error_messages_helper.rb +23 -0
  44. data/lib/generators/niftier/layout/templates/layout.html.erb +24 -0
  45. data/lib/generators/niftier/layout/templates/layout.html.haml +26 -0
  46. data/lib/generators/niftier/layout/templates/layout_helper.rb +26 -0
  47. data/lib/generators/niftier/layout/templates/stylesheet.css +75 -0
  48. data/lib/generators/niftier/layout/templates/stylesheet.sass +66 -0
  49. data/lib/generators/niftier/scaffold/USAGE +51 -0
  50. data/lib/generators/niftier/scaffold/scaffold_generator.rb +241 -0
  51. data/lib/generators/niftier/scaffold/templates/actions/create.rb +9 -0
  52. data/lib/generators/niftier/scaffold/templates/actions/destroy.rb +6 -0
  53. data/lib/generators/niftier/scaffold/templates/actions/edit.rb +3 -0
  54. data/lib/generators/niftier/scaffold/templates/actions/index.rb +3 -0
  55. data/lib/generators/niftier/scaffold/templates/actions/new.rb +3 -0
  56. data/lib/generators/niftier/scaffold/templates/actions/show.rb +3 -0
  57. data/lib/generators/niftier/scaffold/templates/actions/update.rb +9 -0
  58. data/lib/generators/niftier/scaffold/templates/controller.rb +3 -0
  59. data/lib/generators/niftier/scaffold/templates/fixtures.yml +9 -0
  60. data/lib/generators/niftier/scaffold/templates/helper.rb +2 -0
  61. data/lib/generators/niftier/scaffold/templates/migration.rb +16 -0
  62. data/lib/generators/niftier/scaffold/templates/model.rb +3 -0
  63. data/lib/generators/niftier/scaffold/templates/tests/rspec/actions/create.rb +11 -0
  64. data/lib/generators/niftier/scaffold/templates/tests/rspec/actions/destroy.rb +6 -0
  65. data/lib/generators/niftier/scaffold/templates/tests/rspec/actions/edit.rb +4 -0
  66. data/lib/generators/niftier/scaffold/templates/tests/rspec/actions/index.rb +4 -0
  67. data/lib/generators/niftier/scaffold/templates/tests/rspec/actions/new.rb +4 -0
  68. data/lib/generators/niftier/scaffold/templates/tests/rspec/actions/show.rb +4 -0
  69. data/lib/generators/niftier/scaffold/templates/tests/rspec/actions/update.rb +11 -0
  70. data/lib/generators/niftier/scaffold/templates/tests/rspec/controller.rb +8 -0
  71. data/lib/generators/niftier/scaffold/templates/tests/rspec/model.rb +7 -0
  72. data/lib/generators/niftier/scaffold/templates/tests/shoulda/actions/create.rb +13 -0
  73. data/lib/generators/niftier/scaffold/templates/tests/shoulda/actions/destroy.rb +8 -0
  74. data/lib/generators/niftier/scaffold/templates/tests/shoulda/actions/edit.rb +6 -0
  75. data/lib/generators/niftier/scaffold/templates/tests/shoulda/actions/index.rb +6 -0
  76. data/lib/generators/niftier/scaffold/templates/tests/shoulda/actions/new.rb +6 -0
  77. data/lib/generators/niftier/scaffold/templates/tests/shoulda/actions/show.rb +6 -0
  78. data/lib/generators/niftier/scaffold/templates/tests/shoulda/actions/update.rb +13 -0
  79. data/lib/generators/niftier/scaffold/templates/tests/shoulda/controller.rb +5 -0
  80. data/lib/generators/niftier/scaffold/templates/tests/shoulda/model.rb +7 -0
  81. data/lib/generators/niftier/scaffold/templates/tests/testunit/actions/create.rb +11 -0
  82. data/lib/generators/niftier/scaffold/templates/tests/testunit/actions/destroy.rb +6 -0
  83. data/lib/generators/niftier/scaffold/templates/tests/testunit/actions/edit.rb +4 -0
  84. data/lib/generators/niftier/scaffold/templates/tests/testunit/actions/index.rb +4 -0
  85. data/lib/generators/niftier/scaffold/templates/tests/testunit/actions/new.rb +4 -0
  86. data/lib/generators/niftier/scaffold/templates/tests/testunit/actions/show.rb +4 -0
  87. data/lib/generators/niftier/scaffold/templates/tests/testunit/actions/update.rb +11 -0
  88. data/lib/generators/niftier/scaffold/templates/tests/testunit/controller.rb +5 -0
  89. data/lib/generators/niftier/scaffold/templates/tests/testunit/model.rb +7 -0
  90. data/lib/generators/niftier/scaffold/templates/views/erb/_form.html.erb +10 -0
  91. data/lib/generators/niftier/scaffold/templates/views/erb/edit.html.erb +14 -0
  92. data/lib/generators/niftier/scaffold/templates/views/erb/index.html.erb +29 -0
  93. data/lib/generators/niftier/scaffold/templates/views/erb/new.html.erb +7 -0
  94. data/lib/generators/niftier/scaffold/templates/views/erb/show.html.erb +20 -0
  95. data/lib/generators/niftier/scaffold/templates/views/haml/_form.html.haml +10 -0
  96. data/lib/generators/niftier/scaffold/templates/views/haml/edit.html.haml +14 -0
  97. data/lib/generators/niftier/scaffold/templates/views/haml/index.html.haml +25 -0
  98. data/lib/generators/niftier/scaffold/templates/views/haml/new.html.haml +7 -0
  99. data/lib/generators/niftier/scaffold/templates/views/haml/show.html.haml +20 -0
  100. data/lib/generators/niftier.rb +15 -0
  101. data/test/test_helper.rb +119 -0
  102. data/test/test_niftier_authentication_generator.rb +274 -0
  103. data/test/test_niftier_config_generator.rb +37 -0
  104. data/test/test_niftier_layout_generator.rb +42 -0
  105. data/test/test_niftier_scaffold_generator.rb +534 -0
  106. metadata +173 -0
data/CHANGELOG ADDED
@@ -0,0 +1,7 @@
1
+ 0.1.0 (August 1st, 2010)
2
+
3
+ * initial release - forked from nifty-generators 0.4.0
4
+ * dropped rails 2 support
5
+ * added global config file
6
+ * updated application layout to enforce UJS, load order of external dependencies
7
+ * use the name niftier-generators
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Ryan Bates
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,89 @@
1
+ = Niftier Generators
2
+
3
+ A collection of (more) useful Rails generator scripts for scaffolding, layout files, authentication, and more.
4
+
5
+ == Install
6
+
7
+ gem install niftier-generators
8
+
9
+ Include it in your Gemfile.
10
+
11
+ gem "niftier-generators"
12
+
13
+ == Usage
14
+
15
+ Once you install the gem, the generators will be available to all Rails applications on your system. If you run script/generate without any additional arguments you should see the available generators listed.
16
+
17
+ To run the generator, go to your rails project directory and call it using the script/generate or script/destroy command.
18
+
19
+ rails generate niftier:scaffold Recipe name:string index new
20
+
21
+
22
+ == Included Generators
23
+
24
+ * niftier:layout: generates generic layout, stylesheet, and helper files.
25
+ * niftier:scaffold: generates a controller and optional model/migration.
26
+ * niftier:config: generates a config YAML file and loader.
27
+ * niftier:authentication: generates user model with sign up and log in.
28
+
29
+ To view the README for each generator, run it with the +help+ option.
30
+
31
+ rails generate niftier:layout --help
32
+
33
+ == Troubleshooting and FAQs
34
+
35
+ <b>What is the difference between niftier_scaffold and built-in scaffold?</b>
36
+
37
+ One of the primary differences is that niftier_scaffold allows you to choose which controller actions to generate.
38
+
39
+ script/generate niftier_scaffold post name:string index new edit
40
+
41
+ There are a few changes to the generated code as well, such as no XML format by default.
42
+
43
+ It also offers support for HAML, Shoulda, and RSpec.
44
+
45
+ <b>I get "undefined method 'title'" error.</b>
46
+
47
+ Try running niftier:layout, that will generate this helper method. Or you can just change the templates to whatever approach you prefer for setting the title.
48
+
49
+
50
+ <b>I can't set new attributes in my model.</b>
51
+
52
+ Add the attribute to the attr_accessible line in the model.
53
+
54
+
55
+ <b>I get "undefined method 'root_url'" error.</b>
56
+
57
+ Some generators default redirecting to the root_url. Set this in your routes.rb file like this (substituting your controller name).
58
+
59
+ map.root :controller => 'foo'
60
+
61
+
62
+ <b>I get a missing database error.</b>
63
+
64
+ Run <tt>rake db:migrate</tt>.
65
+
66
+
67
+ <b>I get a routing error when I try to submit a form.</b>
68
+
69
+ Try restarting your development server. Sometimes it doesn't detect the change in the routing.
70
+
71
+
72
+ <b>The tests/specs don't work.</b>
73
+
74
+ Make sure you have mocha installed and require it in your spec/test helper.
75
+
76
+ gem install mocha
77
+
78
+ # in spec_helper.rb
79
+ config.mock_with :mocha
80
+
81
+ # in test_helper.rb
82
+ require 'mocha'
83
+
84
+
85
+ == Found a bug?
86
+
87
+ If you are having a problem with Niftier Generators, first look at the FAQs above. If you still cannot resolve it, please submit an issue here.
88
+
89
+ http://github.com/mattyven/niftier-generators/issues
data/Rakefile ADDED
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'rake/testtask'
4
+ require 'cucumber'
5
+ require 'cucumber/rake/task'
6
+
7
+ desc "Run tests."
8
+ Rake::TestTask.new do |t|
9
+ t.libs << "test"
10
+ t.test_files = FileList['test/test*.rb']
11
+ t.verbose = true
12
+ end
13
+
14
+ Cucumber::Rake::Task.new(:features) do |t|
15
+ t.cucumber_opts = "features --format progress"
16
+ end
17
+
18
+ task :default => [:test, :features]
@@ -0,0 +1,59 @@
1
+ Feature: Niftier Authentication Generator
2
+ In order to authenticate users
3
+ As a rails developer
4
+ I want to generate some user authentication
5
+
6
+ Scenario: Generate default authentication
7
+ Given a new Rails app
8
+ When I run "rails g niftier:authentication"
9
+ Then I should see the following files
10
+ | app/models/user.rb |
11
+ | app/controllers/users_controller.rb |
12
+ | app/helpers/users_helper.rb |
13
+ | app/views/users/new.html.erb |
14
+ | app/controllers/sessions_controller.rb |
15
+ | app/helpers/sessions_helper.rb |
16
+ | app/views/sessions/new.html.erb |
17
+ | lib/authentication.rb |
18
+ | test/fixtures/users.yml |
19
+ | test/unit/user_test.rb |
20
+ | test/functional/users_controller_test.rb |
21
+ | test/functional/sessions_controller_test.rb |
22
+ | db/migrate |
23
+ And I should see the following in file "config/routes.rb"
24
+ | resources :sessions |
25
+ | resources :users |
26
+ | match 'login' => 'sessions#new', :as => :login |
27
+ | match 'logout' => 'sessions#destroy', :as => :logout |
28
+ | match 'signup' => 'users#new', :as => :signup |
29
+ And I should see "include Authentication" in file "app/controllers/application_controller.rb"
30
+ When I run "rails g niftier:layout -f"
31
+ And I run "rake db:migrate"
32
+ And I add "gem 'mocha', :group => :test" to file "Gemfile"
33
+ Then I should successfully run "rake test"
34
+
35
+ Scenario: Generate named authentication
36
+ Given a new Rails app
37
+ When I run "rails g niftier:authentication Account CurrentSession"
38
+ Then I should see the following files
39
+ | app/models/account.rb |
40
+ | app/controllers/accounts_controller.rb |
41
+ | app/helpers/accounts_helper.rb |
42
+ | app/views/accounts/new.html.erb |
43
+ | app/controllers/current_sessions_controller.rb |
44
+ | app/helpers/current_sessions_helper.rb |
45
+ | app/views/current_sessions/new.html.erb |
46
+ | test/fixtures/accounts.yml |
47
+ | test/unit/account_test.rb |
48
+ | test/functional/accounts_controller_test.rb |
49
+ | test/functional/current_sessions_controller_test.rb |
50
+ And I should see the following in file "config/routes.rb"
51
+ | resources :current_sessions |
52
+ | resources :accounts |
53
+ | match 'login' => 'current_sessions#new', :as => :login |
54
+ | match 'logout' => 'current_sessions#destroy', :as => :logout |
55
+ | match 'signup' => 'accounts#new', :as => :signup |
56
+ When I run "rails g niftier:layout -f"
57
+ And I run "rake db:migrate"
58
+ And I add "gem 'mocha', :group => :test" to file "Gemfile"
59
+ Then I should successfully run "rake test"
@@ -0,0 +1,17 @@
1
+ Feature: Niftier Config Generator
2
+ In order to have configure an app
3
+ As a rails developer
4
+ I want to generate a config file and manager
5
+
6
+ Scenario: Generate normal config
7
+ Given a new Rails app
8
+ When I run "rails g niftier:config"
9
+ Then I should see file "config/app_config.yml"
10
+ And I should see file "config/initializers/load_app_config.rb"
11
+
12
+ Scenario: Generate named config
13
+ Given a new Rails app
14
+ When I run "rails g niftier:config FooBar"
15
+ Then I should see "FOO_BAR_CONFIG" in file "config/initializers/load_foo_bar_config.rb"
16
+ And I should see "config/foo_bar_config.yml" in file "config/initializers/load_foo_bar_config.rb"
17
+ And I should see file "config/foo_bar_config.yml"
@@ -0,0 +1,19 @@
1
+ Feature: Niftier Layout Generator
2
+ In order to have a layout
3
+ As a rails developer
4
+ I want to generate a simple layout
5
+
6
+ Scenario: Generate normal application layout
7
+ Given a new Rails app
8
+ When I run "rails g niftier:layout -f"
9
+ Then I should see "stylesheet_link_tag "application"" in file "app/views/layouts/application.html.erb"
10
+ And I should see file "app/helpers/layout_helper.rb"
11
+ And I should see file "app/helpers/error_messages_helper.rb"
12
+ And I should see file "public/stylesheets/application.css"
13
+
14
+ Scenario: Generate named layout with haml and sass
15
+ Given a new Rails app
16
+ When I run "rails g niftier:layout FooBar --haml -f"
17
+ Then I should see "stylesheet_link_tag "foo_bar"" in file "app/views/layouts/foo_bar.html.haml"
18
+ And I should see file "public/stylesheets/sass/foo_bar.sass"
19
+ And I should see file "app/helpers/layout_helper.rb"
@@ -0,0 +1,22 @@
1
+ Feature: Niftier Scaffold Generator
2
+ In order to manage a resource
3
+ As a rails developer
4
+ I want to generate a model, controller, and views for that resource
5
+
6
+ Scenario: Generate scaffold for simple resource
7
+ Given a new Rails app
8
+ When I run "rails g niftier:scaffold Project name:string"
9
+ Then I should see the following files
10
+ | app/models/project.rb |
11
+ | app/controllers/projects_controller.rb |
12
+ | app/helpers/projects_helper.rb |
13
+ | app/views/projects/index.html.erb |
14
+ | app/views/projects/show.html.erb |
15
+ | app/views/projects/new.html.erb |
16
+ | app/views/projects/edit.html.erb |
17
+ | db/migrate |
18
+ And I should see "resources :projects" in file "config/routes.rb"
19
+ When I run "rails g niftier:layout -f"
20
+ And I run "rake db:migrate"
21
+ And I add "gem 'mocha', :group => :test" to file "Gemfile"
22
+ Then I should successfully run "rake test"
@@ -0,0 +1,37 @@
1
+ When /^I run "([^\"]*)"$/ do |command|
2
+ system("cd #{@current_directory} && #{command}").should be_true
3
+ end
4
+
5
+ When /^I add "([^\"]*)" to file "([^\"]*)"$/ do |content, short_path|
6
+ path = File.join(@current_directory, short_path)
7
+ File.should exist(path)
8
+ File.open(path, 'a') { |f| f.write(content) }
9
+ end
10
+
11
+ Then /^I should see file "([^\"]*)"$/ do |path|
12
+ File.should exist(File.join(@current_directory, path))
13
+ end
14
+
15
+ Then /^I should see "(.*)" in file "([^\"]*)"$/ do |content, short_path|
16
+ path = File.join(@current_directory, short_path)
17
+ File.should exist(path)
18
+ File.readlines(path).join.should include(content)
19
+ end
20
+
21
+ Then /^I should see the following files$/ do |table|
22
+ table.raw.flatten.each do |path|
23
+ File.should exist(File.join(@current_directory, path))
24
+ end
25
+ end
26
+
27
+ Then /^I should see the following in file "([^\"]*)"$/ do |short_path, table|
28
+ path = File.join(@current_directory, short_path)
29
+ File.should exist(path)
30
+ table.raw.flatten.each do |content|
31
+ File.readlines(path).join.should include(content)
32
+ end
33
+ end
34
+
35
+ Then /^I should successfully run "([^\"]*)"$/ do |command|
36
+ system("cd #{@current_directory} && #{command}").should be_true
37
+ end
@@ -0,0 +1,6 @@
1
+ Given /^a new Rails app$/ do
2
+ FileUtils.mkdir_p("tmp")
3
+ system("rails tmp/rails_app").should be_true
4
+ system("ln -s ../../../lib/generators tmp/rails_app/lib/generators").should be_true
5
+ @current_directory = File.expand_path("tmp/rails_app")
6
+ end
@@ -0,0 +1,6 @@
1
+ require 'cucumber'
2
+ require 'spec'
3
+
4
+ Before do
5
+ FileUtils.rm_rf "tmp/rails_app"
6
+ end
@@ -0,0 +1,7 @@
1
+ Spec::Matchers.define :exist do |path|
2
+ match do
3
+ File.exist?(path)
4
+ end
5
+ failure_message_for_should { "Expected #{path} to exist but no file found" }
6
+ failure_message_for_should_not { "Expected #{path} to not exist but file was found" }
7
+ end
@@ -0,0 +1,55 @@
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 niftier: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 "session". 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
+ Make sure to setup the authlogic gem if you are using that option.
17
+
18
+ gem "authlogic" # in Gemfile
19
+
20
+ NOTE: Rails 3.0.0.rc no longer loads the /lib directory by default. To fix
21
+ this, add the following line to config/application.rb:
22
+
23
+ config.autoload_paths += %W(#{config.root}/lib)
24
+
25
+ Examples:
26
+ rails generate niftier:authentication
27
+
28
+ Creates user model, users_controller, and sessions_controller.
29
+
30
+ rails generate niftier:authentication account
31
+
32
+ Creates account model, accounts_controller, and sessions_controller.
33
+
34
+ rails generate niftier:authentication Account UserSession
35
+
36
+ Creates account model, accounts_controller, and user_sessions_controller.
37
+
38
+ Methods:
39
+ There are several methods generated which you can use in your application.
40
+ Here's a common example of what you might add to your layout.
41
+
42
+ <% if logged_in? %>
43
+ Welcome <%= current_user.username %>! Not you?
44
+ <%= link_to "Log out", logout_path %>
45
+ <% else %>
46
+ <%= link_to "Sign up", signup_path %> or
47
+ <%= link_to "log in", login_path %>.
48
+ <% end %>
49
+
50
+ You can also restrict unregistered users from accessing a controller using
51
+ a before filter. For example.
52
+
53
+ before_filter :login_required, :except => [:index, :show]
54
+
55
+ See the generated file lib/authentication.rb for details.
@@ -0,0 +1,145 @@
1
+ require 'generators/niftier'
2
+ require 'rails/generators/migration'
3
+
4
+ module Niftier
5
+ module Generators
6
+ class AuthenticationGenerator < Base
7
+ include Rails::Generators::Migration
8
+
9
+ argument :user_name, :type => :string, :default => 'user', :banner => 'user_name'
10
+ argument :session_name, :type => :string, :default => '[[DEFAULT]]', :banner => 'sessions_controller_name'
11
+
12
+ class_option :testunit, :desc => 'Use test/unit for test files.', :group => 'Test framework', :type => :boolean
13
+ class_option :rspec, :desc => 'Use RSpec for test files.', :group => 'Test framework', :type => :boolean
14
+ class_option :shoulda, :desc => 'Use shoulda for test files.', :group => 'Test framework', :type => :boolean
15
+
16
+ class_option :haml, :desc => 'Generate HAML views instead of ERB.', :type => :boolean
17
+ class_option :authlogic, :desc => 'Use Authlogic for authentication.', :type => :boolean
18
+
19
+ def create_model_files
20
+ template 'user.rb', "app/models/#{user_singular_name}.rb"
21
+ template 'authlogic_session.rb', "app/models/#{user_singular_name}_session.rb" if options.authlogic?
22
+ end
23
+
24
+ def create_controller_files
25
+ template 'users_controller.rb', "app/controllers/#{user_plural_name}_controller.rb"
26
+ template 'sessions_controller.rb', "app/controllers/#{session_plural_name}_controller.rb"
27
+ end
28
+
29
+ def create_helper_files
30
+ template 'users_helper.rb', "app/helpers/#{user_plural_name}_helper.rb"
31
+ template 'sessions_helper.rb', "app/helpers/#{session_plural_name}_helper.rb"
32
+ end
33
+
34
+ def create_view_files
35
+ template "views/#{view_language}/signup.html.#{view_language}", "app/views/#{user_plural_name}/new.html.#{view_language}"
36
+ template "views/#{view_language}/login.html.#{view_language}", "app/views/#{session_plural_name}/new.html.#{view_language}"
37
+ end
38
+
39
+ def create_lib_files
40
+ template 'authentication.rb', 'lib/authentication.rb'
41
+ end
42
+
43
+ def create_routes
44
+ route "resources #{user_plural_name.to_sym.inspect}"
45
+ route "resources #{session_plural_name.to_sym.inspect}"
46
+ route "match 'login' => '#{session_plural_name}#new', :as => :login"
47
+ route "match 'logout' => '#{session_plural_name}#destroy', :as => :logout"
48
+ route "match 'signup' => '#{user_plural_name}#new', :as => :signup"
49
+ end
50
+
51
+ def create_migration
52
+ migration_template 'migration.rb', "db/migrate/create_#{user_plural_name}.rb"
53
+ end
54
+
55
+ def create_include_line
56
+ inject_into_class "app/controllers/application_controller.rb", "ApplicationController", " include Authentication\n"
57
+ end
58
+
59
+ def create_test_files
60
+ if test_framework == :rspec
61
+ template 'fixtures.yml', "spec/fixtures/#{user_plural_name}.yml"
62
+ template 'tests/rspec/user.rb', "spec/models/#{user_singular_name}_spec.rb"
63
+ template 'tests/rspec/users_controller.rb', "spec/controllers/#{user_plural_name}_controller_spec.rb"
64
+ template 'tests/rspec/sessions_controller.rb', "spec/controllers/#{session_plural_name}_controller_spec.rb"
65
+ else
66
+ template 'fixtures.yml', "test/fixtures/#{user_plural_name}.yml"
67
+ template "tests/#{test_framework}/user.rb", "test/unit/#{user_singular_name}_test.rb"
68
+ template "tests/#{test_framework}/users_controller.rb", "test/functional/#{user_plural_name}_controller_test.rb"
69
+ template "tests/#{test_framework}/sessions_controller.rb", "test/functional/#{session_plural_name}_controller_test.rb"
70
+ end
71
+ end
72
+
73
+ private
74
+
75
+ def session_name
76
+ @_session_name ||= @session_name == '[[DEFAULT]]' ?
77
+ (options.authlogic? ? user_name + '_session' : 'session') :
78
+ @session_name
79
+ end
80
+
81
+ def user_singular_name
82
+ user_name.underscore
83
+ end
84
+
85
+ def user_plural_name
86
+ user_singular_name.pluralize
87
+ end
88
+
89
+ def user_class_name
90
+ user_name.camelize
91
+ end
92
+
93
+ def user_plural_class_name
94
+ user_plural_name.camelize
95
+ end
96
+
97
+ def session_singular_name
98
+ session_name.underscore
99
+ end
100
+
101
+ def session_plural_name
102
+ session_singular_name.pluralize
103
+ end
104
+
105
+ def session_class_name
106
+ session_name.camelize
107
+ end
108
+
109
+ def session_plural_class_name
110
+ session_plural_name.camelize
111
+ end
112
+
113
+ def view_language
114
+ options.haml? ? 'haml' : 'erb'
115
+ end
116
+
117
+ def test_framework
118
+ return @test_framework if defined?(@test_framework)
119
+ if options.testunit?
120
+ return @test_framework = :testunit
121
+ elsif options.rspec?
122
+ return @test_framework = :rspec
123
+ elsif options.shoulda?
124
+ return @test_framework = :shoulda
125
+ else
126
+ return @test_framework = File.exist?(destination_path('spec')) ? :rspec : :testunit
127
+ end
128
+ end
129
+
130
+ def destination_path(path)
131
+ File.join(destination_root, path)
132
+ end
133
+
134
+ # FIXME: Should be proxied to ActiveRecord::Generators::Base
135
+ # Implement the required interface for Rails::Generators::Migration.
136
+ def self.next_migration_number(dirname) #:nodoc:
137
+ if ActiveRecord::Base.timestamped_migrations
138
+ Time.now.utc.strftime("%Y%m%d%H%M%S")
139
+ else
140
+ "%.3d" % (current_migration_number(dirname) + 1)
141
+ end
142
+ end
143
+ end
144
+ end
145
+ end
@@ -0,0 +1,60 @@
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_singular_name %>.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?, :redirect_to_target_or_default
20
+ end
21
+
22
+ <%- if options[:authlogic] -%>
23
+ def current_<%= session_singular_name %>
24
+ return @current_<%= session_singular_name %> if defined?(@current_<%= session_singular_name %>)
25
+ @current_<%= session_singular_name %> = <%= session_class_name %>.find
26
+ end
27
+
28
+ def current_<%= user_singular_name %>
29
+ return @current_<%= user_singular_name %> if defined?(@current_<%= user_singular_name %>)
30
+ @current_<%= user_singular_name %> = current_<%= session_singular_name %> && current_<%= session_singular_name %>.record
31
+ end
32
+ <%- else -%>
33
+ def current_<%= user_singular_name %>
34
+ @current_<%= user_singular_name %> ||= <%= user_class_name %>.find(session[:<%= user_singular_name %>_id]) if session[:<%= user_singular_name %>_id]
35
+ end
36
+ <%- end -%>
37
+
38
+ def logged_in?
39
+ current_<%= user_singular_name %>
40
+ end
41
+
42
+ def login_required
43
+ unless logged_in?
44
+ flash[:error] = "You must first log in or sign up before accessing this page."
45
+ store_target_location
46
+ redirect_to login_url
47
+ end
48
+ end
49
+
50
+ def redirect_to_target_or_default(default)
51
+ redirect_to(session[:return_to] || default)
52
+ session[:return_to] = nil
53
+ end
54
+
55
+ private
56
+
57
+ def store_target_location
58
+ session[:return_to] = request.request_uri
59
+ end
60
+ end
@@ -0,0 +1,2 @@
1
+ class <%= session_class_name %> < Authlogic::Session::Base
2
+ end
@@ -0,0 +1,24 @@
1
+ # password: "secret"
2
+ foo:
3
+ username: foo
4
+ email: foo@example.com
5
+ <%- if options[:authlogic] -%>
6
+ persistence_token: d5ddba13ed4408ea2b0a12ab18ed2d2eda086279736bdc121ca726a11f1e4b99217d9c534c2cc4ebb22729349c8c5fdbe1529e1f2c3c5859c62ef4dd9feea25c
7
+ crypted_password: 3d16c326648cccafe3d4b4cb024475c381dda92f430dfedf6f933e1f61203bacb6bae2437849bdb43b06be335e23790e4aa03902b3c28c3bbbbe27d501e521f3
8
+ password_salt: n6z_wtpWoIsHgQb5IcFd
9
+ <%- else -%>
10
+ password_hash: 3488f5f7efecab14b91eb96169e5e1ee518a569f
11
+ password_salt: bef65e058905c379436d80d1a32e7374b139e7b0
12
+ <%- end -%>
13
+
14
+ bar:
15
+ username: bar
16
+ email: bar@example.com
17
+ <%- if options[:authlogic] -%>
18
+ persistence_token: 19e074bd7cb506ab3e7e53e41f24f0ab3221c8cb68111f4c1aa43965114ad734233979a50a9463537487cdca18c279ac91c4bc83693d589625d446493322394c
19
+ crypted_password: 3bc9f4113ca645a186765df3d31a9352d0067bf2304ba0cdd6b08a7f3d58c6668ab1762fa3e76aef466ea2ff188399d8e6c40244fa59312bb4112292dac9f7f0
20
+ password_salt: UiAh9ejabnKRxqsiK0xO
21
+ <%- else -%>
22
+ password_hash: 3488f5f7efecab14b91eb96169e5e1ee518a569f
23
+ password_salt: bef65e058905c379436d80d1a32e7374b139e7b0
24
+ <%- end -%>
@@ -0,0 +1,20 @@
1
+ class Create<%= user_plural_class_name %> < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :<%= user_plural_name %> do |t|
4
+ t.string :username
5
+ t.string :email
6
+ <%- if options[:authlogic] -%>
7
+ t.string :persistence_token
8
+ t.string :crypted_password
9
+ <%- else -%>
10
+ t.string :password_hash
11
+ <%- end -%>
12
+ t.string :password_salt
13
+ t.timestamps
14
+ end
15
+ end
16
+
17
+ def self.down
18
+ drop_table :<%= user_plural_name %>
19
+ end
20
+ end