diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2005-02-15 00:51:02 +0000 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2005-02-15 00:51:02 +0000 |
commit | c844755e5a0c3d4edfcc78f9c30ef91fa0de550a (patch) | |
tree | 4cf4890fc5af5f58dd0a6a19c0a6fea6ed39a1df /railties/lib/rails_generator/generators/components/scaffold | |
parent | a3298e5efdf33398b49933323ea3fef7ff4e9a9c (diff) | |
download | rails-c844755e5a0c3d4edfcc78f9c30ef91fa0de550a.tar.gz rails-c844755e5a0c3d4edfcc78f9c30ef91fa0de550a.tar.bz2 rails-c844755e5a0c3d4edfcc78f9c30ef91fa0de550a.zip |
Merged back the Routing branch
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@614 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'railties/lib/rails_generator/generators/components/scaffold')
3 files changed, 32 insertions, 17 deletions
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 4445995b46..abf9d79ffe 100644 --- a/railties/lib/rails_generator/generators/components/scaffold/scaffold_generator.rb +++ b/railties/lib/rails_generator/generators/components/scaffold/scaffold_generator.rb @@ -41,7 +41,12 @@ class ScaffoldGenerator < Rails::Generator::NamedBase super @controller_name = args.shift || @name.pluralize base_name, @controller_class_path, @controller_class_nesting = extract_modules(@controller_name) - @controller_class_name, @controller_singular_name, @controller_plural_name = inflect_names(base_name) + @controller_class_name_without_nesting, @controller_singular_name, @controller_plural_name = inflect_names(base_name) + if @controller_class_nesting.empty? + @controller_class_name = @controller_class_name_without_nesting + else + @controller_class_name = "#{@controller_class_nesting}::#{@controller_class_name_without_nesting}" + end end def manifest @@ -52,8 +57,12 @@ class ScaffoldGenerator < Rails::Generator::NamedBase # Check for class naming collisions. m.class_collisions "#{controller_class_name}Controller", "#{controller_class_name}ControllerTest", "#{controller_class_name}Helper" - # Views directory. + # Controller, helper, views, and test directories. + m.directory File.join('app/controllers', controller_class_path) + m.directory File.join('app/helpers', controller_class_path) m.directory File.join('app/views', controller_class_path, controller_file_name) + m.directory File.join('test/functional', controller_class_path) + # Controller class, functional test, helper, and views. m.template 'controller.rb', @@ -79,7 +88,8 @@ class ScaffoldGenerator < Rails::Generator::NamedBase scaffold_views.each do |action| m.template "view_#{action}.rhtml", File.join('app/views', - controller_class_path, controller_file_name, + controller_class_path, + controller_file_name, "#{action}.rhtml"), :assigns => { :action => action } end @@ -103,7 +113,8 @@ class ScaffoldGenerator < Rails::Generator::NamedBase unscaffolded_actions.each do |action| m.template "controller:view.rhtml", File.join('app/views', - controller_class_path, controller_file_name, + controller_class_path, + controller_file_name, "#{action}.rhtml"), :assigns => { :action => action } end @@ -153,9 +164,13 @@ class ScaffoldGenerator < Rails::Generator::NamedBase end def model_instance - unless Object.const_defined?(class_name) - Object.const_set(class_name, Class.new(ActiveRecord::Base)) + base = class_nesting.split('::').inject(Object) do |base, nested| + break base.const_get(nested) if base.const_defined?(nested) + base.const_set(nested, Module.new) + end + unless base.const_defined?(@class_name_without_nesting) + base.const_set(@class_name_without_nesting, Class.new(ActiveRecord::Base)) end - Object.const_get(class_name).new + class_name.constantize.new end end 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 ea9c8e4e94..32185fb715 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 @@ -8,9 +8,9 @@ class <%= controller_class_name %>ControllerTest < Test::Unit::TestCase fixtures :<%= table_name %> def setup - $base_id = 1000001 @controller = <%= controller_class_name %>Controller.new - @request, @response = ActionController::TestRequest.new, ActionController::TestResponse.new + @request = ActionController::TestRequest.new + @response = ActionController::TestResponse.new end <% for action in unscaffolded_actions -%> @@ -34,7 +34,7 @@ class <%= controller_class_name %>ControllerTest < Test::Unit::TestCase end def test_show<%= suffix %> - process :show<%= suffix %>, 'id' => $base_id + process :show<%= suffix %>, 'id' => 1 assert_rendered_file 'show' assert_template_has '<%= singular_name %>' assert_valid_record '<%= singular_name %>' @@ -56,25 +56,25 @@ class <%= controller_class_name %>ControllerTest < Test::Unit::TestCase end def test_edit<%= suffix %> - process :edit<%= suffix %>, 'id' => $base_id + process :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' => $base_id } - assert_redirected_to :action => 'show<%= suffix %>', :id => $base_id + process :update<%= suffix %>, '<%= singular_name %>' => { 'id' => 1 } + assert_redirected_to :action => 'show<%= suffix %>', :id => 1 end def test_destroy<%= suffix %> - assert_not_nil <%= class_name %>.find($base_id) + assert_not_nil <%= class_name %>.find(1) - process :destroy, 'id' => $base_id + process :destroy, 'id' => 1 assert_redirected_to :action => 'list<%= suffix %>' assert_raise(ActiveRecord::RecordNotFound) { - <%= singular_name %> = <%= class_name %>.find($base_id) + <%= singular_name %> = <%= class_name %>.find(1) } end end 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 068fd67472..e0d56d1122 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 @@ -10,7 +10,7 @@ <%% for <%= singular_name %> in @<%= plural_name %> %> <tr> <%% for column in <%= class_name %>.content_columns %> - <td><%%=h <%= singular_name %>[column.name] %></td> + <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> |