rails 0.8.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of rails might be problematic. Click here for more details.
- data/CHANGELOG +159 -0
- data/MIT-LICENSE +20 -0
- data/README +121 -0
- data/Rakefile +259 -0
- data/bin/rails +28 -0
- data/configs/apache.conf +44 -0
- data/configs/database.yml +13 -0
- data/dispatches/dispatch.fcgi +7 -0
- data/dispatches/dispatch.rb +10 -0
- data/dispatches/dispatch.servlet +45 -0
- data/doc/README_FOR_APP +2 -0
- data/doc/apache_protection +3 -0
- data/doc/index.html +94 -0
- data/environments/production.rb +6 -0
- data/environments/shared.rb +27 -0
- data/environments/shared_for_gem.rb +17 -0
- data/environments/test.rb +6 -0
- data/fresh_rakefile +101 -0
- data/generators/new_controller.rb +118 -0
- data/generators/new_mailer.rb +119 -0
- data/generators/new_model.rb +70 -0
- data/helpers/abstract_application.rb +8 -0
- data/helpers/application_helper.rb +6 -0
- data/helpers/test_helper.rb +9 -0
- data/html/404.html +6 -0
- data/html/500.html +6 -0
- data/html/index.html +1 -0
- data/lib/code_statistics.rb +71 -0
- data/lib/dispatcher.rb +46 -0
- data/lib/webrick_server.rb +158 -0
- metadata +117 -0
data/bin/rails
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
if ARGV[0]
|
2
|
+
ENV["RAILS_PKG_DESTINATION"] = File.expand_path(ARGV[0])
|
3
|
+
if RUBY_PLATFORM =~ /mswin32/
|
4
|
+
Dir.chdir File.dirname(__FILE__)
|
5
|
+
system %{rake.cmd fresh_gem_rails}
|
6
|
+
else
|
7
|
+
system %{ cd #{File.dirname(__FILE__)}; rake fresh_gem_rails }
|
8
|
+
end
|
9
|
+
else
|
10
|
+
puts <<-HELP
|
11
|
+
|
12
|
+
NAME
|
13
|
+
rails - creates a new Rails installation
|
14
|
+
|
15
|
+
SYNOPSIS
|
16
|
+
rails [full path]
|
17
|
+
|
18
|
+
DESCRIPTION
|
19
|
+
This generator will create a suggested directory structure, lots of minor helper
|
20
|
+
files, and a default configuration for creating a new Rails application. Once the
|
21
|
+
generator is done, you're adviced to look at the README in the root of the folder.
|
22
|
+
|
23
|
+
EXAMPLE
|
24
|
+
rails ~/Code/Ruby/weblog
|
25
|
+
|
26
|
+
This will generate a new Rails installation in the ~/Code/Ruby/weblog folder.
|
27
|
+
HELP
|
28
|
+
end
|
data/configs/apache.conf
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
# General Apache options
|
2
|
+
AddHandler fastcgi-script .fcgi
|
3
|
+
AddHandler cgi-script .cgi
|
4
|
+
Options +FollowSymLinks +ExecCGI
|
5
|
+
|
6
|
+
|
7
|
+
# Remember to set RubySafeLevel 0 in httpd.conf
|
8
|
+
<IfModule mod_ruby.c>
|
9
|
+
RubyRequire apache/ruby-run
|
10
|
+
<Files dispatch.rb>
|
11
|
+
SetHandler ruby-object
|
12
|
+
RubyHandler Apache::RubyRun.instance
|
13
|
+
</Files>
|
14
|
+
</IfModule>
|
15
|
+
|
16
|
+
|
17
|
+
# Make sure that mod_ruby.c has been added and loaded as a module with Apache
|
18
|
+
RewriteEngine On
|
19
|
+
|
20
|
+
RewriteRule ^dispatch.servlet$ / [R]
|
21
|
+
|
22
|
+
# Enable this rewrite rule to point to the controller/action that should serve root.
|
23
|
+
# RewriteRule ^$ /controller/action [R]
|
24
|
+
|
25
|
+
# Force fcgi
|
26
|
+
RewriteRule ^fcgi/([-_a-zA-Z0-9]+)/([-_a-zA-Z0-9]+)/([-_a-zA-Z0-9]+)$ /dispatch.fcgi?controller=$1&action=$2&id=$3 [QSA] [L]
|
27
|
+
RewriteRule ^fcgi/([-_a-zA-Z0-9]+)/([-_a-zA-Z0-9]+)$ /dispatch.fcgi?controller=$1&action=$2 [QSA] [L]
|
28
|
+
RewriteRule ^fcgi/([-_a-zA-Z0-9]+)/?$ /dispatch.fcgi?controller=$1&action=index [QSA] [L]
|
29
|
+
|
30
|
+
# Force mod_ruby
|
31
|
+
RewriteRule ^mruby/([-_a-zA-Z0-9]+)/([-_a-zA-Z0-9]+)/([-_a-zA-Z0-9]+)$ /dispatch.rb?controller=$1&action=$2&id=$3 [QSA] [L]
|
32
|
+
RewriteRule ^mruby/([-_a-zA-Z0-9]+)/([-_a-zA-Z0-9]+)$ /dispatch.rb?controller=$1&action=$2 [QSA] [L]
|
33
|
+
RewriteRule ^mruby/([-_a-zA-Z0-9]+)/?$ /dispatch.rb?controller=$1&action=index [QSA] [L]
|
34
|
+
|
35
|
+
# Default rewriting rules. Change extension from .cgi to .fcgi to switch to FCGI and to .rb to switch to mod_ruby
|
36
|
+
RewriteRule ^([-_a-zA-Z0-9]+)/([-_a-zA-Z0-9]+)/([-_a-zA-Z0-9]+)$ /dispatch.cgi?controller=$1&action=$2&id=$3 [QSA] [L]
|
37
|
+
RewriteRule ^([-_a-zA-Z0-9]+)/([-_a-zA-Z0-9]+)$ /dispatch.cgi?controller=$1&action=$2 [QSA] [L]
|
38
|
+
RewriteRule ^([-_a-zA-Z0-9]+)/$ /dispatch.cgi?controller=$1&action=index [QSA] [L]
|
39
|
+
RewriteRule ^([-_a-zA-Z0-9]+)$ /$1/ [R]
|
40
|
+
|
41
|
+
|
42
|
+
# You can also point these error messages to a controller/action
|
43
|
+
ErrorDocument 500 /500.html
|
44
|
+
ErrorDocument 404 /404.html
|
@@ -0,0 +1,10 @@
|
|
1
|
+
#!/usr/local/bin/ruby
|
2
|
+
|
3
|
+
require File.dirname(__FILE__) + "/../config/environments/production"
|
4
|
+
|
5
|
+
# If you're using RubyGems and mod_ruby, this require should be changed to an absolute path one, like:
|
6
|
+
# "/usr/local/lib/ruby/gems/1.8/gems/rails-0.8.0/lib/dispatcher" -- otherwise performance is severely impaired
|
7
|
+
require "dispatcher"
|
8
|
+
|
9
|
+
ADDITIONAL_LOAD_PATHS.each { |dir| $:.unshift "#{File.dirname(__FILE__)}/../#{dir}" }
|
10
|
+
Dispatcher.dispatch
|
@@ -0,0 +1,45 @@
|
|
1
|
+
#!/usr/local/bin/ruby
|
2
|
+
|
3
|
+
require File.dirname(__FILE__) + "/../config/environments/production"
|
4
|
+
require 'webrick_server'
|
5
|
+
require 'optparse'
|
6
|
+
|
7
|
+
OPTIONS = {
|
8
|
+
:port => 3000,
|
9
|
+
:ip => "127.0.0.1",
|
10
|
+
:cache_classes => false,
|
11
|
+
:server_root => File.expand_path(File.dirname(__FILE__)),
|
12
|
+
:server_type => SimpleServer
|
13
|
+
}
|
14
|
+
|
15
|
+
ARGV.options do |opts|
|
16
|
+
script_name = File.basename($0)
|
17
|
+
opts.banner = "Usage: ruby #{script_name} [options]"
|
18
|
+
|
19
|
+
opts.separator ""
|
20
|
+
|
21
|
+
opts.on("-p", "--port=port", Integer,
|
22
|
+
"Runs Rails on the specified port.",
|
23
|
+
"Default: 3000") { |OPTIONS[:port]| }
|
24
|
+
opts.on("-b", "--binding=ip", String,
|
25
|
+
"Binds Rails to the specified ip.",
|
26
|
+
"Default: 127.0.0.1") { |OPTIONS[:ip]| }
|
27
|
+
opts.on("-i", "--index=controller", String,
|
28
|
+
"Specifies an index controller that requests for root will go to (instead of congratulations screen)."
|
29
|
+
) { |OPTIONS[:index_controller]| }
|
30
|
+
opts.on("-d", "--daemon",
|
31
|
+
"Make Rails run as a Daemon (only works if fork is available -- meaning on *nix)."
|
32
|
+
) { OPTIONS[:server_type] = Daemon }
|
33
|
+
opts.on("-c", "--cache-classes",
|
34
|
+
"Caches class compilation which will speed up the serving of requests, but require a server restart on source changes."
|
35
|
+
) { |OPTIONS[:cache_classes]| }
|
36
|
+
|
37
|
+
opts.separator ""
|
38
|
+
|
39
|
+
opts.on("-h", "--help",
|
40
|
+
"Show this help message.") { puts opts; exit }
|
41
|
+
|
42
|
+
opts.parse!
|
43
|
+
end
|
44
|
+
|
45
|
+
DispatchServlet.dispatch(OPTIONS)
|
data/doc/README_FOR_APP
ADDED
data/doc/index.html
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
<html>
|
2
|
+
<head>
|
3
|
+
<title>Rails: Welcome on board</title>
|
4
|
+
<style>
|
5
|
+
body { background-color: #fff; color: #333; }
|
6
|
+
|
7
|
+
body, p, ol, ul, td {
|
8
|
+
font-family: verdana, arial, helvetica, sans-serif;
|
9
|
+
font-size: 12px;
|
10
|
+
line-height: 18px;
|
11
|
+
}
|
12
|
+
|
13
|
+
li {
|
14
|
+
margin-bottom: 7px;
|
15
|
+
}
|
16
|
+
|
17
|
+
pre {
|
18
|
+
background-color: #eee;
|
19
|
+
padding: 10px;
|
20
|
+
font-size: 11px;
|
21
|
+
}
|
22
|
+
|
23
|
+
a { color: #000; }
|
24
|
+
a:visited { color: #666; }
|
25
|
+
a:hover { color: #fff; background-color:#000; }
|
26
|
+
</style>
|
27
|
+
</head>
|
28
|
+
<body>
|
29
|
+
|
30
|
+
<h1>Congratulations, you're on Rails!</h1>
|
31
|
+
|
32
|
+
<p>
|
33
|
+
<i>You've succesfully configured your wb-server to point at this Rails application.</i>
|
34
|
+
</p>
|
35
|
+
|
36
|
+
<p>Before you move on, verify that the following conditions have been meet:</p>
|
37
|
+
|
38
|
+
<ol>
|
39
|
+
<li>The log directory and the empty log files must be writable to the web server (<code>chmod -R 777 log</code>).
|
40
|
+
<li>
|
41
|
+
The shebang line in the public/dispatch* files must reference your Ruby installation. <br/>
|
42
|
+
You might need to change it to <code>#!/usr/bin/env ruby</code> or point directly at the installation.
|
43
|
+
</li>
|
44
|
+
<li>
|
45
|
+
Rails on Apache needs to have the cgi handler and mod_rewrite enabled. <br/>
|
46
|
+
Somewhere in your httpd.conf, you should have:<br/>
|
47
|
+
<code>AddHandler cgi-script .cgi</code><br/>
|
48
|
+
<code>LoadModule rewrite_module libexec/httpd/mod_rewrite.so</code><br/>
|
49
|
+
<code>AddModule mod_rewrite.c</code>
|
50
|
+
</li>
|
51
|
+
</ol>
|
52
|
+
|
53
|
+
<p>Take the following steps to get started:</p>
|
54
|
+
|
55
|
+
<ol>
|
56
|
+
<li>Create empty production and test databases for your application.<br/>
|
57
|
+
<small>Warning: Don't point your test database at your production database, it'll destroy the latter on test runs!</small>
|
58
|
+
<li>Edit config/database.yml with your database settings.
|
59
|
+
<li>Create a new controller using the <code>script/new_controller</code> generator <br/>
|
60
|
+
<small>Help: Run with no arguments for documentation</small>
|
61
|
+
<li>Create a new model using the <code>script/new_model</code> generator <br/>
|
62
|
+
<small>Help: Run with no arguments for documentation</small>
|
63
|
+
<li>See all the tests run and by running <code>rake</code>.
|
64
|
+
<li>Develop your Rails application!
|
65
|
+
<li>Setup FastCGI or mod_ruby to get production-level performance
|
66
|
+
</ol>
|
67
|
+
|
68
|
+
<p>
|
69
|
+
Having problems getting up and running? First try debugging it yourself by looking at the log files. <br/> Then try the friendly Rails
|
70
|
+
community on IRC (<a href="http://www.rubyonrails.org/show/IRC">howto IRC</a>). It's on FreeNET in channel #rubyonrails.
|
71
|
+
</p>
|
72
|
+
|
73
|
+
<div style="float: left; margin-right: 20px">
|
74
|
+
<h2>Rails Online</h2>
|
75
|
+
|
76
|
+
<ul>
|
77
|
+
<li><a href="http://www.rubyonrails.org">Ruby on Rails</a></li>
|
78
|
+
<li><a href="http://activerecord.rubyonrails.org">Active Record</a></li>
|
79
|
+
<li><a href="http://actionpack.rubyonrails.org">Action Pack</a></li>
|
80
|
+
</ul>
|
81
|
+
</div>
|
82
|
+
|
83
|
+
<div style="float: left">
|
84
|
+
<h2>Beyond CGI</h2>
|
85
|
+
|
86
|
+
<ul>
|
87
|
+
<li><a href="http://www.fastcgi.com">FastCGI</a></li>
|
88
|
+
<li><a href="http://raa.ruby-lang.org/list.rhtml?name=fcgi">FastCGI bindings for Ruby</a></li>
|
89
|
+
<li><a href="http://modruby.net/en/">mod_ruby</a></li>
|
90
|
+
</ul>
|
91
|
+
</div>
|
92
|
+
|
93
|
+
</body>
|
94
|
+
</html>
|
@@ -0,0 +1,6 @@
|
|
1
|
+
require File.dirname(__FILE__) + "/shared"
|
2
|
+
|
3
|
+
ActiveRecord::Base.logger = ActionController::Base.logger = ActionMailer::Base.logger =
|
4
|
+
Logger.new(File.dirname(__FILE__) + "/../../log/production.log")
|
5
|
+
|
6
|
+
ActiveRecord::Base.establish_connection(database_configurations["production"])
|
@@ -0,0 +1,27 @@
|
|
1
|
+
ADDITIONAL_LOAD_PATHS = [
|
2
|
+
"app/models",
|
3
|
+
"app/controllers",
|
4
|
+
"app/helpers",
|
5
|
+
"config",
|
6
|
+
"lib",
|
7
|
+
"vendor",
|
8
|
+
"vendor/railties",
|
9
|
+
"vendor/railties/lib",
|
10
|
+
"vendor/activerecord/lib",
|
11
|
+
"vendor/actionpack/lib",
|
12
|
+
"vendor/actionmailer/lib"
|
13
|
+
]
|
14
|
+
|
15
|
+
ADDITIONAL_LOAD_PATHS.each { |dir| $:.unshift "#{File.dirname(__FILE__)}/../../#{dir}" }
|
16
|
+
|
17
|
+
require 'active_record'
|
18
|
+
require 'action_controller'
|
19
|
+
require 'action_mailer'
|
20
|
+
|
21
|
+
require 'yaml'
|
22
|
+
|
23
|
+
ActionController::Base.template_root = ActionMailer::Base.template_root = File.dirname(__FILE__) + '/../../app/views/'
|
24
|
+
|
25
|
+
def database_configurations
|
26
|
+
YAML::load(File.open(File.dirname(__FILE__) + "/../../config/database.yml"))
|
27
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
ADDITIONAL_LOAD_PATHS = [ "app/models", "app/controllers", "app/helpers", "config", "lib", "vendor" ]
|
2
|
+
ADDITIONAL_LOAD_PATHS.each { |dir| $:.unshift "#{File.dirname(__FILE__)}/../../#{dir}" }
|
3
|
+
|
4
|
+
require 'rubygems'
|
5
|
+
|
6
|
+
require_gem 'activerecord'
|
7
|
+
require_gem 'actionpack'
|
8
|
+
require_gem 'actionmailer'
|
9
|
+
require_gem 'rails'
|
10
|
+
|
11
|
+
require 'yaml'
|
12
|
+
|
13
|
+
ActionController::Base.template_root = ActionMailer::Base.template_root = File.dirname(__FILE__) + '/../../app/views/'
|
14
|
+
|
15
|
+
def database_configurations
|
16
|
+
YAML::load(File.open(File.dirname(__FILE__) + "/../../config/database.yml"))
|
17
|
+
end
|
@@ -0,0 +1,6 @@
|
|
1
|
+
require File.dirname(__FILE__) + "/shared"
|
2
|
+
|
3
|
+
ActiveRecord::Base.logger = ActionController::Base.logger = ActionMailer::Base.logger =
|
4
|
+
Logger.new(File.dirname(__FILE__) + "/../../log/test.log")
|
5
|
+
|
6
|
+
ActiveRecord::Base.establish_connection(database_configurations["test"])
|
data/fresh_rakefile
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
|
5
|
+
$VERBOSE = nil
|
6
|
+
|
7
|
+
require File.dirname(__FILE__) + '/config/environments/production'
|
8
|
+
require 'code_statistics'
|
9
|
+
|
10
|
+
desc "Run all the tests on a fresh test database"
|
11
|
+
task :default => [ :clone_production_structure_to_test, :test_units, :test_functional ]
|
12
|
+
|
13
|
+
desc "Generate API documentatio, show coding stats"
|
14
|
+
task :doc => [ :appdoc, :stats ]
|
15
|
+
|
16
|
+
|
17
|
+
desc "Run the unit tests in test/unit"
|
18
|
+
Rake::TestTask.new("test_units") { |t|
|
19
|
+
t.libs << "test"
|
20
|
+
t.pattern = 'test/unit/*_test.rb'
|
21
|
+
t.verbose = true
|
22
|
+
}
|
23
|
+
|
24
|
+
desc "Run the functional tests in test/functional"
|
25
|
+
Rake::TestTask.new("test_functional") { |t|
|
26
|
+
t.libs << "test"
|
27
|
+
t.pattern = 'test/functional/*_test.rb'
|
28
|
+
t.verbose = true
|
29
|
+
}
|
30
|
+
|
31
|
+
desc "Generate documentation for the application"
|
32
|
+
Rake::RDocTask.new("appdoc") { |rdoc|
|
33
|
+
rdoc.rdoc_dir = 'doc/app'
|
34
|
+
rdoc.title = "Rails Application Documentation"
|
35
|
+
rdoc.options << '--line-numbers --inline-source'
|
36
|
+
rdoc.rdoc_files.include('doc/README_FOR_APP')
|
37
|
+
rdoc.rdoc_files.include('app/**/*.rb')
|
38
|
+
}
|
39
|
+
|
40
|
+
desc "Generate documentation for the Rails framework"
|
41
|
+
Rake::RDocTask.new("apidoc") { |rdoc|
|
42
|
+
rdoc.rdoc_dir = 'doc/api'
|
43
|
+
rdoc.title = "Rails Framework Documentation"
|
44
|
+
rdoc.options << '--line-numbers --inline-source'
|
45
|
+
rdoc.rdoc_files.include('README')
|
46
|
+
rdoc.rdoc_files.include('vendor/railties/CHANGELOG')
|
47
|
+
rdoc.rdoc_files.include('vendor/railties/MIT-LICENSE')
|
48
|
+
rdoc.rdoc_files.include('vendor/activerecord/README')
|
49
|
+
rdoc.rdoc_files.include('vendor/activerecord/CHANGELOG')
|
50
|
+
rdoc.rdoc_files.include('vendor/activerecord/lib/active_record/**/*.rb')
|
51
|
+
rdoc.rdoc_files.exclude('vendor/activerecord/lib/active_record/vendor/*')
|
52
|
+
rdoc.rdoc_files.include('vendor/actionpack/README')
|
53
|
+
rdoc.rdoc_files.include('vendor/actionpack/CHANGELOG')
|
54
|
+
rdoc.rdoc_files.include('vendor/actionpack/lib/action_controller/**/*.rb')
|
55
|
+
rdoc.rdoc_files.include('vendor/actionpack/lib/action_view/**/*.rb')
|
56
|
+
}
|
57
|
+
|
58
|
+
desc "Report code statistics (KLOCs, etc) from the application"
|
59
|
+
task :stats do
|
60
|
+
CodeStatistics.new(
|
61
|
+
["Controllers", "app/controllers"],
|
62
|
+
["Helpers", "app/helpers"],
|
63
|
+
["Models", "app/models"],
|
64
|
+
["Units", "test/unit"],
|
65
|
+
["Functionals", "test/functional"]
|
66
|
+
).to_s
|
67
|
+
end
|
68
|
+
|
69
|
+
desc "Recreate the test databases from the production structure"
|
70
|
+
task :clone_production_structure_to_test => [ :db_structure_dump, :purge_test_database ] do
|
71
|
+
if database_configurations["test"]["adapter"] == "mysql"
|
72
|
+
ActiveRecord::Base.establish_connection(database_configurations["test"])
|
73
|
+
ActiveRecord::Base.connection.execute('SET foreign_key_checks = 0')
|
74
|
+
IO.readlines("db/production_structure.sql").join.split("\n\n").each do |table|
|
75
|
+
ActiveRecord::Base.connection.execute(table)
|
76
|
+
end
|
77
|
+
elsif database_configurations["test"]["adapter"] == "postgresql"
|
78
|
+
`psql -U #{database_configurations["test"]["username"]} -f db/production_structure.sql #{database_configurations["test"]["database"]}`
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
desc "Dump the database structure to a SQL file"
|
83
|
+
task :db_structure_dump do
|
84
|
+
if database_configurations["test"]["adapter"] == "mysql"
|
85
|
+
ActiveRecord::Base.establish_connection(database_configurations["production"])
|
86
|
+
File.open("db/production_structure.sql", "w+") { |f| f << ActiveRecord::Base.connection.structure_dump }
|
87
|
+
elsif database_configurations["test"]["adapter"] == "postgresql"
|
88
|
+
`pg_dump -U #{database_configurations["test"]["username"]} -s -f db/production_structure.sql #{database_configurations["production"]["database"]}`
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
desc "Drop the test database and bring it back again"
|
93
|
+
task :purge_test_database do
|
94
|
+
if database_configurations["test"]["adapter"] == "mysql"
|
95
|
+
ActiveRecord::Base.establish_connection(database_configurations["production"])
|
96
|
+
ActiveRecord::Base.connection.recreate_database(database_configurations["test"]["database"])
|
97
|
+
elsif database_configurations["test"]["adapter"] == "postgresql"
|
98
|
+
`dropdb -U #{database_configurations["test"]["username"]} #{database_configurations["test"]["database"]}`
|
99
|
+
`createdb -U #{database_configurations["test"]["username"]} #{database_configurations["test"]["database"]}`
|
100
|
+
end
|
101
|
+
end
|
@@ -0,0 +1,118 @@
|
|
1
|
+
#!/usr/local/bin/ruby
|
2
|
+
|
3
|
+
require File.dirname(__FILE__) + '/../config/environments/production'
|
4
|
+
|
5
|
+
def create_controller_class(controller_class_name, controller_file_name, show_actions)
|
6
|
+
File.open("app/controllers/" + controller_file_name + "_controller.rb", "w", 0777) do |controller_file|
|
7
|
+
controller_file.write <<EOF
|
8
|
+
require 'abstract_application'
|
9
|
+
require '#{controller_file_name}_helper'
|
10
|
+
|
11
|
+
class #{controller_class_name}Controller < AbstractApplicationController
|
12
|
+
include #{controller_class_name}Helper
|
13
|
+
|
14
|
+
#{show_actions.collect { |action| " def #{action}\n end" }.join "\n\n" }
|
15
|
+
end
|
16
|
+
EOF
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def create_helper_class(controller_class_name, controller_file_name)
|
21
|
+
File.open("app/helpers/" + controller_file_name + "_helper.rb", "w", 0777) do |helper_file|
|
22
|
+
helper_file.write <<EOF
|
23
|
+
module #{controller_class_name}Helper
|
24
|
+
def self.append_features(controller) #:nodoc:
|
25
|
+
controller.ancestors.include?(ActionController::Base) ? controller.add_template_helper(self) : super
|
26
|
+
end
|
27
|
+
end
|
28
|
+
EOF
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def create_templates(controller_class_name, controller_file_name, show_actions)
|
33
|
+
Dir.mkdir("app/views/#{controller_file_name}") rescue nil
|
34
|
+
show_actions.each { |action| File.open("app/views/#{controller_file_name}/#{action}.rhtml", "w", 0777) do |template_file|
|
35
|
+
template_file.write <<EOF
|
36
|
+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
|
37
|
+
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
38
|
+
<head>
|
39
|
+
<title>#{controller_class_name}##{action}</title>
|
40
|
+
</head>
|
41
|
+
<body>
|
42
|
+
<h1>#{controller_class_name}##{action}</h1>
|
43
|
+
<p>Find me in app/views/#{controller_file_name}/#{action}.rhtml</p>
|
44
|
+
</body>
|
45
|
+
</html>
|
46
|
+
EOF
|
47
|
+
end }
|
48
|
+
end
|
49
|
+
|
50
|
+
def create_test_class(controller_class_name, controller_file_name)
|
51
|
+
File.open("test/functional/" + controller_file_name + "_controller_test.rb", "w", 0777) do |test_file|
|
52
|
+
test_file.write <<EOF
|
53
|
+
require File.dirname(__FILE__) + '/../test_helper'
|
54
|
+
require '#{controller_file_name}_controller'
|
55
|
+
|
56
|
+
# Raise errors beyond the default web-based presentation
|
57
|
+
class #{controller_class_name}Controller; def rescue_action(e) raise e end; end
|
58
|
+
|
59
|
+
class #{controller_class_name}ControllerTest < Test::Unit::TestCase
|
60
|
+
def setup
|
61
|
+
@controller = #{controller_class_name}Controller.new
|
62
|
+
@request, @response = ActionController::TestRequest.new, ActionController::TestResponse.new
|
63
|
+
end
|
64
|
+
|
65
|
+
def test_truth
|
66
|
+
assert true, "Test implementation missing"
|
67
|
+
end
|
68
|
+
end
|
69
|
+
EOF
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
|
74
|
+
if !ARGV.empty?
|
75
|
+
controller_name = ARGV[0]
|
76
|
+
show_actions = ARGV[1..-1]
|
77
|
+
|
78
|
+
controller_class_name = Inflector.camelize(controller_name)
|
79
|
+
controller_file_name = Inflector.underscore(controller_name)
|
80
|
+
|
81
|
+
create_controller_class(controller_class_name, controller_file_name, show_actions)
|
82
|
+
create_helper_class(controller_class_name, controller_file_name)
|
83
|
+
create_templates(controller_class_name, controller_file_name, show_actions)
|
84
|
+
create_test_class(controller_class_name, controller_file_name)
|
85
|
+
else
|
86
|
+
puts <<-END_HELP
|
87
|
+
|
88
|
+
NAME
|
89
|
+
new_controller - create controller and view stub files
|
90
|
+
|
91
|
+
SYNOPSIS
|
92
|
+
new_controller [controller_name] [view_name ...]
|
93
|
+
|
94
|
+
DESCRIPTION
|
95
|
+
The new_controller generator takes the name of the new controller as the
|
96
|
+
first argument and a variable number of view names as subsequent arguments.
|
97
|
+
The controller name should be supplied without a "Controller" suffix. The
|
98
|
+
generator will add that itself.
|
99
|
+
|
100
|
+
From the passed arguments, new_controller generates a controller file in
|
101
|
+
app/controllers with a render action for each of the view names passed.
|
102
|
+
It then creates a controller test suite in test/functional with one failing
|
103
|
+
test case. Finally, it creates an HTML stub for each of the view names in
|
104
|
+
app/views under a directory with the same name as the controller.
|
105
|
+
|
106
|
+
EXAMPLE
|
107
|
+
new_controller Blog list display new edit
|
108
|
+
|
109
|
+
This will generate a BlogController class in
|
110
|
+
app/controllers/blog_controller.rb, a BlogHelper class in
|
111
|
+
app/helpers/blog_helper.rb and a BlogControllerTest in
|
112
|
+
test/functional/blog_controller_test.rb. It will also create list.rhtml,
|
113
|
+
display.rhtml, new.rhtml, and edit.rhtml in app/views/blog.
|
114
|
+
|
115
|
+
The BlogController class will have the following methods: list, display, new, edit.
|
116
|
+
Each will default to render the associated template file.
|
117
|
+
END_HELP
|
118
|
+
end
|