aboutsummaryrefslogtreecommitdiffstats
path: root/railties
diff options
context:
space:
mode:
Diffstat (limited to 'railties')
-rw-r--r--railties/bin/destroy3
-rwxr-xr-xrailties/bin/generate3
-rwxr-xr-xrailties/bin/rails3
-rw-r--r--railties/bin/update3
-rwxr-xr-xrailties/configs/apache.conf59
-rw-r--r--railties/environments/shared.rb2
-rw-r--r--railties/environments/shared_for_gem.rb2
-rw-r--r--railties/generators/model/USAGE0
-rw-r--r--railties/lib/dispatcher.rb52
-rw-r--r--railties/lib/rails_generator.rb6
-rw-r--r--railties/lib/rails_generator/base.rb21
-rw-r--r--railties/lib/rails_generator/generators/applications/app/app_generator.rb6
-rw-r--r--railties/lib/rails_generator/generators/components/controller/controller_generator.rb5
-rw-r--r--railties/lib/rails_generator/generators/components/controller/templates/functional_test.rb3
-rw-r--r--railties/lib/rails_generator/generators/components/mailer/mailer_generator.rb21
-rw-r--r--railties/lib/rails_generator/generators/components/model/model_generator.rb5
-rw-r--r--railties/lib/rails_generator/generators/components/model/templates/fixtures.yml6
-rw-r--r--railties/lib/rails_generator/generators/components/model/templates/unit_test.rb4
-rw-r--r--railties/lib/rails_generator/generators/components/scaffold/scaffold_generator.rb29
-rw-r--r--railties/lib/rails_generator/generators/components/scaffold/templates/functional_test.rb18
-rw-r--r--railties/lib/rails_generator/generators/components/scaffold/templates/view_list.rhtml2
-rw-r--r--railties/lib/rails_generator/lookup.rb30
-rw-r--r--railties/lib/rails_generator/options.rb19
-rw-r--r--railties/lib/rails_generator/scripts.rb28
-rw-r--r--railties/lib/rails_generator/scripts/destroy.rb4
-rw-r--r--railties/lib/rails_generator/scripts/generate.rb4
-rw-r--r--railties/lib/rails_generator/scripts/update.rb3
-rw-r--r--railties/test/rails_generator_test.rb25
28 files changed, 208 insertions, 158 deletions
diff --git a/railties/bin/destroy b/railties/bin/destroy
index ba6dc7703f..6c1848ce1e 100644
--- a/railties/bin/destroy
+++ b/railties/bin/destroy
@@ -1,5 +1,8 @@
#!/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 dde69e61a1..8bce002510 100755
--- a/railties/bin/generate
+++ b/railties/bin/generate
@@ -1,5 +1,8 @@
#!/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 3d23b99609..da9b2af041 100755
--- a/railties/bin/rails
+++ b/railties/bin/rails
@@ -1,4 +1,7 @@
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 430d325b07..64cc49cf02 100644
--- a/railties/bin/update
+++ b/railties/bin/update
@@ -1,5 +1,8 @@
#!/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 1aa874c866..5548292f8d 100755
--- a/railties/configs/apache.conf
+++ b/railties/configs/apache.conf
@@ -1,6 +1,61 @@
+# 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]
+
+# <caching>
+# 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
+# </caching>
+
+# 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 f8a8e8cb6f..8e36ae6670 100644
--- a/railties/environments/shared.rb
+++ b/railties/environments/shared.rb
@@ -39,8 +39,6 @@ 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 948b5e47eb..a277641a99 100644
--- a/railties/environments/shared_for_gem.rb
+++ b/railties/environments/shared_for_gem.rb
@@ -36,8 +36,6 @@ 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
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/railties/generators/model/USAGE
diff --git a/railties/lib/dispatcher.rb b/railties/lib/dispatcher.rb
index 74de93713c..566b031295 100644
--- a/railties/lib/dispatcher.rb
+++ b/railties/lib/dispatcher.rb
@@ -24,34 +24,56 @@
require 'breakpoint'
class Dispatcher
- class << self
+ class <<self
def dispatch(cgi = CGI.new, session_options = ActionController::CgiRequest::DEFAULT_SESSION_OPTIONS)
begin
- prepare_application
- request, response = ActionController::CgiRequest.new(cgi, session_options), ActionController::CgiResponse.new(cgi)
- ActionController::Routing::Routes.recognize!(request).process(request, response).out
+ Breakpoint.activate_drb("druby://localhost:#{BREAKPOINT_SERVER_PORT}", nil, !defined?(FastCGI)) if defined?(BREAKPOINT_SERVER_PORT)
+
+ request = ActionController::CgiRequest.new(cgi, session_options)
+ response = ActionController::CgiResponse.new(cgi)
+
+ controller_name, module_name = controller_name(request.parameters), module_name(request.parameters)
+
+ require_or_load("application")
+ require_or_load(controller_path(controller_name, module_name))
+
+ controller_class(controller_name).process(request, response).out
rescue Object => exception
ActionController::Base.process_with_exception(request, response, exception).out
ensure
- reset_application
+ reset_application if Dependencies.load?
+ Breakpoint.deactivate_drb if defined?(BREAKPOINT_SERVER_PORT)
end
end
private
- 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)
+ def reset_application
+ Dependencies.clear
+ Dependencies.remove_subclasses_for(ActiveRecord::Base, ActiveRecord::Observer, ActionController::Base)
end
- def reset_application
- if Dependencies.load?
- Controllers.clear
- Dependencies.clear
- Dependencies.remove_subclasses_for(ActiveRecord::Base, ActiveRecord::Observer, ActionController::Base)
+ def controller_path(controller_name, module_name = nil)
+ if module_name
+ "#{module_name}/#{controller_name.underscore}_controller"
+ else
+ "#{controller_name.underscore}_controller"
end
+ end
- Breakpoint.deactivate_drb if defined?(BREAKPOINT_SERVER_PORT)
+ 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"]
end
end
end \ No newline at end of file
diff --git a/railties/lib/rails_generator.rb b/railties/lib/rails_generator.rb
index c46989819d..0875a22dee 100644
--- a/railties/lib/rails_generator.rb
+++ b/railties/lib/rails_generator.rb
@@ -23,7 +23,7 @@
$:.unshift(File.dirname(__FILE__))
-require 'rails_generator/support/core_ext'
+require 'support/core_ext'
require 'rails_generator/base'
require 'rails_generator/lookup'
@@ -31,7 +31,3 @@ 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 92a54e2453..065ce63966 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 defined? ::RAILS_ROOT
- @destination_root = ::RAILS_ROOT
+ elsif Object.const_defined?(:RAILS_ROOT)
+ @destination_root = Object.const_get(:RAILS_ROOT)
end
# Silence the logger if requested.
@@ -173,20 +173,11 @@ module Rails
def assign_names!(name)
@name = name
base_name, @class_path, @class_nesting = extract_modules(@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
+ @class_name, @singular_name, @plural_name = inflect_names(base_name)
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.include?('/') ? name.split('/') : name.split('::')
+ modules = 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 4a04757ddd..0beb11b237 100644
--- a/railties/lib/rails_generator/generators/applications/app/app_generator.rb
+++ b/railties/lib/rails_generator/generators/applications/app/app_generator.rb
@@ -1,16 +1,15 @@
-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
@@ -33,7 +32,6 @@ 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 d537031fea..1f7e69d124 100644
--- a/railties/lib/rails_generator/generators/components/controller/controller_generator.rb
+++ b/railties/lib/rails_generator/generators/components/controller/controller_generator.rb
@@ -4,11 +4,8 @@ class ControllerGenerator < Rails::Generator::NamedBase
# Check for class naming collisions.
m.class_collisions "#{class_name}Controller", "#{class_name}ControllerTest", "#{class_name}Helper"
- # Controller, helper, views, and test directories.
- m.directory File.join('app/controllers', class_path)
- m.directory File.join('app/helpers', class_path)
+ # Views directory even if there are no actions.
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 76e2b33ba5..c975cb3ce3 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,8 +7,7 @@ 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 = ActionController::TestRequest.new
- @response = ActionController::TestResponse.new
+ @request, @response = ActionController::TestRequest.new, 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 d8ddb43644..81d4599f7f 100644
--- a/railties/lib/rails_generator/generators/components/mailer/mailer_generator.rb
+++ b/railties/lib/rails_generator/generators/components/mailer/mailer_generator.rb
@@ -4,28 +4,21 @@ class MailerGenerator < Rails::Generator::NamedBase
# Check for class naming collisions.
m.class_collisions class_name, "#{class_name}Test"
- # 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)
-
# 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")
+ m.template "mailer.rb", "app/models/#{file_name}.rb"
+ m.template "unit_test.rb", "test/unit/#{file_name}_test.rb"
+
+ # Views and fixtures directories.
+ m.directory "app/views/#{file_name}"
+ m.directory "test/fixtures/#{table_name}"
# View template and fixture for each action.
actions.each do |action|
m.template "view.rhtml",
- File.join('app/views', class_path, file_name, "#{action}.rhtml"),
+ "app/views/#{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 c3407ca283..32577d08a3 100644
--- a/railties/lib/rails_generator/generators/components/model/model_generator.rb
+++ b/railties/lib/rails_generator/generators/components/model/model_generator.rb
@@ -4,11 +4,6 @@ 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 6285727968..fc3185dc46 100644
--- a/railties/lib/rails_generator/generators/components/model/templates/fixtures.yml
+++ b/railties/lib/rails_generator/generators/components/model/templates/fixtures.yml
@@ -1,8 +1,10 @@
# 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: 1
+ id: <%%= $base_id %>
another_<%= singular_name %>:
- id: 2
+ id: <%%= $base_id + 1 %>
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 e8714b589a..db0fbf5d33 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
- @<%= singular_name %> = <%= class_name %>.find(1)
+ $base_id = 1000001
end
# Replace this with your real tests.
def test_truth
- assert_kind_of <%= class_name %>, @<%= singular_name %>
+ assert true
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 abf9d79ffe..4445995b46 100644
--- a/railties/lib/rails_generator/generators/components/scaffold/scaffold_generator.rb
+++ b/railties/lib/rails_generator/generators/components/scaffold/scaffold_generator.rb
@@ -41,12 +41,7 @@ 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_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
+ @controller_class_name, @controller_singular_name, @controller_plural_name = inflect_names(base_name)
end
def manifest
@@ -57,12 +52,8 @@ 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"
- # Controller, helper, views, and test directories.
- m.directory File.join('app/controllers', controller_class_path)
- m.directory File.join('app/helpers', controller_class_path)
+ # Views directory.
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',
@@ -88,8 +79,7 @@ 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
@@ -113,8 +103,7 @@ 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
@@ -164,13 +153,9 @@ class ScaffoldGenerator < Rails::Generator::NamedBase
end
def model_instance
- 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))
+ unless Object.const_defined?(class_name)
+ Object.const_set(class_name, Class.new(ActiveRecord::Base))
end
- class_name.constantize.new
+ Object.const_get(class_name).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 32185fb715..ea9c8e4e94 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 = ActionController::TestRequest.new
- @response = ActionController::TestResponse.new
+ @request, @response = ActionController::TestRequest.new, 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' => 1
+ process :show<%= suffix %>, 'id' => $base_id
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' => 1
+ process :edit<%= suffix %>, 'id' => $base_id
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 }
- assert_redirected_to :action => 'show<%= suffix %>', :id => 1
+ process :update<%= suffix %>, '<%= singular_name %>' => { 'id' => $base_id }
+ assert_redirected_to :action => 'show<%= suffix %>', :id => $base_id
end
def test_destroy<%= suffix %>
- assert_not_nil <%= class_name %>.find(1)
+ assert_not_nil <%= class_name %>.find($base_id)
- process :destroy, 'id' => 1
+ process :destroy, 'id' => $base_id
assert_redirected_to :action => 'list<%= suffix %>'
assert_raise(ActiveRecord::RecordNotFound) {
- <%= singular_name %> = <%= class_name %>.find(1)
+ <%= singular_name %> = <%= class_name %>.find($base_id)
}
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 e0d56d1122..068fd67472 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 %>.send(column.name) %></td>
+ <td><%%=h <%= singular_name %>[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>
diff --git a/railties/lib/rails_generator/lookup.rb b/railties/lib/rails_generator/lookup.rb
index 00b78ce645..ba47fd79be 100644
--- a/railties/lib/rails_generator/lookup.rb
+++ b/railties/lib/rails_generator/lookup.rb
@@ -4,20 +4,24 @@ 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.
- 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)
+ # 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
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
@@ -98,7 +102,7 @@ module Rails
# 4. Builtins. Model, controller, mailer, scaffold.
def use_component_sources!
reset_sources
- sources << PathSource.new(:app, "#{::RAILS_ROOT}/script/generators") if defined? ::RAILS_ROOT
+ sources << PathSource.new(:app, "#{Object.const_get(:RAILS_ROOT)}/script/generators") if Object.const_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 54785413ef..afe2d31625 100644
--- a/railties/lib/rails_generator/options.rb
+++ b/railties/lib/rails_generator/options.rb
@@ -1,12 +1,8 @@
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
@@ -89,14 +85,13 @@ module Rails
@option_parser = OptionParser.new do |opt|
opt.banner = banner
- add_options!(opt) if respond_to?(:add_options!)
+ add_options!(opt)
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
@@ -114,6 +109,14 @@ 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 f0b6b6f1ff..007980dcb5 100644
--- a/railties/lib/rails_generator/scripts.rb
+++ b/railties/lib/rails_generator/scripts.rb
@@ -16,23 +16,21 @@ module Rails
# or first remaining argument, and invokes the requested command.
def run(args = [], runtime_options = {})
begin
- parse!(args.dup, runtime_options)
- rescue OptionParser::InvalidOption => e
- # Don't cry, script. Generators want what you think is invalid.
- end
+ parse!(args, runtime_options)
- # Generator name is the only required option.
- unless options[:generator]
- usage if args.empty?
- options[:generator] ||= args.shift
- end
+ # 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
+ # 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
end
protected
diff --git a/railties/lib/rails_generator/scripts/destroy.rb b/railties/lib/rails_generator/scripts/destroy.rb
index 628ec4de32..fd8469fbc7 100644
--- a/railties/lib/rails_generator/scripts/destroy.rb
+++ b/railties/lib/rails_generator/scripts/destroy.rb
@@ -3,5 +3,9 @@ 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 1fe2f54ab3..329d6691df 100644
--- a/railties/lib/rails_generator/scripts/generate.rb
+++ b/railties/lib/rails_generator/scripts/generate.rb
@@ -3,5 +3,9 @@ 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 53a9faa366..ad1ae8004a 100644
--- a/railties/lib/rails_generator/scripts/update.rb
+++ b/railties/lib/rails_generator/scripts/update.rb
@@ -5,6 +5,9 @@ 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 5934bb3e7b..8470bed943 100644
--- a/railties/test/rails_generator_test.rb
+++ b/railties/test/rails_generator_test.rb
@@ -1,29 +1,22 @@
+$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../lib"
+RAILS_ROOT = File.dirname(__FILE__)
+
require 'test/unit'
-# Optionally load RubyGems.
+require 'rails_generator'
+require 'rails_generator/simple_logger'
+Rails::Generator::Base.logger = Rails::Generator::SimpleLogger.new
+
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
@@ -78,7 +71,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 'Admin::Foo', g.class_name
+ assert_equal 'Foo', g.class_name
assert_equal 'foo', g.singular_name
assert_equal 'foos', g.plural_name
assert_equal g.singular_name, g.file_name