nifty-generators 0.3.2 → 0.4.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.
- data/CHANGELOG +9 -0
- data/README.rdoc +14 -8
- data/Rakefile +7 -1
- data/features/nifty_authentication.feature +59 -0
- data/features/nifty_config.feature +17 -0
- data/features/nifty_layout.feature +19 -0
- data/features/nifty_scaffold.feature +22 -0
- data/features/step_definitions/common_steps.rb +37 -0
- data/features/step_definitions/rails_setup_steps.rb +6 -0
- data/features/support/env.rb +6 -0
- data/features/support/matchers.rb +7 -0
- data/lib/generators/nifty.rb +15 -0
- data/lib/generators/nifty/authentication/USAGE +50 -0
- data/lib/generators/nifty/authentication/authentication_generator.rb +145 -0
- data/lib/generators/nifty/authentication/templates/authentication.rb +60 -0
- data/lib/generators/nifty/authentication/templates/authlogic_session.rb +2 -0
- data/lib/generators/nifty/authentication/templates/fixtures.yml +24 -0
- data/lib/generators/nifty/authentication/templates/migration.rb +20 -0
- data/lib/generators/nifty/authentication/templates/sessions_controller.rb +45 -0
- data/lib/generators/nifty/authentication/templates/sessions_helper.rb +2 -0
- data/lib/generators/nifty/authentication/templates/tests/rspec/sessions_controller.rb +39 -0
- data/lib/generators/nifty/authentication/templates/tests/rspec/user.rb +83 -0
- data/lib/generators/nifty/authentication/templates/tests/rspec/users_controller.rb +26 -0
- data/lib/generators/nifty/authentication/templates/tests/shoulda/sessions_controller.rb +40 -0
- data/lib/generators/nifty/authentication/templates/tests/shoulda/user.rb +85 -0
- data/lib/generators/nifty/authentication/templates/tests/shoulda/users_controller.rb +27 -0
- data/lib/generators/nifty/authentication/templates/tests/testunit/sessions_controller.rb +36 -0
- data/lib/generators/nifty/authentication/templates/tests/testunit/user.rb +88 -0
- data/lib/generators/nifty/authentication/templates/tests/testunit/users_controller.rb +23 -0
- data/lib/generators/nifty/authentication/templates/user.rb +42 -0
- data/lib/generators/nifty/authentication/templates/users_controller.rb +18 -0
- data/lib/generators/nifty/authentication/templates/users_helper.rb +2 -0
- data/lib/generators/nifty/authentication/templates/views/erb/login.html.erb +30 -0
- data/lib/generators/nifty/authentication/templates/views/erb/signup.html.erb +24 -0
- data/lib/generators/nifty/authentication/templates/views/haml/login.html.haml +30 -0
- data/lib/generators/nifty/authentication/templates/views/haml/signup.html.haml +24 -0
- data/lib/generators/nifty/config/USAGE +23 -0
- data/lib/generators/nifty/config/config_generator.rb +24 -0
- data/lib/generators/nifty/config/templates/config.yml +8 -0
- data/lib/generators/nifty/config/templates/load_config.rb +2 -0
- data/lib/generators/nifty/layout/USAGE +25 -0
- data/lib/generators/nifty/layout/layout_generator.rb +29 -0
- data/lib/generators/nifty/layout/templates/error_messages_helper.rb +23 -0
- data/lib/generators/nifty/layout/templates/layout.html.erb +19 -0
- data/lib/generators/nifty/layout/templates/layout.html.haml +21 -0
- data/lib/generators/nifty/layout/templates/layout_helper.rb +22 -0
- data/lib/generators/nifty/layout/templates/stylesheet.css +75 -0
- data/lib/generators/nifty/layout/templates/stylesheet.sass +66 -0
- data/lib/generators/nifty/scaffold/USAGE +51 -0
- data/lib/generators/nifty/scaffold/scaffold_generator.rb +241 -0
- data/lib/generators/nifty/scaffold/templates/actions/create.rb +9 -0
- data/lib/generators/nifty/scaffold/templates/actions/destroy.rb +6 -0
- data/lib/generators/nifty/scaffold/templates/actions/edit.rb +3 -0
- data/lib/generators/nifty/scaffold/templates/actions/index.rb +3 -0
- data/lib/generators/nifty/scaffold/templates/actions/new.rb +3 -0
- data/lib/generators/nifty/scaffold/templates/actions/show.rb +3 -0
- data/lib/generators/nifty/scaffold/templates/actions/update.rb +9 -0
- data/lib/generators/nifty/scaffold/templates/controller.rb +3 -0
- data/lib/generators/nifty/scaffold/templates/fixtures.yml +9 -0
- data/lib/generators/nifty/scaffold/templates/helper.rb +2 -0
- data/lib/generators/nifty/scaffold/templates/migration.rb +16 -0
- data/lib/generators/nifty/scaffold/templates/model.rb +3 -0
- data/lib/generators/nifty/scaffold/templates/tests/rspec/actions/create.rb +11 -0
- data/lib/generators/nifty/scaffold/templates/tests/rspec/actions/destroy.rb +6 -0
- data/lib/generators/nifty/scaffold/templates/tests/rspec/actions/edit.rb +4 -0
- data/lib/generators/nifty/scaffold/templates/tests/rspec/actions/index.rb +4 -0
- data/lib/generators/nifty/scaffold/templates/tests/rspec/actions/new.rb +4 -0
- data/lib/generators/nifty/scaffold/templates/tests/rspec/actions/show.rb +4 -0
- data/lib/generators/nifty/scaffold/templates/tests/rspec/actions/update.rb +11 -0
- data/lib/generators/nifty/scaffold/templates/tests/rspec/controller.rb +8 -0
- data/lib/generators/nifty/scaffold/templates/tests/rspec/model.rb +7 -0
- data/lib/generators/nifty/scaffold/templates/tests/shoulda/actions/create.rb +13 -0
- data/lib/generators/nifty/scaffold/templates/tests/shoulda/actions/destroy.rb +8 -0
- data/lib/generators/nifty/scaffold/templates/tests/shoulda/actions/edit.rb +6 -0
- data/lib/generators/nifty/scaffold/templates/tests/shoulda/actions/index.rb +6 -0
- data/lib/generators/nifty/scaffold/templates/tests/shoulda/actions/new.rb +6 -0
- data/lib/generators/nifty/scaffold/templates/tests/shoulda/actions/show.rb +6 -0
- data/lib/generators/nifty/scaffold/templates/tests/shoulda/actions/update.rb +13 -0
- data/lib/generators/nifty/scaffold/templates/tests/shoulda/controller.rb +5 -0
- data/lib/generators/nifty/scaffold/templates/tests/shoulda/model.rb +7 -0
- data/lib/generators/nifty/scaffold/templates/tests/testunit/actions/create.rb +11 -0
- data/lib/generators/nifty/scaffold/templates/tests/testunit/actions/destroy.rb +6 -0
- data/lib/generators/nifty/scaffold/templates/tests/testunit/actions/edit.rb +4 -0
- data/lib/generators/nifty/scaffold/templates/tests/testunit/actions/index.rb +4 -0
- data/lib/generators/nifty/scaffold/templates/tests/testunit/actions/new.rb +4 -0
- data/lib/generators/nifty/scaffold/templates/tests/testunit/actions/show.rb +4 -0
- data/lib/generators/nifty/scaffold/templates/tests/testunit/actions/update.rb +11 -0
- data/lib/generators/nifty/scaffold/templates/tests/testunit/controller.rb +5 -0
- data/lib/generators/nifty/scaffold/templates/tests/testunit/model.rb +7 -0
- data/lib/generators/nifty/scaffold/templates/views/erb/_form.html.erb +10 -0
- data/lib/generators/nifty/scaffold/templates/views/erb/edit.html.erb +14 -0
- data/lib/generators/nifty/scaffold/templates/views/erb/index.html.erb +29 -0
- data/lib/generators/nifty/scaffold/templates/views/erb/new.html.erb +7 -0
- data/lib/generators/nifty/scaffold/templates/views/erb/show.html.erb +20 -0
- data/lib/generators/nifty/scaffold/templates/views/haml/_form.html.haml +10 -0
- data/lib/generators/nifty/scaffold/templates/views/haml/edit.html.haml +14 -0
- data/lib/generators/nifty/scaffold/templates/views/haml/index.html.haml +25 -0
- data/lib/generators/nifty/scaffold/templates/views/haml/new.html.haml +7 -0
- data/lib/generators/nifty/scaffold/templates/views/haml/show.html.haml +20 -0
- data/rails_generators/nifty_layout/templates/layout.html.erb +1 -1
- data/rails_generators/nifty_scaffold/templates/views/erb/_form.html.erb +1 -1
- data/rails_generators/nifty_scaffold/templates/views/haml/_form.html.haml +1 -1
- metadata +118 -21
- data/lib/nifty_generators.rb +0 -3
data/CHANGELOG
CHANGED
|
@@ -1,3 +1,12 @@
|
|
|
1
|
+
0.4.0 (April 19th, 2010)
|
|
2
|
+
|
|
3
|
+
* adding error_messages_for and f.error_messages helper for Rails 3 nifty:layout
|
|
4
|
+
|
|
5
|
+
* adding initial Rails 3 support (thanks Henrik Hodne)
|
|
6
|
+
|
|
7
|
+
* adding xmlns to <head> element in nifty_layout
|
|
8
|
+
|
|
9
|
+
|
|
1
10
|
0.3.2 (February 16th, 2010)
|
|
2
11
|
|
|
3
12
|
* Including all files in gem, was missing files in rails_generator directory
|
data/README.rdoc
CHANGED
|
@@ -24,11 +24,24 @@ To run the generator, go to your rails project directory and call it using the s
|
|
|
24
24
|
* nifty_config: generates a config YAML file and loader.
|
|
25
25
|
* nifty_authentication: generates user model with sign up and log in.
|
|
26
26
|
|
|
27
|
-
To view the README for each generator, run it with the
|
|
27
|
+
To view the README for each generator, run it with the +help+ option.
|
|
28
28
|
|
|
29
29
|
script/generate nifty_layout --help
|
|
30
30
|
|
|
31
31
|
|
|
32
|
+
== Rails 3
|
|
33
|
+
|
|
34
|
+
To use Nifty Generators with Rails 3 you will need to include it in your Gemfile.
|
|
35
|
+
|
|
36
|
+
gem "nifty-generators"
|
|
37
|
+
|
|
38
|
+
The generators use a colon as the separator instead of an underscore.
|
|
39
|
+
|
|
40
|
+
rails g nifty:layout
|
|
41
|
+
|
|
42
|
+
The Rails 3 support is still in early development, please {report any issues}[http://github.com/ryanb/nifty-generators/issues] you find.
|
|
43
|
+
|
|
44
|
+
|
|
32
45
|
== Troubleshooting and FAQs
|
|
33
46
|
|
|
34
47
|
<b>What is the difference between nifty_scaffold and built-in scaffold?</b>
|
|
@@ -89,10 +102,3 @@ Also, make sure you're using Rails 2.1 or greater.
|
|
|
89
102
|
If you are having a problem with Nifty Generators, first look at the FAQs above. If you still cannot resolve it, please submit an issue here.
|
|
90
103
|
|
|
91
104
|
http://github.com/ryanb/nifty-generators/issues
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
== Rails 3
|
|
95
|
-
|
|
96
|
-
Nifty Generators does not yet officially work with Rails 3, but a fork is available here.
|
|
97
|
-
|
|
98
|
-
http://github.com/dvyjones/nifty-generators
|
data/Rakefile
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
require 'rubygems'
|
|
2
2
|
require 'rake'
|
|
3
3
|
require 'rake/testtask'
|
|
4
|
+
require 'cucumber'
|
|
5
|
+
require 'cucumber/rake/task'
|
|
4
6
|
|
|
5
7
|
desc "Run tests."
|
|
6
8
|
Rake::TestTask.new do |t|
|
|
@@ -9,4 +11,8 @@ Rake::TestTask.new do |t|
|
|
|
9
11
|
t.verbose = true
|
|
10
12
|
end
|
|
11
13
|
|
|
12
|
-
|
|
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: Nifty 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 nifty: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 nifty: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 nifty: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 nifty: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: Nifty 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 nifty: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 nifty: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: Nifty 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 nifty: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 nifty: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: Nifty 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 nifty: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 nifty: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,15 @@
|
|
|
1
|
+
require 'rails/generators/base'
|
|
2
|
+
|
|
3
|
+
module Nifty
|
|
4
|
+
module Generators
|
|
5
|
+
class Base < Rails::Generators::Base #:nodoc:
|
|
6
|
+
def self.source_root
|
|
7
|
+
@_nifty_source_root ||= File.expand_path(File.join(File.dirname(__FILE__), 'nifty', generator_name, 'templates'))
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def self.banner
|
|
11
|
+
"#{$0} nifty:#{generator_name} #{self.arguments.map{ |a| a.usage }.join(' ')} [options]"
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
@@ -0,0 +1,50 @@
|
|
|
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 "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
|
+
Examples:
|
|
21
|
+
rails generate nifty:authentication
|
|
22
|
+
|
|
23
|
+
Creates user model, users_controller, and sessions_controller.
|
|
24
|
+
|
|
25
|
+
rails generate nifty:authentication account
|
|
26
|
+
|
|
27
|
+
Creates account model, accounts_controller, and sessions_controller.
|
|
28
|
+
|
|
29
|
+
rails generate nifty:authentication Account UserSession
|
|
30
|
+
|
|
31
|
+
Creates account model, accounts_controller, and user_sessions_controller.
|
|
32
|
+
|
|
33
|
+
Methods:
|
|
34
|
+
There are several methods generated which you can use in your application.
|
|
35
|
+
Here's a common example of what you might add to your layout.
|
|
36
|
+
|
|
37
|
+
<% if logged_in? %>
|
|
38
|
+
Welcome <%= current_user.username %>! Not you?
|
|
39
|
+
<%= link_to "Log out", logout_path %>
|
|
40
|
+
<% else %>
|
|
41
|
+
<%= link_to "Sign up", signup_path %> or
|
|
42
|
+
<%= link_to "log in", login_path %>.
|
|
43
|
+
<% end %>
|
|
44
|
+
|
|
45
|
+
You can also restrict unregistered users from accessing a controller using
|
|
46
|
+
a before filter. For example.
|
|
47
|
+
|
|
48
|
+
before_filter :login_required, :except => [:index, :show]
|
|
49
|
+
|
|
50
|
+
See the generated file lib/authentication.rb for details.
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
require 'generators/nifty'
|
|
2
|
+
require 'rails/generators/migration'
|
|
3
|
+
|
|
4
|
+
module Nifty
|
|
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 crete_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
|