From 2594581e9f5594b32918326be895b4d443ab3e9c Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Tue, 7 Dec 2004 11:49:38 +0000 Subject: Added a better generator for scaffolding that actually creates the code, so it can be edited bit by bit. See "script/generate scaffold" [bitsweat]. Added a whole new approach to generators that used the shared "script/generate" command. Run with no-args to see help [bitsweat]. git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@63 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- railties/generators/controller/USAGE | 29 ++++++++++++++++++++++ .../generators/controller/controller_generator.rb | 22 ++++++++++++++++ .../generators/controller/templates/controller.rb | 10 ++++++++ .../controller/templates/functional_test.rb | 17 +++++++++++++ railties/generators/controller/templates/helper.rb | 2 ++ .../generators/controller/templates/view.rhtml | 2 ++ 6 files changed, 82 insertions(+) create mode 100644 railties/generators/controller/USAGE create mode 100644 railties/generators/controller/controller_generator.rb create mode 100644 railties/generators/controller/templates/controller.rb create mode 100644 railties/generators/controller/templates/functional_test.rb create mode 100644 railties/generators/controller/templates/helper.rb create mode 100644 railties/generators/controller/templates/view.rhtml (limited to 'railties/generators/controller') diff --git a/railties/generators/controller/USAGE b/railties/generators/controller/USAGE new file mode 100644 index 0000000000..0259b3d027 --- /dev/null +++ b/railties/generators/controller/USAGE @@ -0,0 +1,29 @@ +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. diff --git a/railties/generators/controller/controller_generator.rb b/railties/generators/controller/controller_generator.rb new file mode 100644 index 0000000000..4b53741565 --- /dev/null +++ b/railties/generators/controller/controller_generator.rb @@ -0,0 +1,22 @@ +require 'rails_generator' + +class ControllerGenerator < Rails::Generator::Base + attr_reader :actions + + def generate + @actions = args + + # Controller class, functional test, and helper class. + template "controller.rb", "app/controllers/#{file_name}_controller.rb" + template "functional_test.rb", "test/functional/#{file_name}_controller_test.rb" + template "helper.rb", "app/helpers/#{file_name}_helper.rb" + + # Create the views directory even if there are no actions. + FileUtils.mkdir_p "app/views/#{file_name}" + + # Create a view for each action. + actions.each do |action| + template "view.rhtml", "app/views/#{file_name}/#{action}.rhtml", binding + end + end +end diff --git a/railties/generators/controller/templates/controller.rb b/railties/generators/controller/templates/controller.rb new file mode 100644 index 0000000000..f9800ab556 --- /dev/null +++ b/railties/generators/controller/templates/controller.rb @@ -0,0 +1,10 @@ +class <%= class_name %>Controller < AbstractApplicationController +<% if options[:scaffold] -%> + scaffold :<%= singular_name %> +<% end -%> +<% for action in actions -%> + + def <%= action %> + end +<% end -%> +end diff --git a/railties/generators/controller/templates/functional_test.rb b/railties/generators/controller/templates/functional_test.rb new file mode 100644 index 0000000000..c975cb3ce3 --- /dev/null +++ b/railties/generators/controller/templates/functional_test.rb @@ -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/controller/templates/helper.rb b/railties/generators/controller/templates/helper.rb new file mode 100644 index 0000000000..3fe2ecdc74 --- /dev/null +++ b/railties/generators/controller/templates/helper.rb @@ -0,0 +1,2 @@ +module <%= class_name %>Helper +end diff --git a/railties/generators/controller/templates/view.rhtml b/railties/generators/controller/templates/view.rhtml new file mode 100644 index 0000000000..7e7a7d53ce --- /dev/null +++ b/railties/generators/controller/templates/view.rhtml @@ -0,0 +1,2 @@ +

<%= class_name %>#<%= action %>

+

Find me in app/views/<%= file_name %>/<%= action %>.rhtml

-- cgit v1.2.3