From c844755e5a0c3d4edfcc78f9c30ef91fa0de550a Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Tue, 15 Feb 2005 00:51:02 +0000 Subject: Merged back the Routing branch git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@614 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- .../components/controller/controller_generator.rb | 5 +++- .../controller/templates/functional_test.rb | 3 ++- .../components/mailer/mailer_generator.rb | 21 ++++++++++------ .../generators/components/model/model_generator.rb | 5 ++++ .../components/model/templates/fixtures.yml | 6 ++--- .../components/model/templates/unit_test.rb | 4 +-- .../components/scaffold/scaffold_generator.rb | 29 ++++++++++++++++------ .../scaffold/templates/functional_test.rb | 18 +++++++------- .../components/scaffold/templates/view_list.rhtml | 2 +- 9 files changed, 61 insertions(+), 32 deletions(-) (limited to 'railties/lib/rails_generator/generators/components') diff --git a/railties/lib/rails_generator/generators/components/controller/controller_generator.rb b/railties/lib/rails_generator/generators/components/controller/controller_generator.rb index 1f7e69d124..d537031fea 100644 --- a/railties/lib/rails_generator/generators/components/controller/controller_generator.rb +++ b/railties/lib/rails_generator/generators/components/controller/controller_generator.rb @@ -4,8 +4,11 @@ class ControllerGenerator < Rails::Generator::NamedBase # Check for class naming collisions. m.class_collisions "#{class_name}Controller", "#{class_name}ControllerTest", "#{class_name}Helper" - # Views directory even if there are no actions. + # Controller, helper, views, and test directories. + m.directory File.join('app/controllers', class_path) + m.directory File.join('app/helpers', class_path) m.directory File.join('app/views', class_path, file_name) + m.directory File.join('test/functional', class_path) # Controller class, functional test, and helper class. m.template 'controller.rb', diff --git a/railties/lib/rails_generator/generators/components/controller/templates/functional_test.rb b/railties/lib/rails_generator/generators/components/controller/templates/functional_test.rb index c975cb3ce3..76e2b33ba5 100644 --- a/railties/lib/rails_generator/generators/components/controller/templates/functional_test.rb +++ b/railties/lib/rails_generator/generators/components/controller/templates/functional_test.rb @@ -7,7 +7,8 @@ class <%= class_name %>Controller; def rescue_action(e) raise e end; end class <%= class_name %>ControllerTest < Test::Unit::TestCase def setup @controller = <%= class_name %>Controller.new - @request, @response = ActionController::TestRequest.new, ActionController::TestResponse.new + @request = ActionController::TestRequest.new + @response = ActionController::TestResponse.new end # Replace this with your real tests. diff --git a/railties/lib/rails_generator/generators/components/mailer/mailer_generator.rb b/railties/lib/rails_generator/generators/components/mailer/mailer_generator.rb index 81d4599f7f..d8ddb43644 100644 --- a/railties/lib/rails_generator/generators/components/mailer/mailer_generator.rb +++ b/railties/lib/rails_generator/generators/components/mailer/mailer_generator.rb @@ -4,21 +4,28 @@ class MailerGenerator < Rails::Generator::NamedBase # Check for class naming collisions. m.class_collisions class_name, "#{class_name}Test" - # Mailer class and unit test. - m.template "mailer.rb", "app/models/#{file_name}.rb" - m.template "unit_test.rb", "test/unit/#{file_name}_test.rb" + # Mailer, view, test, and fixture directories. + m.directory File.join('app/models', class_path) + m.directory File.join('app/views', class_path, file_name) + m.directory File.join('test/unit', class_path) + m.directory File.join('test/fixtures', class_path, table_name) - # Views and fixtures directories. - m.directory "app/views/#{file_name}" - m.directory "test/fixtures/#{table_name}" + # Mailer class and unit test. + m.template "mailer.rb", File.join('app/models', + class_path, + "#{file_name}.rb") + m.template "unit_test.rb", File.join('test/unit', + class_path, + "#{file_name}_test.rb") # View template and fixture for each action. actions.each do |action| m.template "view.rhtml", - "app/views/#{file_name}/#{action}.rhtml", + File.join('app/views', class_path, file_name, "#{action}.rhtml"), :assigns => { :action => action } m.template "fixture.rhtml", "test/fixtures/#{table_name}/#{action}", + File.join('test/fixtures', class_path, table_name, action), :assigns => { :action => action } end end diff --git a/railties/lib/rails_generator/generators/components/model/model_generator.rb b/railties/lib/rails_generator/generators/components/model/model_generator.rb index 32577d08a3..c3407ca283 100644 --- a/railties/lib/rails_generator/generators/components/model/model_generator.rb +++ b/railties/lib/rails_generator/generators/components/model/model_generator.rb @@ -4,6 +4,11 @@ class ModelGenerator < Rails::Generator::NamedBase # Check for class naming collisions. m.class_collisions class_name, "#{class_name}Test" + # Model, test, and fixture directories. + m.directory File.join('app/models', class_path) + m.directory File.join('test/unit', class_path) + m.directory File.join('test/fixtures', class_path) + # Model class, unit test, and fixtures. m.template 'model.rb', File.join('app/models', class_path, "#{file_name}.rb") m.template 'unit_test.rb', File.join('test/unit', class_path, "#{file_name}_test.rb") diff --git a/railties/lib/rails_generator/generators/components/model/templates/fixtures.yml b/railties/lib/rails_generator/generators/components/model/templates/fixtures.yml index fc3185dc46..6285727968 100644 --- a/railties/lib/rails_generator/generators/components/model/templates/fixtures.yml +++ b/railties/lib/rails_generator/generators/components/model/templates/fixtures.yml @@ -1,10 +1,8 @@ # Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html # Set the $base_id variable in the setup method of your tests. # It's used to ensure that ids don't clash in some databases. -<%% $base_id ||= 100000 %> - first_<%= singular_name %>: - id: <%%= $base_id %> + id: 1 another_<%= singular_name %>: - id: <%%= $base_id + 1 %> + id: 2 diff --git a/railties/lib/rails_generator/generators/components/model/templates/unit_test.rb b/railties/lib/rails_generator/generators/components/model/templates/unit_test.rb index db0fbf5d33..e8714b589a 100644 --- a/railties/lib/rails_generator/generators/components/model/templates/unit_test.rb +++ b/railties/lib/rails_generator/generators/components/model/templates/unit_test.rb @@ -4,11 +4,11 @@ class <%= class_name %>Test < Test::Unit::TestCase fixtures :<%= table_name %> def setup - $base_id = 1000001 + @<%= singular_name %> = <%= class_name %>.find(1) end # Replace this with your real tests. def test_truth - assert true + assert_kind_of <%= class_name %>, @<%= singular_name %> end end 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 %> %> <%% for column in <%= class_name %>.content_columns %> - <%%=h <%= singular_name %>[column.name] %> + <%%=h <%= singular_name %>.send(column.name) %> <%% end %> <%%= link_to 'Show', :action => 'show<%= suffix %>', :id => <%= singular_name %>.id %> <%%= link_to 'Edit', :action => 'edit<%= suffix %>', :id => <%= singular_name %>.id %> -- cgit v1.2.3