From 52251baae48cf25ea06106569af3d04c76841210 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Thu, 13 Jan 2005 15:49:26 +0000 Subject: [PATCH] Added the option to specify a controller name to "generate scaffold" and made the default controller name the plural form of the model. git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@404 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- railties/CHANGELOG | 2 ++ railties/Rakefile | 11 +++++++++++ railties/generators/scaffold/USAGE | 14 ++++++++------ .../generators/scaffold/scaffold_generator.rb | 17 ++++++++++------- .../generators/scaffold/templates/controller.rb | 7 ++----- .../scaffold/templates/view_show.rhtml | 2 +- 6 files changed, 34 insertions(+), 19 deletions(-) diff --git a/railties/CHANGELOG b/railties/CHANGELOG index 008cf98c455e5..e5fb9ade29fc7 100644 --- a/railties/CHANGELOG +++ b/railties/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Added the option to specify a controller name to "generate scaffold" and made the default controller name the plural form of the model. + * Added that rake clone_structure_to_test, db_structure_dump, and purge_test_database tasks now pick up the source database to use from RAILS_ENV instead of just forcing development #424 [Tobias Luetke] diff --git a/railties/Rakefile b/railties/Rakefile index 9719858f14cea..43723af8a2841 100644 --- a/railties/Rakefile +++ b/railties/Rakefile @@ -39,6 +39,9 @@ task :fresh_gem_rails => [ :clean, :make_dir_structure, :initialize_file_stubs, desc "Generates a fresh Rails package without documentation (faster)" task :fresh_rails_without_docs => [ :clean, :make_dir_structure, :initialize_file_stubs, :copy_vendor_libraries, :copy_ties_content ] +desc "Generates a fresh Rails package without documentation (faster)" +task :fresh_rails_without_docs_using_links => [ :clean, :make_dir_structure, :initialize_file_stubs, :link_vendor_libraries, :copy_ties_content ] + desc "Packages the fresh Rails package with documentation" task :package => [ :clean, :fresh_rails ] do system %{cd ..; tar -czvf #{PKG_NAME}-#{PKG_VERSION}.tgz #{PKG_NAME}} @@ -89,6 +92,14 @@ task :copy_vendor_libraries do File.join(PKG_DESTINATION, 'vendor') end +desc "Link in all the Rails packages to vendor" +task :link_vendor_libraries do + return_dir = File.dirname(File.expand_path(__FILE__)) + cd File.join(PKG_DESTINATION, 'vendor') + VENDOR_LIBS.each { |dir| ln_s File.dirname(__FILE__) + "/../../#{dir}", "." } + cd return_dir +end + # Copy Ties Content ----------------------------------------------------------------------- diff --git a/railties/generators/scaffold/USAGE b/railties/generators/scaffold/USAGE index 338ba5aaf546b..a8c9ed896790e 100644 --- a/railties/generators/scaffold/USAGE +++ b/railties/generators/scaffold/USAGE @@ -2,12 +2,14 @@ GENERATOR scaffold - create a model and basic controller SYNOPSIS - generate scaffold ModelName [action ...] + generate scaffold ModelName [ControllerName] [action ...] DESCRIPTION The 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 + first argument, an optional controller name as the second, and + an optional list of controller actions as the subsequent arguments. + If the controller name is not specified, the plural form of the model + name will be used. Any actions with scaffolding code available will be generated in your controller; others will be left as stubs. The generated controller has the same code that "scaffold :model" @@ -15,11 +17,11 @@ DESCRIPTION your controller. EXAMPLE - ./script/generate scaffold Account debit credit + ./script/generate scaffold Account Bank debit credit This will generate the Account model with unit tests and fixtures, - the AccountController controller with actions, views, and tests for + the BankController controller with actions, views, and tests for index, list, show, new, create, edit, update, and destroy. Now create the accounts table in your database and browse to - http://localhost/account/ -- voila, you're on Rails! + http://localhost/bank/ -- voila, you're on Rails! diff --git a/railties/generators/scaffold/scaffold_generator.rb b/railties/generators/scaffold/scaffold_generator.rb index 55bb835bb7bc6..00dd27c5edbf9 100644 --- a/railties/generators/scaffold/scaffold_generator.rb +++ b/railties/generators/scaffold/scaffold_generator.rb @@ -8,14 +8,17 @@ def generate # Fixtures. template "fixtures.yml", "test/fixtures/#{table_name}.yml" + @controller_class_name = args.empty? ? Inflector.pluralize(class_name) : args.shift.sub(/^[a-z]?/) { |m| m.capitalize } + controller_name = Inflector.underscore(@controller_class_name) + # 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" + template "controller.rb", "app/controllers/#{controller_name}_controller.rb" + template "functional_test.rb", "test/functional/#{controller_name}_controller_test.rb" + template "controller/helper.rb", "app/helpers/#{controller_name}_helper.rb" # Layout and stylesheet. - unless File.file?("app/views/layouts/scaffold.rhtml") - template "layout.rhtml", "app/views/layouts/scaffold.rhtml" + unless File.file?("app/views/layouts/#{controller_name}.rhtml") + template "layout.rhtml", "app/views/layouts/#{controller_name}.rhtml" end unless File.file?("public/stylesheets/scaffold.css") template "style.css", "public/stylesheets/scaffold.css" @@ -23,13 +26,13 @@ def generate # Scaffolded views. scaffold_views.each do |action| - template "view_#{action}.rhtml", "app/views/#{file_name}/#{action}.rhtml" + template "view_#{action}.rhtml", "app/views/#{controller_name}/#{action}.rhtml" end # Unscaffolded views. unscaffolded_actions.each do |action| template "controller/view.rhtml", - "app/views/#{file_name}/#{action}.rhtml", + "app/views/#{controller_name}/#{action}.rhtml", binding end end diff --git a/railties/generators/scaffold/templates/controller.rb b/railties/generators/scaffold/templates/controller.rb index 15ade4c49b904..7ccb517beee25 100644 --- a/railties/generators/scaffold/templates/controller.rb +++ b/railties/generators/scaffold/templates/controller.rb @@ -1,6 +1,4 @@ -class <%= class_name %>Controller < ApplicationController - layout 'scaffold' - +class <%= @controller_class_name %>Controller < ApplicationController <% unless suffix -%> def index list @@ -41,8 +39,7 @@ def edit<%= suffix %> def update @<%= singular_name %> = <%= class_name %>.find(@params['<%= singular_name %>']['id']) - @<%= singular_name %>.attributes = @params['<%= singular_name %>'] - if @<%= singular_name %>.save + if @<%= singular_name %>.update_attributes(@params['<%= singular_name %>']) flash['notice'] = '<%= class_name %> was successfully updated.' redirect_to :action => 'show<%= suffix %>', :id => @<%= singular_name %>.id else diff --git a/railties/generators/scaffold/templates/view_show.rhtml b/railties/generators/scaffold/templates/view_show.rhtml index 30a3242f14a58..ba8f3616ddb3d 100644 --- a/railties/generators/scaffold/templates/view_show.rhtml +++ b/railties/generators/scaffold/templates/view_show.rhtml @@ -1,6 +1,6 @@ <%% for column in <%= class_name %>.content_columns %>

- <%%= column.human_name %>: <%%= @<%= singular_name %>[column.name] %> + <%%= column.human_name %>: <%%= @<%= singular_name %>.send(column.name) %>

<%% end %>