Improved the generated scaffold code a lot to take advantage of recent Rails...

Improved the generated scaffold code a lot to take advantage of recent Rails developments #882 [Tobias Luetke]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@951 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
上级 2c918ed9
*SVN*
* Improved the generated scaffold code a lot to take advantage of recent Rails developments #882 [Tobias Luetke]
* Combined the script/environment.rb used for gems and regular files version. If vendor/rails/* has all the frameworks, then files version is used, otherwise gems #878 [Nicholas Seckar]
* Changed .htaccess to allow dispatch.* to be called from a sub-directory as part of the push with Action Pack to make Rails work on non-vhost setups #826 [Nicholas Seckar/Tobias Luetke]
......
......@@ -5,7 +5,7 @@ Description:
given in CamelCase or under_score and should not be suffixed with 'Model'.
The generator creates a model class in app/models, a test suite in
test/unit, and test fixtures in test/fixtures/model_name.yml.
test/unit, and test fixtures in test/fixtures/singular_name.yml.
Example:
./script/generate model Account
......
......@@ -6,6 +6,11 @@ class ScaffoldingSandbox
def sandbox_binding
binding
end
def default_input_block
Proc.new { |record, column| "<p><label for=\"#{record}_#{column.name}\">#{column.human_name}</label><br/>\n#{input(record, column.name)}</p>\n" }
end
end
class ActionView::Helpers::InstanceTag
......@@ -54,7 +59,7 @@ def initialize(runtime_args, runtime_options = {})
def manifest
record do |m|
# Depend on model generator but skip if the model exists.
m.dependency 'model', [@name], :collision => :skip
m.dependency 'model', [singular_name], :collision => :skip
# Check for class naming collisions.
m.class_collisions controller_class_path, "#{controller_class_name}Controller", "#{controller_class_name}ControllerTest", "#{controller_class_name}Helper"
......@@ -97,19 +102,16 @@ def manifest
end
# Scaffolded forms.
scaffold_forms.each do |action|
m.complex_template "view_#{action}.rhtml",
File.join('app/views',
controller_class_path,
controller_file_name,
"#{action}.rhtml"),
:assigns => { :action => action },
:insert => 'form.rhtml',
:sandbox => lambda { create_sandbox(action) },
:begin_mark => 'form',
:end_mark => 'eoform',
:mark_id => singular_name
end
m.complex_template "form.rhtml",
File.join('app/views',
controller_class_path,
controller_file_name,
"_form.rhtml"),
:insert => 'form_scaffolding.rhtml',
:sandbox => lambda { create_sandbox },
:begin_mark => 'form',
:end_mark => 'eoform',
:mark_id => singular_name
# Unscaffolded views.
unscaffolded_actions.each do |action|
......@@ -130,16 +132,16 @@ def banner
end
def scaffold_views
%w(list show)
end
def scaffold_forms
%w(new edit)
%w(list show new edit)
end
def scaffold_actions
scaffold_views + %w(index create update destroy)
end
def model_name
class_name.demodulize
end
def unscaffolded_actions
args - scaffold_actions
......@@ -149,10 +151,8 @@ def suffix
"_#{singular_name}" if options[:suffix]
end
def create_sandbox(action)
def create_sandbox
sandbox = ScaffoldingSandbox.new
action = if action == 'edit' then 'update' else 'create' end
sandbox.form_action = action
sandbox.singular_name = singular_name
begin
sandbox.model_instance = model_instance
......@@ -164,7 +164,7 @@ def create_sandbox(action)
sandbox.suffix = suffix
sandbox
end
def model_instance
base = class_nesting.split('::').inject(Object) do |base, nested|
break base.const_get(nested) if base.const_defined?(nested)
......
......@@ -12,21 +12,21 @@ def <%= action %><%= suffix %>
<% end -%>
def list<%= suffix %>
@<%= plural_name %> = <%= class_name %>.find_all
@<%= plural_name %> = <%= model_name %>.find_all
end
def show<%= suffix %>
@<%= singular_name %> = <%= class_name %>.find(@params['id'])
@<%= singular_name %> = <%= model_name %>.find(@params[:id])
end
def new<%= suffix %>
@<%= singular_name %> = <%= class_name %>.new
@<%= singular_name %> = <%= model_name %>.new
end
def create<%= suffix %>
@<%= singular_name %> = <%= class_name %>.new(@params['<%= singular_name %>'])
@<%= singular_name %> = <%= model_name %>.new(@params[:<%= singular_name %>])
if @<%= singular_name %>.save
flash['notice'] = '<%= class_name %> was successfully created.'
flash['notice'] = '<%= model_name %> was successfully created.'
redirect_to :action => 'list<%= suffix %>'
else
render_action 'new<%= suffix %>'
......@@ -34,21 +34,21 @@ def create<%= suffix %>
end
def edit<%= suffix %>
@<%= singular_name %> = <%= class_name %>.find(@params['id'])
@<%= singular_name %> = <%= model_name %>.find(@params[:id])
end
def update
@<%= singular_name %> = <%= class_name %>.find(@params['<%= singular_name %>']['id'])
if @<%= singular_name %>.update_attributes(@params['<%= singular_name %>'])
flash['notice'] = '<%= class_name %> was successfully updated.'
redirect_to :action => 'show<%= suffix %>', :id => @<%= singular_name %>.id
@<%= singular_name %> = <%= model_name %>.find(@params[:id])
if @<%= singular_name %>.update_attributes(@params[:<%= singular_name %>])
flash['notice'] = '<%= model_name %> was successfully updated.'
redirect_to :action => 'show<%= suffix %>', :id => @<%= singular_name %>
else
render_action 'edit<%= suffix %>'
end
end
def destroy<%= suffix %>
<%= class_name %>.find(@params['id']).destroy
<%= model_name %>.find(@params[:id]).destroy
redirect_to :action => 'list<%= suffix %>'
end
end
<%%= start_form_tag :action => '<%= @form_action %><%= @suffix %>' %>
<%%= hidden_field '<%= @singular_name %>', 'id' %>
<%= all_input_tags(@model_instance, @singular_name, {}) %>
<input type="submit" value="<%= @form_action.to_s.capitalize %>" />
<%%= end_form_tag %>
<%%= error_messages_for '<%= singular_name %>' %>
<%= template_for_inclusion %>
require File.dirname(__FILE__) + '<%= '/..' * controller_class_nesting_depth %>/../test_helper'
require File.dirname(__FILE__) + '<%= "/.." * controller_class_nesting_depth %>/../test_helper'
require '<%= controller_file_path %>_controller'
# Re-raise errors caught by the controller.
......@@ -15,66 +15,66 @@ def setup
<% for action in unscaffolded_actions -%>
def test_<%= action %>
process :<%= action %>
get :<%= action %>
assert_rendered_file '<%= action %>'
end
<% end -%>
<% unless suffix -%>
def test_index
process :index
get :index
assert_rendered_file 'list'
end
<% end -%>
def test_list<%= suffix %>
process :list<%= suffix %>
get :list<%= suffix %>
assert_rendered_file 'list<%= suffix %>'
assert_template_has '<%= plural_name %>'
end
def test_show<%= suffix %>
process :show<%= suffix %>, 'id' => 1
get :show<%= suffix %>, 'id' => 1
assert_rendered_file 'show'
assert_template_has '<%= singular_name %>'
assert_valid_record '<%= singular_name %>'
end
def test_new<%= suffix %>
process :new<%= suffix %>
get :new<%= suffix %>
assert_rendered_file 'new<%= suffix %>'
assert_template_has '<%= singular_name %>'
end
def test_create
num_<%= plural_name %> = <%= class_name %>.find_all.size
num_<%= plural_name %> = <%= model_name %>.find_all.size
process :create<%= suffix %>, '<%= singular_name %>' => { }
post :create<%= suffix %>, '<%= singular_name %>' => { }
assert_redirected_to :action => 'list<%= suffix %>'
assert_equal num_<%= plural_name %> + 1, <%= class_name %>.find_all.size
assert_equal num_<%= plural_name %> + 1, <%= model_name %>.find_all.size
end
def test_edit<%= suffix %>
process :edit<%= suffix %>, 'id' => 1
get :edit<%= suffix %>, 'id' => 1
assert_rendered_file 'edit<%= suffix %>'
assert_template_has '<%= singular_name %>'
assert_valid_record '<%= singular_name %>'
end
def test_update<%= suffix %>
process :update<%= suffix %>, '<%= singular_name %>' => { 'id' => 1 }
post :update<%= suffix %>, 'id' => 1
assert_redirected_to :action => 'show<%= suffix %>', :id => 1
end
def test_destroy<%= suffix %>
assert_not_nil <%= class_name %>.find(1)
assert_not_nil <%= model_name %>.find(1)
process :destroy, 'id' => 1
post :destroy, 'id' => 1
assert_redirected_to :action => 'list<%= suffix %>'
assert_raise(ActiveRecord::RecordNotFound) {
<%= singular_name %> = <%= class_name %>.find(1)
<%= singular_name %> = <%= model_name %>.find(1)
}
end
end
<html>
<head>
<title><%= controller_class_name %>: <%%= controller.action_name %></title>
<link href="/stylesheets/scaffold.css" rel="stylesheet" type="text/css" />
<%%= stylesheet_link_tag 'scaffold' %>
</head>
<body>
......
<h1>Editing <%= singular_name %></h1>
<%%= error_messages_for '<%= singular_name %>' %>
<%= template_for_inclusion %>
<%%= link_to 'Show', :action => 'show<%= suffix %>', :id => @<%= singular_name %>.id %> |
<%%= start_form_tag :action => 'update<%= @suffix %>', :id => @<%= singular_name %> %>
<%%= render_partial "form" %>
<input type="submit" value="Edit" />
<%%= end_form_tag %>
<%%= link_to 'Show', :action => 'show<%= suffix %>', :id => @<%= singular_name %> %> |
<%%= link_to 'Back', :action => 'list<%= suffix %>' %>
......@@ -2,19 +2,19 @@
<table>
<tr>
<%% for column in <%= class_name %>.content_columns %>
<%% for column in <%= model_name %>.content_columns %>
<th><%%= column.human_name %></th>
<%% end %>
</tr>
<%% for <%= singular_name %> in @<%= plural_name %> %>
<tr>
<%% for column in <%= class_name %>.content_columns %>
<%% for column in <%= model_name %>.content_columns %>
<td><%%=h <%= singular_name %>.send(column.name) %></td>
<%% end %>
<td><%%= link_to 'Show', :action => 'show<%= suffix %>', :id => <%= singular_name %>.id %></td>
<td><%%= link_to 'Edit', :action => 'edit<%= suffix %>', :id => <%= singular_name %>.id %></td>
<td><%%= link_to 'Destroy', :action => 'destroy<%= suffix %>', :id => <%= singular_name %>.id %></td>
<td><%%= link_to 'Show', :action => 'show<%= suffix %>', :id => <%= singular_name %> %></td>
<td><%%= link_to 'Edit', :action => 'edit<%= suffix %>', :id => <%= singular_name %> %></td>
<td><%%= link_to 'Destroy', {:action => 'destroy<%= suffix %>', :id => <%= singular_name %>}, :confirm => "Are you sure?" %></td>
</tr>
<%% end %>
</table>
......
<h1>New <%= singular_name %></h1>
<%%= error_messages_for '<%= singular_name %>' %>
<%= template_for_inclusion %>
<%%= start_form_tag :action => 'create<%= @suffix %>' %>
<%%= render_partial "form" %>
<input type="submit" value="Create" />
<%%= end_form_tag %>
<%%= link_to 'Back', :action => 'list<%= suffix %>' %>
<%% for column in <%= class_name %>.content_columns %>
<%% for column in <%= model_name %>.content_columns %>
<p>
<b><%%= column.human_name %>:</b> <%%= @<%= singular_name %>.send(column.name) %>
</p>
<%% end %>
<%%= link_to 'Edit', :action => 'edit<%= suffix %>', :id => @<%= singular_name %>.id %> |
<%%= link_to 'Edit', :action => 'edit<%= suffix %>', :id => @<%= singular_name %> %> |
<%%= link_to 'Back', :action => 'list<%= suffix %>' %>
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册