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/scaffold/USAGE | 18 ++++ railties/generators/scaffold/scaffold_generator.rb | 53 ++++++++++ .../generators/scaffold/templates/controller.rb | 58 +++++++++++ .../generators/scaffold/templates/fixtures.yml | 7 ++ .../scaffold/templates/functional_test.rb | 109 +++++++++++++++++++++ .../generators/scaffold/templates/layout.rhtml | 11 +++ railties/generators/scaffold/templates/style.css | 17 ++++ .../generators/scaffold/templates/view_edit.rhtml | 6 ++ .../generators/scaffold/templates/view_list.rhtml | 24 +++++ .../generators/scaffold/templates/view_new.rhtml | 5 + .../generators/scaffold/templates/view_show.rhtml | 8 ++ 11 files changed, 316 insertions(+) create mode 100644 railties/generators/scaffold/USAGE create mode 100644 railties/generators/scaffold/scaffold_generator.rb create mode 100644 railties/generators/scaffold/templates/controller.rb create mode 100644 railties/generators/scaffold/templates/fixtures.yml create mode 100644 railties/generators/scaffold/templates/functional_test.rb create mode 100644 railties/generators/scaffold/templates/layout.rhtml create mode 100644 railties/generators/scaffold/templates/style.css create mode 100644 railties/generators/scaffold/templates/view_edit.rhtml create mode 100644 railties/generators/scaffold/templates/view_list.rhtml create mode 100644 railties/generators/scaffold/templates/view_new.rhtml create mode 100644 railties/generators/scaffold/templates/view_show.rhtml (limited to 'railties/generators/scaffold') diff --git a/railties/generators/scaffold/USAGE b/railties/generators/scaffold/USAGE new file mode 100644 index 0000000000..f299ee6f06 --- /dev/null +++ b/railties/generators/scaffold/USAGE @@ -0,0 +1,18 @@ +NAME + new_scaffold - create a model and a skeleton controller + +SYNOPSIS + new_scaffold ModelName [action ...] + +DESCRIPTION + The new_scaffold generator takes the name of the new model as the + first argument and an optional list of controller actions as the + subsequent arguments. Any actions with scaffolding code available + will be generated in your controller; others will be left as stubs. + +EXAMPLE + new_scaffold Account + + This will generate an Account model and controller. + Now create the accounts table in your database and browse to + http://localhost/account/ -- voila, you're on Rails! diff --git a/railties/generators/scaffold/scaffold_generator.rb b/railties/generators/scaffold/scaffold_generator.rb new file mode 100644 index 0000000000..ee556e8a03 --- /dev/null +++ b/railties/generators/scaffold/scaffold_generator.rb @@ -0,0 +1,53 @@ +require 'rails_generator' + +class ScaffoldGenerator < Rails::Generator::Base + def generate + # Model. + generator('model').generate + + # Fixtures. + template "fixtures.yml", "test/fixtures/#{table_name}.yml" + + # Controller class, functional test, helper, and views. + template "controller.rb", "app/controllers/#{file_name}_controller.rb" + template "functional_test.rb", "test/functional/#{file_name}_controller_test.rb" + template "controller/helper.rb", "app/helpers/#{file_name}_helper.rb" + + # Layout and stylesheet. + unless File.file?("app/views/layouts/scaffold.rhtml") + template "layout.rhtml", "app/views/layouts/scaffold.rhtml" + end + unless File.file?("public/stylesheets/scaffold.css") + template "style.css", "public/stylesheets/scaffold.css" + end + + # Scaffolded views. + scaffold_views.each do |action| + template "view_#{action}.rhtml", "app/views/#{file_name}/#{action}.rhtml" + end + + # Unscaffolded views. + unscaffolded_actions.each do |action| + template "controller/view.rhtml", + "app/views/#{file_name}/#{action}.rhtml", + binding + end + end + + protected + def scaffold_views + %w(list show new edit) + end + + def scaffold_actions + scaffold_views + %w(index create update destroy) + end + + def unscaffolded_actions + args - scaffold_actions + end + + def suffix + "_#{singular_name}" if options[:suffix] + end +end diff --git a/railties/generators/scaffold/templates/controller.rb b/railties/generators/scaffold/templates/controller.rb new file mode 100644 index 0000000000..c8706098da --- /dev/null +++ b/railties/generators/scaffold/templates/controller.rb @@ -0,0 +1,58 @@ +class <%= class_name %>Controller < AbstractApplicationController + model :<%= singular_name %> + layout 'scaffold' + +<% unless suffix -%> + def index + list + render_action 'list' + end +<% end -%> + +<% for action in unscaffolded_actions -%> + def <%= action %><%= suffix %> + end + +<% end -%> + def list<%= suffix %> + @<%= plural_name %> = <%= class_name %>.find_all + end + + def show<%= suffix %> + @<%= singular_name %> = <%= class_name %>.find(@params['id']) + end + + def new<%= suffix %> + @<%= singular_name %> = <%= class_name %>.new + end + + def create<%= suffix %> + @<%= singular_name %> = <%= class_name %>.new(@params['<%= singular_name %>']) + if @<%= singular_name %>.save + flash['notice'] = '<%= class_name %> was successfully created.' + redirect_to :action => 'list<%= suffix %>' + else + render_action 'new<%= suffix %>' + end + end + + def edit<%= suffix %> + @<%= singular_name %> = <%= class_name %>.find(@params['id']) + end + + def update + @<%= singular_name %> = <%= class_name %>.find(@params['<%= singular_name %>']['id']) + @<%= singular_name %>.attributes = @params['<%= singular_name %>'] + if @<%= singular_name %>.save + flash['notice'] = '<%= class_name %> was successfully updated.' + redirect_to :action => 'show<%= suffix %>', :id => @<%= singular_name %>.id + else + render_action 'edit<%= suffix %>' + end + end + + def destroy<%= suffix %> + <%= class_name %>.find(@params['id']).destroy + redirect_to :action => 'list<%= suffix %>' + end +end diff --git a/railties/generators/scaffold/templates/fixtures.yml b/railties/generators/scaffold/templates/fixtures.yml new file mode 100644 index 0000000000..ea67f36ad8 --- /dev/null +++ b/railties/generators/scaffold/templates/fixtures.yml @@ -0,0 +1,7 @@ +# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html + +first_<%= singular_name %>: + id: 1 + +another_<%= singular_name %>: + id: 2 diff --git a/railties/generators/scaffold/templates/functional_test.rb b/railties/generators/scaffold/templates/functional_test.rb new file mode 100644 index 0000000000..e82349375b --- /dev/null +++ b/railties/generators/scaffold/templates/functional_test.rb @@ -0,0 +1,109 @@ +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 + fixtures :<%= table_name %> + + def setup + @controller = <%= class_name %>Controller.new + @request, @response = ActionController::TestRequest.new, ActionController::TestResponse.new + end + +<% for action in unscaffolded_actions -%> + def test_<%= action %> + process :<%= action %> + assert_success + assert_rendered_file '<%= action %>' + end + +<% end -%> +<% unless suffix -%> + def test_index + process :index + assert_success + assert_rendered_file 'list' + end + +<% end -%> + def test_list<%= suffix %> + process :list<%= suffix %> + assert_success + assert_rendered_file 'list<%= suffix %>' + assert_template_has '<%= plural_name %>' + end + + def test_show<%= suffix %> + process :show<%= suffix %>, 'id' => 1 + assert_success + assert_rendered_file 'show' + assert_template_has '<%= singular_name %>' + assert_valid_record '<%= singular_name %>' + end + + def test_show_missing_<%= suffix || 'record' %> + process :show<%= suffix %> + assert_success + assert_rendered_file 'error' + end + + def test_new<%= suffix %> + process :new<%= suffix %> + assert_success + assert_rendered_file 'new<%= suffix %>' + assert_template_has '<%= singular_name %>' + end + + def test_create + num_<%= plural_name %> = <%= class_name %>.find_all.size + + process :create<%= suffix %>, '<%= singular_name %>' => { } + assert_redirected_to :action => 'list<%= suffix %>' + + assert_equal num_<%= plural_name %> + 1, <%= class_name %>.find_all.size + end + + def test_edit<%= suffix %> + process :edit<%= suffix %>, 'id' => 1 + assert_success + assert_rendered_file 'edit<%= suffix %>' + assert_template_has '<%= singular_name %>' + assert_valid_record '<%= singular_name %>' + end + + def test_edit_missing_<%= suffix || 'record' %> + process :edit<%= suffix %> + assert_success + assert_rendered_file 'error' + end + + def test_update<%= suffix %> + process :update<%= suffix %>, 'id' => 1 + assert_redirected_to :action => 'show<%= suffix %>', :id => 1 + end + + def test_update_missing_<%= suffix || 'record' %> + process :update<%= suffix %>, '<%= singular_name %>' => {} + assert_success + assert_rendered_file 'error' + end + + def test_destroy<%= suffix %> + assert_not_nil <%= class_name %>.find(1) + + process :destroy, 'id' => 1 + assert_redirected_to :action => 'list<%= suffix %>' + + assert_raise(ActiveRecord::RecordNotFound) { + <%= singular_name %> = <%= class_name %>.find(1) + } + end + + def test_destroy_missing_<%= suffix || 'record' %> + process :destroy<%= suffix %> + assert_success + assert_rendered_file 'error' + end +end diff --git a/railties/generators/scaffold/templates/layout.rhtml b/railties/generators/scaffold/templates/layout.rhtml new file mode 100644 index 0000000000..59d5585b51 --- /dev/null +++ b/railties/generators/scaffold/templates/layout.rhtml @@ -0,0 +1,11 @@ + + + Scaffolding: <%%= controller.controller_name %>#<%%= controller.action_name %> + + + + +<%%= @content_for_layout %> + + + diff --git a/railties/generators/scaffold/templates/style.css b/railties/generators/scaffold/templates/style.css new file mode 100644 index 0000000000..2db43b7fa1 --- /dev/null +++ b/railties/generators/scaffold/templates/style.css @@ -0,0 +1,17 @@ +body { background-color: #fff; color: #333; } + +body, p, ol, ul, td { + font-family: verdana, arial, helvetica, sans-serif; + font-size: 13px; + line-height: 18px; +} + +pre { + background-color: #eee; + padding: 10px; + font-size: 11px; +} + +a { color: #000; } +a:visited { color: #666; } +a:hover { color: #fff; background-color:#000; } diff --git a/railties/generators/scaffold/templates/view_edit.rhtml b/railties/generators/scaffold/templates/view_edit.rhtml new file mode 100644 index 0000000000..fec09e2cbe --- /dev/null +++ b/railties/generators/scaffold/templates/view_edit.rhtml @@ -0,0 +1,6 @@ +

Editing <%= singular_name %>

+ +<%%= form '<%= singular_name %>', :action => 'update<%= suffix %>' %> + +<%%= link_to 'Show', :action => 'show<%= suffix %>', :id => @<%= singular_name %>.id %> | +<%%= link_to 'Back', :action => 'list<%= suffix %>' %> diff --git a/railties/generators/scaffold/templates/view_list.rhtml b/railties/generators/scaffold/templates/view_list.rhtml new file mode 100644 index 0000000000..068fd67472 --- /dev/null +++ b/railties/generators/scaffold/templates/view_list.rhtml @@ -0,0 +1,24 @@ +

Listing <%= plural_name %>

+ + + +<%% for column in <%= class_name %>.content_columns %> + +<%% end %> + + +<%% for <%= singular_name %> in @<%= plural_name %> %> + + <%% for column in <%= class_name %>.content_columns %> + + <%% end %> + + + + +<%% end %> +
<%%= column.human_name %>
<%%=h <%= singular_name %>[column.name] %><%%= link_to 'Show', :action => 'show<%= suffix %>', :id => <%= singular_name %>.id %><%%= link_to 'Edit', :action => 'edit<%= suffix %>', :id => <%= singular_name %>.id %><%%= link_to 'Destroy', :action => 'destroy<%= suffix %>', :id => <%= singular_name %>.id %>
+ +
+ +<%%= link_to 'New <%= singular_name %>', :action => 'new<%= suffix %>' %> diff --git a/railties/generators/scaffold/templates/view_new.rhtml b/railties/generators/scaffold/templates/view_new.rhtml new file mode 100644 index 0000000000..840bb02775 --- /dev/null +++ b/railties/generators/scaffold/templates/view_new.rhtml @@ -0,0 +1,5 @@ +

New <%= @singular_name %>

+ +<%%= form '<%= singular_name %>', :action => 'create<%= suffix %>' %> + +<%%= link_to 'Back', :action => 'list<%= suffix %>' %> diff --git a/railties/generators/scaffold/templates/view_show.rhtml b/railties/generators/scaffold/templates/view_show.rhtml new file mode 100644 index 0000000000..30a3242f14 --- /dev/null +++ b/railties/generators/scaffold/templates/view_show.rhtml @@ -0,0 +1,8 @@ +<%% for column in <%= class_name %>.content_columns %> +

+ <%%= column.human_name %>: <%%= @<%= singular_name %>[column.name] %> +

+<%% end %> + +<%%= link_to 'Edit', :action => 'edit<%= suffix %>', :id => @<%= singular_name %>.id %> | +<%%= link_to 'Back', :action => 'list<%= suffix %>' %> -- cgit v1.2.3