aboutsummaryrefslogtreecommitdiffstats
path: root/railties/generators
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2004-11-24 01:04:44 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2004-11-24 01:04:44 +0000
commitdb045dbbf60b53dbe013ef25554fd013baf88134 (patch)
tree257830e3c76458c8ff3d1329de83f32b23926028 /railties/generators
downloadrails-db045dbbf60b53dbe013ef25554fd013baf88134.tar.gz
rails-db045dbbf60b53dbe013ef25554fd013baf88134.tar.bz2
rails-db045dbbf60b53dbe013ef25554fd013baf88134.zip
Initial
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'railties/generators')
-rwxr-xr-xrailties/generators/new_controller.rb43
-rwxr-xr-xrailties/generators/new_crud.rb34
-rw-r--r--railties/generators/new_mailer.rb43
-rwxr-xr-xrailties/generators/new_model.rb31
-rw-r--r--railties/generators/templates/controller.erb19
-rw-r--r--railties/generators/templates/controller_test.erb17
-rw-r--r--railties/generators/templates/controller_view.rhtml10
-rw-r--r--railties/generators/templates/helper.erb2
-rw-r--r--railties/generators/templates/mailer.erb15
-rw-r--r--railties/generators/templates/mailer_action.rhtml3
-rw-r--r--railties/generators/templates/mailer_fixture.rhtml4
-rw-r--r--railties/generators/templates/mailer_test.erb37
-rw-r--r--railties/generators/templates/model.erb2
-rw-r--r--railties/generators/templates/model_test.erb11
14 files changed, 271 insertions, 0 deletions
diff --git a/railties/generators/new_controller.rb b/railties/generators/new_controller.rb
new file mode 100755
index 0000000000..3060c06382
--- /dev/null
+++ b/railties/generators/new_controller.rb
@@ -0,0 +1,43 @@
+#!/usr/local/bin/ruby
+require File.dirname(__FILE__) + '/../config/environment'
+require 'generator'
+
+unless ARGV.empty?
+ rails_root = File.dirname(__FILE__) + '/..'
+ name = ARGV.shift
+ actions = ARGV
+ Generator::Controller.new(rails_root, name, actions).generate
+else
+ puts <<-END_HELP
+
+NAME
+ new_controller - create controller and view stub files
+
+SYNOPSIS
+ new_controller ControllerName action [action ...]
+
+DESCRIPTION
+ The new_controller generator takes the name of the new controller as the
+ first argument and a variable number of view names as subsequent arguments.
+ The controller name should be supplied without a "Controller" suffix. The
+ generator will add that itself.
+
+ From the passed arguments, new_controller generates a controller file in
+ app/controllers with a render action for each of the view names passed.
+ It then creates a controller test suite in test/functional with one failing
+ test case. Finally, it creates an HTML stub for each of the view names in
+ app/views under a directory with the same name as the controller.
+
+EXAMPLE
+ new_controller Blog list display new edit
+
+ This will generate a BlogController class in
+ app/controllers/blog_controller.rb, a BlogHelper class in
+ app/helpers/blog_helper.rb and a BlogControllerTest in
+ test/functional/blog_controller_test.rb. It will also create list.rhtml,
+ display.rhtml, new.rhtml, and edit.rhtml in app/views/blog.
+
+ The BlogController class will have the following methods: list, display, new, edit.
+ Each will default to render the associated template file.
+END_HELP
+end
diff --git a/railties/generators/new_crud.rb b/railties/generators/new_crud.rb
new file mode 100755
index 0000000000..4eaa1cb1f3
--- /dev/null
+++ b/railties/generators/new_crud.rb
@@ -0,0 +1,34 @@
+#!/usr/local/bin/ruby
+require File.dirname(__FILE__) + '/../config/environment'
+require 'generator'
+
+unless ARGV.empty?
+ rails_root = File.dirname(__FILE__) + '/..'
+ name = ARGV.shift
+ actions = ARGV
+ Generator::Model.new(rails_root, name).generate
+ Generator::Controller.new(rails_root, name, actions, :scaffold => true).generate
+else
+ puts <<-END_HELP
+
+NAME
+ new_crud - create a model and a controller scaffold
+
+SYNOPSIS
+ new_crud ModelName [action ...]
+
+DESCRIPTION
+ The new_crud generator takes the name of the new model as the
+ first argument and an optional list of controller actions as the
+ subsequent arguments. All actions may be omitted since the controller
+ will have scaffolding automatically set up for this model.
+
+EXAMPLE
+ new_crud Account
+
+ This will generate an Account model and controller with scaffolding.
+ Now create the accounts table in your database and browse to
+ http://localhost/account/ -- voila, you're on Rails!
+
+END_HELP
+end
diff --git a/railties/generators/new_mailer.rb b/railties/generators/new_mailer.rb
new file mode 100644
index 0000000000..05d0c9ae82
--- /dev/null
+++ b/railties/generators/new_mailer.rb
@@ -0,0 +1,43 @@
+#!/usr/local/bin/ruby
+require File.dirname(__FILE__) + '/../config/environment'
+require 'generator'
+
+unless ARGV.empty?
+ rails_root = File.dirname(__FILE__) + '/..'
+ name = ARGV.shift
+ actions = ARGV
+ Generator::Mailer.new(rails_root, name, actions).generate
+else
+ puts <<-END_HELP
+
+NAME
+ new_mailer - create mailer and view stub files
+
+SYNOPSIS
+ new_mailer MailerName action [action ...]
+
+DESCRIPTION
+ The new_mailer generator takes the name of the new mailer class as the
+ first argument and a variable number of mail action names as subsequent
+ arguments.
+
+ From the passed arguments, new_mailer generates a class file in
+ app/models with a mail action for each of the mail action names passed.
+ It then creates a mail test suite in test/unit with one stub test case
+ and one stub fixture per mail action. Finally, it creates a template stub
+ for each of the mail action names in app/views under a directory with the
+ same name as the class.
+
+EXAMPLE
+ new_mailer Notifications signup forgot_password invoice
+
+ This will generate a Notifications class in
+ app/models/notifications.rb, a NotificationsTest in
+ test/unit/notifications_test.rb, and signup, forgot_password, and invoice
+ in test/fixture/notification. It will also create signup.rhtml,
+ forgot_password.rhtml, and invoice.rhtml in app/views/notifications.
+
+ The Notifications class will have the following methods: signup,
+ forgot_password, and invoice.
+END_HELP
+end
diff --git a/railties/generators/new_model.rb b/railties/generators/new_model.rb
new file mode 100755
index 0000000000..f6fbf5f002
--- /dev/null
+++ b/railties/generators/new_model.rb
@@ -0,0 +1,31 @@
+#!/usr/local/bin/ruby
+require File.dirname(__FILE__) + '/../config/environment'
+require 'generator'
+
+if ARGV.size == 1
+ rails_root = File.dirname(__FILE__) + '/..'
+ name = ARGV.shift
+ Generator::Model.new(rails_root, name).generate
+else
+ puts <<-HELP
+
+NAME
+ new_model - create model stub files
+
+SYNOPSIS
+ new_model ModelName
+
+DESCRIPTION
+ The new_model generator takes a model name (in CamelCase) and generates
+ a new, empty model in app/models, a test suite in test/unit with one
+ failing test case, and a fixtures directory in test/fixtures.
+
+EXAMPLE
+ new_model Account
+
+ This will generate an Account class in app/models/account.rb, an
+ AccountTest in test/unit/account_test.rb, and the directory
+ test/fixtures/account.
+
+HELP
+end
diff --git a/railties/generators/templates/controller.erb b/railties/generators/templates/controller.erb
new file mode 100644
index 0000000000..600f5d2c59
--- /dev/null
+++ b/railties/generators/templates/controller.erb
@@ -0,0 +1,19 @@
+class <%= class_name %>Controller < AbstractApplicationController
+ helper :<%= file_name %>
+<% if options[:scaffold] -%>
+ model :<%= file_name %>
+ scaffold :<%= options[:scaffold] %>
+
+ <%- for action in actions -%>
+ #def <%= action %>
+ #end
+
+ <%- end -%>
+<% else -%>
+ <%- for action in actions -%>
+ def <%= action %>
+ end
+
+ <%- end -%>
+<% end -%>
+end
diff --git a/railties/generators/templates/controller_test.erb b/railties/generators/templates/controller_test.erb
new file mode 100644
index 0000000000..5577379c62
--- /dev/null
+++ b/railties/generators/templates/controller_test.erb
@@ -0,0 +1,17 @@
+require File.dirname(__FILE__) + '/../test_helper'
+require '<%= file_name %>_controller'
+
+# Re-raise errors caught by the controller.
+class <%= class_name %>Controller; def rescue_action(e) raise e end; end
+
+class <%= class_name %>ControllerTest < Test::Unit::TestCase
+ def setup
+ @controller = <%= class_name %>Controller.new
+ @request, @response = ActionController::TestRequest.new, ActionController::TestResponse.new
+ end
+
+ # Replace this with your real tests
+ def test_truth
+ assert true
+ end
+end
diff --git a/railties/generators/templates/controller_view.rhtml b/railties/generators/templates/controller_view.rhtml
new file mode 100644
index 0000000000..d8a310df50
--- /dev/null
+++ b/railties/generators/templates/controller_view.rhtml
@@ -0,0 +1,10 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
+<head>
+ <title><%= class_name %>#<%= action %></title>
+</head>
+<body>
+<h1><%= class_name %>#<%= action %></h1>
+<p>Find me in app/views/<%= file_name %>/<%= action %>.rhtml</p>
+</body>
+</html>
diff --git a/railties/generators/templates/helper.erb b/railties/generators/templates/helper.erb
new file mode 100644
index 0000000000..3fe2ecdc74
--- /dev/null
+++ b/railties/generators/templates/helper.erb
@@ -0,0 +1,2 @@
+module <%= class_name %>Helper
+end
diff --git a/railties/generators/templates/mailer.erb b/railties/generators/templates/mailer.erb
new file mode 100644
index 0000000000..5afc254923
--- /dev/null
+++ b/railties/generators/templates/mailer.erb
@@ -0,0 +1,15 @@
+require 'action_mailer'
+
+class <%= class_name %> < ActionMailer::Base
+
+<% for action in actions -%>
+ def <%= action %>(sent_on = Time.now)
+ @recipients = ''
+ @from = ''
+ @subject = ''
+ @body = {}
+ @sent_on = sent_on
+ end
+
+<% end -%>
+end
diff --git a/railties/generators/templates/mailer_action.rhtml b/railties/generators/templates/mailer_action.rhtml
new file mode 100644
index 0000000000..b481906829
--- /dev/null
+++ b/railties/generators/templates/mailer_action.rhtml
@@ -0,0 +1,3 @@
+<%= class_name %>#<%= action %>
+
+Find me in app/views/<%= file_name %>/<%= action %>.rhtml
diff --git a/railties/generators/templates/mailer_fixture.rhtml b/railties/generators/templates/mailer_fixture.rhtml
new file mode 100644
index 0000000000..f315d430ed
--- /dev/null
+++ b/railties/generators/templates/mailer_fixture.rhtml
@@ -0,0 +1,4 @@
+<%= class_name %>#<%= action %>
+
+Find me in test/fixtures/<%= file_name %>/<%= action %>.
+I'm tested against the view in app/views/<%= file_name %>/<%= action %>.
diff --git a/railties/generators/templates/mailer_test.erb b/railties/generators/templates/mailer_test.erb
new file mode 100644
index 0000000000..f17d614195
--- /dev/null
+++ b/railties/generators/templates/mailer_test.erb
@@ -0,0 +1,37 @@
+require File.dirname(__FILE__) + '/../test_helper'
+require '<%= file_name %>'
+
+class <%= class_name %>Test < Test::Unit::TestCase
+ FIXTURES_PATH = File.dirname(__FILE__) + '/../fixtures'
+
+ def setup
+ ActionMailer::Base.delivery_method = :test
+ ActionMailer::Base.perform_deliveries = true
+ ActionMailer::Base.deliveries = []
+
+ @expected = TMail::Mail.new
+ @expected.to = 'test@localhost'
+ @expected.from = 'test@localhost'
+ @expected.subject = '<%= class_name %> test mail'
+ end
+
+<% for action in actions -%>
+ def test_<%= action %>
+ @expected.body = read_fixture('<%= action %>')
+ @expected.date = Time.now
+
+ created = nil
+ assert_nothing_raised { created = <%= class_name %>.create_<%= action %>(@expected.date) }
+ assert_not_nil created
+ assert_equal @expected.encoded, created.encoded
+
+ assert_nothing_raised { <%= class_name %>.deliver_<%= action %>(@expected.date) }
+ assert_equal @expected.encoded, ActionMailer::Base.deliveries.first.encoded
+ end
+
+<% end -%>
+ private
+ def read_fixture(action)
+ IO.readlines("#{FIXTURES_PATH}/<%= file_name %>/#{action}")
+ end
+end
diff --git a/railties/generators/templates/model.erb b/railties/generators/templates/model.erb
new file mode 100644
index 0000000000..8d4c89e912
--- /dev/null
+++ b/railties/generators/templates/model.erb
@@ -0,0 +1,2 @@
+class <%= class_name %> < ActiveRecord::Base
+end
diff --git a/railties/generators/templates/model_test.erb b/railties/generators/templates/model_test.erb
new file mode 100644
index 0000000000..a3ad2b72fb
--- /dev/null
+++ b/railties/generators/templates/model_test.erb
@@ -0,0 +1,11 @@
+require File.dirname(__FILE__) + '/../test_helper'
+require '<%= file_name %>'
+
+class <%= class_name %>Test < Test::Unit::TestCase
+ fixtures :<%= table_name %>
+
+ # Replace this with your real tests
+ def test_truth
+ assert true
+ end
+end \ No newline at end of file