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 --- railties/bin/destroy | 3 -- railties/bin/generate | 3 -- railties/bin/rails | 3 -- railties/bin/update | 3 -- railties/configs/apache.conf | 59 +--------------------- railties/environments/shared.rb | 2 + railties/environments/shared_for_gem.rb | 2 + railties/generators/model/USAGE | 0 railties/lib/dispatcher.rb | 52 ++++++------------- railties/lib/rails_generator.rb | 6 ++- railties/lib/rails_generator/base.rb | 21 +++++--- .../generators/applications/app/app_generator.rb | 6 ++- .../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 +- railties/lib/rails_generator/lookup.rb | 30 +++++------ railties/lib/rails_generator/options.rb | 19 +++---- railties/lib/rails_generator/scripts.rb | 28 +++++----- railties/lib/rails_generator/scripts/destroy.rb | 4 -- railties/lib/rails_generator/scripts/generate.rb | 4 -- railties/lib/rails_generator/scripts/update.rb | 3 -- railties/test/rails_generator_test.rb | 25 +++++---- 28 files changed, 158 insertions(+), 208 deletions(-) delete mode 100644 railties/generators/model/USAGE (limited to 'railties') diff --git a/railties/bin/destroy b/railties/bin/destroy index 6c1848ce1e..ba6dc7703f 100644 --- a/railties/bin/destroy +++ b/railties/bin/destroy @@ -1,8 +1,5 @@ #!/usr/local/bin/ruby require File.dirname(__FILE__) + '/../config/environment' require 'rails_generator' -require 'rails_generator/simple_logger' require 'rails_generator/scripts/destroy' - -Rails::Generator::Base.logger = Rails::Generator::SimpleLogger.new(STDOUT) Rails::Generator::Scripts::Destroy.new.run(ARGV) diff --git a/railties/bin/generate b/railties/bin/generate index 8bce002510..dde69e61a1 100755 --- a/railties/bin/generate +++ b/railties/bin/generate @@ -1,8 +1,5 @@ #!/usr/local/bin/ruby require File.dirname(__FILE__) + '/../config/environment' require 'rails_generator' -require 'rails_generator/simple_logger' require 'rails_generator/scripts/generate' - -Rails::Generator::Base.logger = Rails::Generator::SimpleLogger.new(STDOUT) Rails::Generator::Scripts::Generate.new.run(ARGV) diff --git a/railties/bin/rails b/railties/bin/rails index da9b2af041..3d23b99609 100755 --- a/railties/bin/rails +++ b/railties/bin/rails @@ -1,7 +1,4 @@ require File.dirname(__FILE__) + '/../lib/rails_generator' -require 'rails_generator/simple_logger' require 'rails_generator/scripts/generate' - -Rails::Generator::Base.logger = Rails::Generator::SimpleLogger.new(STDOUT) Rails::Generator::Base.use_application_sources! Rails::Generator::Scripts::Generate.new.run(ARGV, :generator => 'app') diff --git a/railties/bin/update b/railties/bin/update index 64cc49cf02..430d325b07 100644 --- a/railties/bin/update +++ b/railties/bin/update @@ -1,8 +1,5 @@ #!/usr/local/bin/ruby require File.dirname(__FILE__) + '/../config/environment' require 'rails_generator' -require 'rails_generator/simple_logger' require 'rails_generator/scripts/update' - -Rails::Generator::Base.logger = Rails::Generator::SimpleLogger.new(STDOUT) Rails::Generator::Scripts::Update.new.run(ARGV) diff --git a/railties/configs/apache.conf b/railties/configs/apache.conf index 5548292f8d..1aa874c866 100755 --- a/railties/configs/apache.conf +++ b/railties/configs/apache.conf @@ -1,61 +1,6 @@ -# General Apache options -AddHandler fastcgi-script .fcgi -AddHandler cgi-script .cgi -Options +FollowSymLinks +ExecCGI - -# Make sure that mod_ruby.c has been added and loaded as a module with Apache RewriteEngine On +RewriteCond %{REQUEST_FILENAME} !-f +RewriteRule ^(.*)$ /dispatch.fcgi?$1 [QSA,L] -# Change extension from .cgi to .fcgi to switch to FCGI and to .rb to switch to mod_ruby -RewriteBase /dispatch.cgi - -# Enable this rewrite rule to point to the controller/action that should serve root. -# RewriteRule ^$ /controller/action [R] - -# -# no query string? -RewriteCond %{QUERY_STRING} ^$ - -# no POST method? -RewriteCond %{REQUEST_METHOD} !^POST$ [NC] - -# Request filename is a directory? -RewriteCond %{REQUEST_FILENAME} -d - -# Request filename + '/index' is a file? -RewriteCond %{REQUEST_FILENAME}/index -f - -# Rewrite to request filename + '/index' and finish -RewriteRule ^(.*)/?$ $1/index [QSA,L] - -# no query string? -RewriteCond %{QUERY_STRING} ^$ - -# no POST method? -RewriteCond %{REQUEST_METHOD} !^POST$ [NC] - -# Request filename is a file? -RewriteCond %{REQUEST_FILENAME} -f - -# Finish rewriting -RewriteRule .* - [L] - -# Set default type of cached files to text/html -DefaultType text/html -# - -# Add missing slash -RewriteRule ^([-_a-zA-Z0-9]+)$ /$1/ [R] - -# Default rewriting rules. -RewriteRule ^([-_a-zA-Z0-9]+)/([-_a-zA-Z0-9]+)/([0-9]+)$ ?controller=$1&action=$2&id=$3 [QSA,L] -RewriteRule ^([-_a-zA-Z0-9]+)/([-_a-zA-Z0-9]+)$ ?controller=$1&action=$2 [QSA,L] -RewriteRule ^([-_a-zA-Z0-9]+)/$ ?controller=$1&action=index [QSA,L] - -RewriteRule ^([-_a-zA-Z0-9]+)/([-_a-zA-Z0-9]+)/([-_a-zA-Z0-9]+)/([0-9]+)$ ?module=$1&controller=$2&action=$3&id=$4 [QSA,L] -RewriteRule ^([-_a-zA-Z0-9]+)/([-_a-zA-Z0-9]+)/([-_a-zA-Z0-9]+)$ ?module=$1&controller=$2&action=$3 [QSA,L] -RewriteRule ^([-_a-zA-Z0-9]+)/([-_a-zA-Z0-9]+)/$ ?module=$1&controller=$2&action=index [QSA,L] - -# You can also point these error messages to a controller/action ErrorDocument 500 /500.html ErrorDocument 404 /404.html \ No newline at end of file diff --git a/railties/environments/shared.rb b/railties/environments/shared.rb index 8e36ae6670..f8a8e8cb6f 100644 --- a/railties/environments/shared.rb +++ b/railties/environments/shared.rb @@ -39,6 +39,8 @@ require_dependency "environments/#{RAILS_ENV}" ActiveRecord::Base.configurations = YAML::load(File.open("#{RAILS_ROOT}/config/database.yml")) ActiveRecord::Base.establish_connection +Controllers = Dependencies::LoadingModule.new(File.expand_path(File.join(RAILS_ROOT, 'app', 'controllers'))) + # Configure defaults if the included environment did not. begin diff --git a/railties/environments/shared_for_gem.rb b/railties/environments/shared_for_gem.rb index a277641a99..948b5e47eb 100644 --- a/railties/environments/shared_for_gem.rb +++ b/railties/environments/shared_for_gem.rb @@ -36,6 +36,8 @@ require_dependency "environments/#{RAILS_ENV}" ActiveRecord::Base.configurations = YAML::load(File.open("#{RAILS_ROOT}/config/database.yml")) ActiveRecord::Base.establish_connection +Controllers = Dependencies::LoadingModule.new(File.expand_path(File.join(RAILS_ROOT, 'app', 'controllers'))) + # Configure defaults if the included environment did not. begin diff --git a/railties/generators/model/USAGE b/railties/generators/model/USAGE deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/railties/lib/dispatcher.rb b/railties/lib/dispatcher.rb index 566b031295..74de93713c 100644 --- a/railties/lib/dispatcher.rb +++ b/railties/lib/dispatcher.rb @@ -24,56 +24,34 @@ require 'breakpoint' class Dispatcher - class < exception ActionController::Base.process_with_exception(request, response, exception).out ensure - reset_application if Dependencies.load? - Breakpoint.deactivate_drb if defined?(BREAKPOINT_SERVER_PORT) + reset_application end end private - def reset_application - Dependencies.clear - Dependencies.remove_subclasses_for(ActiveRecord::Base, ActiveRecord::Observer, ActionController::Base) + def prepare_application + ActionController::Routing::Routes.reload if Dependencies.load? + Breakpoint.activate_drb("druby://localhost:#{BREAKPOINT_SERVER_PORT}", nil, !defined?(FastCGI)) if defined?(BREAKPOINT_SERVER_PORT) + Controllers.const_load!("application") unless Controllers.const_defined?(:ApplicationController) end - def controller_path(controller_name, module_name = nil) - if module_name - "#{module_name}/#{controller_name.underscore}_controller" - else - "#{controller_name.underscore}_controller" + def reset_application + if Dependencies.load? + Controllers.clear + Dependencies.clear + Dependencies.remove_subclasses_for(ActiveRecord::Base, ActiveRecord::Observer, ActionController::Base) end - end - - def controller_class(controller_name) - Object.const_get(controller_class_name(controller_name)) - end - - def controller_class_name(controller_name) - "#{controller_name.camelize}Controller" - end - - def controller_name(parameters) - parameters["controller"].downcase.gsub(/[^_a-zA-Z0-9]/, "").untaint - end - def module_name(parameters) - parameters["module"].downcase.gsub(/[^_a-zA-Z0-9]/, "").untaint if parameters["module"] + Breakpoint.deactivate_drb if defined?(BREAKPOINT_SERVER_PORT) end end end \ No newline at end of file diff --git a/railties/lib/rails_generator.rb b/railties/lib/rails_generator.rb index 0875a22dee..c46989819d 100644 --- a/railties/lib/rails_generator.rb +++ b/railties/lib/rails_generator.rb @@ -23,7 +23,7 @@ $:.unshift(File.dirname(__FILE__)) -require 'support/core_ext' +require 'rails_generator/support/core_ext' require 'rails_generator/base' require 'rails_generator/lookup' @@ -31,3 +31,7 @@ require 'rails_generator/commands' Rails::Generator::Base.send(:include, Rails::Generator::Lookup) Rails::Generator::Base.send(:include, Rails::Generator::Commands) + +# Set up a default logger for convenience. +require 'rails_generator/simple_logger' +Rails::Generator::Base.logger = Rails::Generator::SimpleLogger.new(STDOUT) diff --git a/railties/lib/rails_generator/base.rb b/railties/lib/rails_generator/base.rb index 065ce63966..92a54e2453 100644 --- a/railties/lib/rails_generator/base.rb +++ b/railties/lib/rails_generator/base.rb @@ -1,5 +1,5 @@ -require File.dirname(__FILE__) + '/../support/class_attribute_accessors' -require File.dirname(__FILE__) + '/../support/inflector' +require File.dirname(__FILE__) + '/support/class_attribute_accessors' +require File.dirname(__FILE__) + '/support/inflector' require File.dirname(__FILE__) + '/options' require File.dirname(__FILE__) + '/manifest' require File.dirname(__FILE__) + '/spec' @@ -69,8 +69,8 @@ module Rails @source_root = options[:source] || File.join(spec.path, 'templates') if options[:destination] @destination_root = options[:destination] - elsif Object.const_defined?(:RAILS_ROOT) - @destination_root = Object.const_get(:RAILS_ROOT) + elsif defined? ::RAILS_ROOT + @destination_root = ::RAILS_ROOT end # Silence the logger if requested. @@ -173,11 +173,20 @@ module Rails def assign_names!(name) @name = name base_name, @class_path, @class_nesting = extract_modules(@name) - @class_name, @singular_name, @plural_name = inflect_names(base_name) + @class_name_without_nesting, @singular_name, @plural_name = inflect_names(base_name) + if @class_nesting.empty? + @class_name = @class_name_without_nesting + else + @class_name = "#{@class_nesting}::#{@class_name_without_nesting}" + end end + # Extract modules from filesystem-style or ruby-style path: + # good/fun/stuff + # Good::Fun::Stuff + # produce the same results. def extract_modules(name) - modules = name.split('/') + modules = name.include?('/') ? name.split('/') : name.split('::') name = modules.pop path = modules.map { |m| m.underscore } nesting = modules.map { |m| m.camelize }.join('::') diff --git a/railties/lib/rails_generator/generators/applications/app/app_generator.rb b/railties/lib/rails_generator/generators/applications/app/app_generator.rb index 0beb11b237..4a04757ddd 100644 --- a/railties/lib/rails_generator/generators/applications/app/app_generator.rb +++ b/railties/lib/rails_generator/generators/applications/app/app_generator.rb @@ -1,15 +1,16 @@ +require 'rbconfig' + class AppGenerator < Rails::Generator::Base DEFAULT_SHEBANG = File.join(Config::CONFIG['bindir'], Config::CONFIG['ruby_install_name']) default_options :gem => true, :shebang => DEFAULT_SHEBANG - mandatory_options :source => "#{File.dirname(__FILE__)}/../.." + mandatory_options :source => "#{File.dirname(__FILE__)}/../../../../.." def initialize(runtime_args, runtime_options = {}) super usage if args.empty? @destination_root = args.shift - puts "eek! #{destination_root.inspect}" end def manifest @@ -32,6 +33,7 @@ class AppGenerator < Rails::Generator::Base # database.yml and .htaccess m.template "configs/database.yml", "config/database.yml" + m.template "configs/routes.rb", "config/routes.rb" m.template "configs/apache.conf", "public/.htaccess" # Environments 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 %> diff --git a/railties/lib/rails_generator/lookup.rb b/railties/lib/rails_generator/lookup.rb index ba47fd79be..00b78ce645 100644 --- a/railties/lib/rails_generator/lookup.rb +++ b/railties/lib/rails_generator/lookup.rb @@ -4,24 +4,20 @@ class Object class << self # Lookup missing generators using const_missing. This allows any # generator to reference another without having to know its location: - # RubyGems, ~/.rails/generators, and RAILS_ROOT/script/generators all - # cooperate to get the job done. The greatest use of const_missing - # autoloading is to easily subclass existing generators. Example: - # class HorsebackGenerator < PostbackGenerator - # We don't know whether the postback generator is built in, installed - # as a gem, or in the user's home directory, and we shouldn't have to. - unless respond_to?(:pre_generator_const_missing) - alias_method :pre_generator_const_missing, :const_missing - - def const_missing(class_id) - if md = /(.+)Generator$/.match(class_id.to_s) - name = md.captures.first.demodulize.underscore - Rails::Generator::Base.lookup(name).klass - else - pre_generator_const_missing(class_id) - end + # RubyGems, ~/.rails/generators, and RAILS_ROOT/script/generators. + def lookup_missing_generator(class_id) + if md = /(.+)Generator$/.match(class_id.to_s) + name = md.captures.first.demodulize.underscore + Rails::Generator::Base.lookup(name).klass + else + const_missing_before_generators(class_id) end end + + unless respond_to?(:const_missing_before_generators) + alias_method :const_missing_before_generators, :const_missing + alias_method :const_missing, :lookup_missing_generator + end end end @@ -102,7 +98,7 @@ module Rails # 4. Builtins. Model, controller, mailer, scaffold. def use_component_sources! reset_sources - sources << PathSource.new(:app, "#{Object.const_get(:RAILS_ROOT)}/script/generators") if Object.const_defined?(:RAILS_ROOT) + sources << PathSource.new(:app, "#{::RAILS_ROOT}/script/generators") if defined? ::RAILS_ROOT sources << PathSource.new(:user, "#{Dir.user_home}/.rails/generators") sources << GemSource.new if Object.const_defined?(:Gem) sources << PathSource.new(:builtin, "#{File.dirname(__FILE__)}/generators/components") diff --git a/railties/lib/rails_generator/options.rb b/railties/lib/rails_generator/options.rb index afe2d31625..54785413ef 100644 --- a/railties/lib/rails_generator/options.rb +++ b/railties/lib/rails_generator/options.rb @@ -1,8 +1,12 @@ require 'optparse' -require File.dirname(__FILE__) + '/../support/class_inheritable_attributes' +require File.dirname(__FILE__) + '/support/class_inheritable_attributes' module Rails module Generator + # Implement add_options! to add your options to the parser: + # def add_options!(opt) + # opt.on('-v', '--verbose') { |value| options[:verbose] = value } + # end module Options def self.append_features(base) super @@ -85,13 +89,14 @@ module Rails @option_parser = OptionParser.new do |opt| opt.banner = banner - add_options!(opt) + add_options!(opt) if respond_to?(:add_options!) add_general_options!(opt) opt.parse!(args) end + return args + ensure self.options = full_options(runtime_options) - args end # Raise a usage error. Override usage_message to provide a blurb @@ -109,14 +114,6 @@ module Rails "Usage: #{$0} [options]" end - # Override with a method that adds options to the parser. - # Added options should use the options hash. For example, - # def add_options!(opt) - # opt.on('-v', '--verbose') { |value| options[:verbose] = value } - # end - def add_options!(opt) - end - # Adds general options like -h and --quiet. Usually don't override. def add_general_options!(opt) opt.separator '' diff --git a/railties/lib/rails_generator/scripts.rb b/railties/lib/rails_generator/scripts.rb index 007980dcb5..f0b6b6f1ff 100644 --- a/railties/lib/rails_generator/scripts.rb +++ b/railties/lib/rails_generator/scripts.rb @@ -16,21 +16,23 @@ module Rails # or first remaining argument, and invokes the requested command. def run(args = [], runtime_options = {}) begin - parse!(args, runtime_options) - - # Generator name is the only required option. - unless options[:generator] - usage if args.empty? - options[:generator] ||= args.shift - end + parse!(args.dup, runtime_options) + rescue OptionParser::InvalidOption => e + # Don't cry, script. Generators want what you think is invalid. + end - # Look up generator instance and invoke command on it. - Rails::Generator::Base.instance(options[:generator], args, options).command(options[:command]).invoke! - rescue => e - puts e - puts " #{e.backtrace.join("\n ")}\n" if options[:backtrace] - raise SystemExit + # Generator name is the only required option. + unless options[:generator] + usage if args.empty? + options[:generator] ||= args.shift end + + # Look up generator instance and invoke command on it. + Rails::Generator::Base.instance(options[:generator], args, options).command(options[:command]).invoke! + rescue => e + puts e + puts " #{e.backtrace.join("\n ")}\n" if options[:backtrace] + raise SystemExit end protected diff --git a/railties/lib/rails_generator/scripts/destroy.rb b/railties/lib/rails_generator/scripts/destroy.rb index fd8469fbc7..628ec4de32 100644 --- a/railties/lib/rails_generator/scripts/destroy.rb +++ b/railties/lib/rails_generator/scripts/destroy.rb @@ -3,9 +3,5 @@ require File.dirname(__FILE__) + '/../scripts' module Rails::Generator::Scripts class Destroy < Base mandatory_options :command => :destroy - - protected - def add_options!(opt) - end end end diff --git a/railties/lib/rails_generator/scripts/generate.rb b/railties/lib/rails_generator/scripts/generate.rb index 329d6691df..1fe2f54ab3 100644 --- a/railties/lib/rails_generator/scripts/generate.rb +++ b/railties/lib/rails_generator/scripts/generate.rb @@ -3,9 +3,5 @@ require File.dirname(__FILE__) + '/../scripts' module Rails::Generator::Scripts class Generate < Base mandatory_options :command => :create - - protected - def add_options!(opt) - end end end diff --git a/railties/lib/rails_generator/scripts/update.rb b/railties/lib/rails_generator/scripts/update.rb index ad1ae8004a..53a9faa366 100644 --- a/railties/lib/rails_generator/scripts/update.rb +++ b/railties/lib/rails_generator/scripts/update.rb @@ -5,9 +5,6 @@ module Rails::Generator::Scripts mandatory_options :command => :update protected - def add_options!(opt) - end - def banner "Usage: #{$0} [options] scaffold" end diff --git a/railties/test/rails_generator_test.rb b/railties/test/rails_generator_test.rb index 8470bed943..5934bb3e7b 100644 --- a/railties/test/rails_generator_test.rb +++ b/railties/test/rails_generator_test.rb @@ -1,22 +1,29 @@ -$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../lib" -RAILS_ROOT = File.dirname(__FILE__) - require 'test/unit' -require 'rails_generator' -require 'rails_generator/simple_logger' -Rails::Generator::Base.logger = Rails::Generator::SimpleLogger.new - +# Optionally load RubyGems. begin require 'rubygems' - require_gem 'actionpack' rescue LoadError end +# Must set before requiring generator libs. +RAILS_ROOT = File.dirname(__FILE__) + +# Preemptively load the rest of Rails so Gems don't hijack our requires. +require File.dirname(__FILE__) + '/../../activerecord/lib/active_record' +require File.dirname(__FILE__) + '/../../actionpack/lib/action_controller' +require File.dirname(__FILE__) + '/../lib/rails_generator' + class RailsGeneratorTest < Test::Unit::TestCase BUILTINS = %w(controller mailer model scaffold) CAPITALIZED_BUILTINS = BUILTINS.map { |b| b.capitalize } + def test_sources + expected = [:app, :user, :RubyGems, :builtin] + expected.delete(:gem) unless Object.const_defined?(:Gem) + assert_equal expected, Rails::Generator::Base.sources.map { |s| s.label } + end + def test_lookup_builtins (BUILTINS + CAPITALIZED_BUILTINS).each do |name| assert_nothing_raised do @@ -71,7 +78,7 @@ class RailsGeneratorTest < Test::Unit::TestCase assert_equal 'admin/foo', g.name assert_equal %w(admin), g.class_path assert_equal 'Admin', g.class_nesting - assert_equal 'Foo', g.class_name + assert_equal 'Admin::Foo', g.class_name assert_equal 'foo', g.singular_name assert_equal 'foos', g.plural_name assert_equal g.singular_name, g.file_name -- cgit v1.2.3