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
This commit is contained in:
David Heinemeier Hansson
2005-01-13 15:49:26 +00:00
parent 9c09f81bc6
commit 52251baae4
6 changed files with 34 additions and 19 deletions

View File

@@ -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]

View File

@@ -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 -----------------------------------------------------------------------

View File

@@ -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!

View File

@@ -8,14 +8,17 @@ class ScaffoldGenerator < Rails::Generator::Base
# 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 @@ class ScaffoldGenerator < Rails::Generator::Base
# 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

View File

@@ -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 @@ class <%= class_name %>Controller < ApplicationController
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

View File

@@ -1,6 +1,6 @@
<%% for column in <%= class_name %>.content_columns %>
<p>
<b><%%= column.human_name %>:</b> <%%= @<%= singular_name %>[column.name] %>
<b><%%= column.human_name %>:</b> <%%= @<%= singular_name %>.send(column.name) %>
</p>
<%% end %>