diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2005-03-20 23:47:35 +0000 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2005-03-20 23:47:35 +0000 |
commit | 778a9ec84f725ba5c346d975649770f9ebe23446 (patch) | |
tree | 67de215e88a79c72056b0927ffcffabf69dd49a6 /railties | |
parent | 2c918ed9b5fba4802654a4e9792519391fd687d6 (diff) | |
download | rails-778a9ec84f725ba5c346d975649770f9ebe23446.tar.gz rails-778a9ec84f725ba5c346d975649770f9ebe23446.tar.bz2 rails-778a9ec84f725ba5c346d975649770f9ebe23446.zip |
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
Diffstat (limited to 'railties')
11 files changed, 75 insertions, 65 deletions
diff --git a/railties/CHANGELOG b/railties/CHANGELOG index c92fcb6760..c8c0fa540a 100644 --- a/railties/CHANGELOG +++ b/railties/CHANGELOG @@ -1,5 +1,7 @@ *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] diff --git a/railties/lib/rails_generator/generators/components/model/USAGE b/railties/lib/rails_generator/generators/components/model/USAGE index f0669104fe..9d5a2fd749 100644 --- a/railties/lib/rails_generator/generators/components/model/USAGE +++ b/railties/lib/rails_generator/generators/components/model/USAGE @@ -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 diff --git a/railties/lib/rails_generator/generators/components/scaffold/scaffold_generator.rb b/railties/lib/rails_generator/generators/components/scaffold/scaffold_generator.rb index 67f17dc874..6307a73e43 100644 --- a/railties/lib/rails_generator/generators/components/scaffold/scaffold_generator.rb +++ b/railties/lib/rails_generator/generators/components/scaffold/scaffold_generator.rb @@ -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 @@ class ScaffoldGenerator < Rails::Generator::NamedBase 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 @@ class ScaffoldGenerator < Rails::Generator::NamedBase 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 @@ class ScaffoldGenerator < Rails::Generator::NamedBase 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 @@ class ScaffoldGenerator < Rails::Generator::NamedBase "_#{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 @@ class ScaffoldGenerator < Rails::Generator::NamedBase 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) diff --git a/railties/lib/rails_generator/generators/components/scaffold/templates/controller.rb b/railties/lib/rails_generator/generators/components/scaffold/templates/controller.rb index c5e6d87210..b3190918bd 100644 --- a/railties/lib/rails_generator/generators/components/scaffold/templates/controller.rb +++ b/railties/lib/rails_generator/generators/components/scaffold/templates/controller.rb @@ -12,21 +12,21 @@ class <%= controller_class_name %>Controller < ApplicationController <% 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 @@ class <%= controller_class_name %>Controller < ApplicationController 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 diff --git a/railties/lib/rails_generator/generators/components/scaffold/templates/form.rhtml b/railties/lib/rails_generator/generators/components/scaffold/templates/form.rhtml index d314c5f6b5..d15f0d4ec2 100644 --- a/railties/lib/rails_generator/generators/components/scaffold/templates/form.rhtml +++ b/railties/lib/rails_generator/generators/components/scaffold/templates/form.rhtml @@ -1,5 +1,3 @@ -<%%= 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 %> diff --git a/railties/lib/rails_generator/generators/components/scaffold/templates/functional_test.rb b/railties/lib/rails_generator/generators/components/scaffold/templates/functional_test.rb index cbd89d1b4c..c1b934a5d3 100644 --- a/railties/lib/rails_generator/generators/components/scaffold/templates/functional_test.rb +++ b/railties/lib/rails_generator/generators/components/scaffold/templates/functional_test.rb @@ -1,4 +1,4 @@ -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 @@ class <%= controller_class_name %>ControllerTest < Test::Unit::TestCase <% 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 diff --git a/railties/lib/rails_generator/generators/components/scaffold/templates/layout.rhtml b/railties/lib/rails_generator/generators/components/scaffold/templates/layout.rhtml index c4815bd0a3..4fb704219a 100644 --- a/railties/lib/rails_generator/generators/components/scaffold/templates/layout.rhtml +++ b/railties/lib/rails_generator/generators/components/scaffold/templates/layout.rhtml @@ -1,7 +1,7 @@ <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> diff --git a/railties/lib/rails_generator/generators/components/scaffold/templates/view_edit.rhtml b/railties/lib/rails_generator/generators/components/scaffold/templates/view_edit.rhtml index 4ad70f537a..d932095f93 100644 --- a/railties/lib/rails_generator/generators/components/scaffold/templates/view_edit.rhtml +++ b/railties/lib/rails_generator/generators/components/scaffold/templates/view_edit.rhtml @@ -1,7 +1,12 @@ <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 %>' %> diff --git a/railties/lib/rails_generator/generators/components/scaffold/templates/view_list.rhtml b/railties/lib/rails_generator/generators/components/scaffold/templates/view_list.rhtml index e0d56d1122..0ca1d1c97e 100644 --- a/railties/lib/rails_generator/generators/components/scaffold/templates/view_list.rhtml +++ b/railties/lib/rails_generator/generators/components/scaffold/templates/view_list.rhtml @@ -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> diff --git a/railties/lib/rails_generator/generators/components/scaffold/templates/view_new.rhtml b/railties/lib/rails_generator/generators/components/scaffold/templates/view_new.rhtml index 54b2e49312..19cfa12ba2 100644 --- a/railties/lib/rails_generator/generators/components/scaffold/templates/view_new.rhtml +++ b/railties/lib/rails_generator/generators/components/scaffold/templates/view_new.rhtml @@ -1,6 +1,11 @@ <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 %>' %> diff --git a/railties/lib/rails_generator/generators/components/scaffold/templates/view_show.rhtml b/railties/lib/rails_generator/generators/components/scaffold/templates/view_show.rhtml index ba8f3616dd..69fb55b5e4 100644 --- a/railties/lib/rails_generator/generators/components/scaffold/templates/view_show.rhtml +++ b/railties/lib/rails_generator/generators/components/scaffold/templates/view_show.rhtml @@ -1,8 +1,8 @@ -<%% 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 %>' %> |