aboutsummaryrefslogtreecommitdiffstats
path: root/railties
diff options
context:
space:
mode:
authorPratik Naik <pratiknaik@gmail.com>2008-08-28 11:36:56 +0100
committerPratik Naik <pratiknaik@gmail.com>2008-08-28 11:36:56 +0100
commit3e3945b2fafb9ccedfd9ff181c31b18f5d4cd0ce (patch)
treecd86abce0ab9accbbbf24018961464590fa6bb39 /railties
parent5db2f199aba9aa8d00adefa8237922ad684aca03 (diff)
parent96c6fe084228d570dad80e3100830edb2bc0448d (diff)
downloadrails-3e3945b2fafb9ccedfd9ff181c31b18f5d4cd0ce.tar.gz
rails-3e3945b2fafb9ccedfd9ff181c31b18f5d4cd0ce.tar.bz2
rails-3e3945b2fafb9ccedfd9ff181c31b18f5d4cd0ce.zip
Merge commit 'mainstream/master'
Conflicts: activerecord/lib/active_record/associations/association_proxy.rb activerecord/lib/active_record/callbacks.rb activeresource/lib/active_resource/base.rb
Diffstat (limited to 'railties')
-rw-r--r--railties/html/500.html5
-rw-r--r--railties/lib/commands/dbconsole.rb2
-rw-r--r--railties/lib/fcgi_handler.rb4
-rw-r--r--railties/lib/initializer.rb18
-rw-r--r--railties/lib/rails/rack.rb1
-rw-r--r--railties/lib/rails/rack/logger.rb28
-rw-r--r--railties/lib/rails_generator/generated_attribute.rb4
-rw-r--r--railties/lib/rails_generator/generators/applications/app/app_generator.rb4
-rw-r--r--railties/lib/rails_generator/generators/components/model/templates/model.rb3
-rw-r--r--railties/lib/rails_generator/secret_key_generator.rb152
-rw-r--r--railties/lib/tasks/documentation.rake1
-rw-r--r--railties/lib/tasks/framework.rake5
-rw-r--r--railties/lib/tasks/misc.rake7
-rw-r--r--railties/test/error_page_test.rb43
-rw-r--r--railties/test/fixtures/eager/zoo.rb3
-rw-r--r--railties/test/fixtures/eager/zoo/reptile_house.rb2
-rw-r--r--railties/test/generators/rails_model_generator_test.rb16
-rw-r--r--railties/test/initializer_test.rb18
-rw-r--r--railties/test/secret_key_generation_test.rb8
19 files changed, 151 insertions, 173 deletions
diff --git a/railties/html/500.html b/railties/html/500.html
index 0e9c14f4c6..74142cb04a 100644
--- a/railties/html/500.html
+++ b/railties/html/500.html
@@ -25,6 +25,9 @@
<div class="dialog">
<h1>We're sorry, but something went wrong.</h1>
<p>We've been notified about this issue and we'll take a look at it shortly.</p>
+ <p><small>(If you're the administrator of this website, then please read
+ the log file "<%= "<%s>" % "%=h RAILS_ENV %" %>.log"
+ to find out what went wrong.)</small></p>
</div>
</body>
-</html> \ No newline at end of file
+</html>
diff --git a/railties/lib/commands/dbconsole.rb b/railties/lib/commands/dbconsole.rb
index e0a2abe73d..6ff895aa30 100644
--- a/railties/lib/commands/dbconsole.rb
+++ b/railties/lib/commands/dbconsole.rb
@@ -47,7 +47,7 @@ when "mysql"
args << config['database']
- exec(find_cmd('mysql5', 'mysql'), *args)
+ exec(find_cmd('mysql', 'mysql5'), *args)
when "postgresql"
ENV['PGUSER'] = config["username"] if config["username"]
diff --git a/railties/lib/fcgi_handler.rb b/railties/lib/fcgi_handler.rb
index 722aa1940c..1bb55b9275 100644
--- a/railties/lib/fcgi_handler.rb
+++ b/railties/lib/fcgi_handler.rb
@@ -18,7 +18,6 @@ class RailsFCGIHandler
attr_accessor :log_file_path
attr_accessor :gc_request_period
-
# Initialize and run the FastCGI instance, passing arguments through to new.
def self.process!(*args, &block)
new(*args, &block).process!
@@ -68,7 +67,6 @@ class RailsFCGIHandler
end
end
-
protected
def process_each_request(provider)
cgi = nil
@@ -197,7 +195,7 @@ class RailsFCGIHandler
# close resources as they won't be closed by
# the OS when using exec
logger.close rescue nil
- RAILS_DEFAULT_LOGGER.close rescue nil
+ Rails.logger.close rescue nil
exec(command_line)
end
diff --git a/railties/lib/initializer.rb b/railties/lib/initializer.rb
index 6576cd368b..008f1de8fa 100644
--- a/railties/lib/initializer.rb
+++ b/railties/lib/initializer.rb
@@ -33,7 +33,11 @@ module Rails
end
def logger
- RAILS_DEFAULT_LOGGER
+ if defined?(RAILS_DEFAULT_LOGGER)
+ RAILS_DEFAULT_LOGGER
+ else
+ nil
+ end
end
def root
@@ -45,6 +49,7 @@ module Rails
end
def env
+ require 'active_support/string_inquirer'
ActiveSupport::StringInquirer.new(RAILS_ENV)
end
@@ -352,7 +357,7 @@ Run `rake gems:install` to install the missing gems.
if configuration.cache_classes
configuration.eager_load_paths.each do |load_path|
matcher = /\A#{Regexp.escape(load_path)}(.*)\.rb\Z/
- Dir.glob("#{load_path}/**/*.rb").each do |file|
+ Dir.glob("#{load_path}/**/*.rb").sort.each do |file|
require_dependency file.sub(matcher, '\1')
end
end
@@ -403,7 +408,7 @@ Run `rake gems:install` to install the missing gems.
# +STDERR+, with a log level of +WARN+.
def initialize_logger
# if the environment has explicitly defined a logger, use it
- return if defined?(RAILS_DEFAULT_LOGGER)
+ return if Rails.logger
unless logger = configuration.logger
begin
@@ -431,10 +436,11 @@ Run `rake gems:install` to install the missing gems.
# RAILS_DEFAULT_LOGGER.
def initialize_framework_logging
for framework in ([ :active_record, :action_controller, :action_mailer ] & configuration.frameworks)
- framework.to_s.camelize.constantize.const_get("Base").logger ||= RAILS_DEFAULT_LOGGER
+ framework.to_s.camelize.constantize.const_get("Base").logger ||= Rails.logger
end
- RAILS_CACHE.logger ||= RAILS_DEFAULT_LOGGER
+ ActiveSupport::Dependencies.logger ||= Rails.logger
+ Rails.cache.logger ||= Rails.logger
end
# Sets +ActionController::Base#view_paths+ and +ActionMailer::Base#template_root+
@@ -531,7 +537,7 @@ Run `rake gems:install` to install the missing gems.
return unless configuration.frameworks.include?(:action_controller)
require 'dispatcher' unless defined?(::Dispatcher)
Dispatcher.define_dispatcher_callbacks(configuration.cache_classes)
- Dispatcher.new(RAILS_DEFAULT_LOGGER).send :run_callbacks, :prepare_dispatch
+ Dispatcher.new(Rails.logger).send :run_callbacks, :prepare_dispatch
end
def disable_dependency_loading
diff --git a/railties/lib/rails/rack.rb b/railties/lib/rails/rack.rb
index abcd0741bf..b4f32c2d95 100644
--- a/railties/lib/rails/rack.rb
+++ b/railties/lib/rails/rack.rb
@@ -1,5 +1,6 @@
module Rails
module Rack
+ autoload :Logger, "rails/rack/logger"
autoload :Static, "rails/rack/static"
end
end
diff --git a/railties/lib/rails/rack/logger.rb b/railties/lib/rails/rack/logger.rb
new file mode 100644
index 0000000000..89d02e45a9
--- /dev/null
+++ b/railties/lib/rails/rack/logger.rb
@@ -0,0 +1,28 @@
+module Rails
+ module Rack
+ class Logger
+ EnvironmentLog = "#{File.expand_path(Rails.root)}/log/#{Rails.env}.log"
+
+ def initialize(app, log = nil)
+ @app = app
+ @path = Pathname.new(log || EnvironmentLog).cleanpath
+ @cursor = ::File.size(@path)
+ @last_checked = Time.now
+ end
+
+ def call(env)
+ response = @app.call(env)
+ ::File.open(@path, 'r') do |f|
+ f.seek @cursor
+ if f.mtime > @last_checked
+ contents = f.read
+ @last_checked = f.mtime
+ @cursor += contents.length
+ print contents
+ end
+ end
+ response
+ end
+ end
+ end
+end
diff --git a/railties/lib/rails_generator/generated_attribute.rb b/railties/lib/rails_generator/generated_attribute.rb
index 25af3931de..a3d4a01142 100644
--- a/railties/lib/rails_generator/generated_attribute.rb
+++ b/railties/lib/rails_generator/generated_attribute.rb
@@ -37,6 +37,10 @@ module Rails
""
end
end
+
+ def reference?
+ [ :references, :belongs_to ].include?(self.type)
+ end
end
end
end
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 98fe163455..9849948339 100644
--- a/railties/lib/rails_generator/generators/applications/app/app_generator.rb
+++ b/railties/lib/rails_generator/generators/applications/app/app_generator.rb
@@ -1,6 +1,5 @@
require 'rbconfig'
require 'digest/md5'
-require 'rails_generator/secret_key_generator'
class AppGenerator < Rails::Generator::Base
DEFAULT_SHEBANG = File.join(Config::CONFIG['bindir'],
@@ -36,7 +35,7 @@ class AppGenerator < Rails::Generator::Base
md5 << @app_name
# Do our best to generate a secure secret key for CookieStore
- secret = Rails::SecretKeyGenerator.new(@app_name).generate_secret
+ secret = ActiveSupport::SecureRandom.hex(64)
record do |m|
# Root directory and all subdirectories.
@@ -46,7 +45,6 @@ class AppGenerator < Rails::Generator::Base
# Root
m.file "fresh_rakefile", "Rakefile"
m.file "README", "README"
- m.file "config.ru", "config.ru"
# Application
m.template "helpers/application.rb", "app/controllers/application.rb", :assigns => { :app_name => @app_name, :app_secret => md5.hexdigest }
diff --git a/railties/lib/rails_generator/generators/components/model/templates/model.rb b/railties/lib/rails_generator/generators/components/model/templates/model.rb
index 8d4c89e912..6fcf393bdf 100644
--- a/railties/lib/rails_generator/generators/components/model/templates/model.rb
+++ b/railties/lib/rails_generator/generators/components/model/templates/model.rb
@@ -1,2 +1,5 @@
class <%= class_name %> < ActiveRecord::Base
+<% attributes.select(&:reference?).each do |attribute| -%>
+ belongs_to :<%= attribute.name %>
+<% end -%>
end
diff --git a/railties/lib/rails_generator/secret_key_generator.rb b/railties/lib/rails_generator/secret_key_generator.rb
index 5ae492312e..553811d35d 100644
--- a/railties/lib/rails_generator/secret_key_generator.rb
+++ b/railties/lib/rails_generator/secret_key_generator.rb
@@ -5,160 +5,18 @@ module Rails
#
# generator = Rails::SecretKeyGenerator("some unique identifier, such as the application name")
# generator.generate_secret # => "f3f1be90053fa851... (some long string)"
+ #
+ # This class is *deprecated* in Rails 2.2 in favor of ActiveSupport::SecureRandom.
+ # It is currently a wrapper around ActiveSupport::SecureRandom.
class SecretKeyGenerator
- GENERATORS = [ :secure_random, :win32_api, :urandom, :openssl, :prng ].freeze
-
def initialize(identifier)
- @identifier = identifier
end
# Generate a random secret key with the best possible method available on
# the current platform.
def generate_secret
- generator = GENERATORS.find do |g|
- self.class.send("supports_#{g}?")
- end
- send("generate_secret_with_#{generator}")
- end
-
- # Generate a random secret key by using the Win32 API. Raises LoadError
- # if the current platform cannot make use of the Win32 API. Raises
- # SystemCallError if some other error occurred.
- def generate_secret_with_win32_api
- # Following code is based on David Garamond's GUID library for Ruby.
- require 'Win32API'
-
- crypt_acquire_context = Win32API.new("advapi32", "CryptAcquireContext",
- 'PPPII', 'L')
- crypt_gen_random = Win32API.new("advapi32", "CryptGenRandom",
- 'LIP', 'L')
- crypt_release_context = Win32API.new("advapi32", "CryptReleaseContext",
- 'LI', 'L')
- prov_rsa_full = 1
- crypt_verifycontext = 0xF0000000
-
- hProvStr = " " * 4
- if crypt_acquire_context.call(hProvStr, nil, nil, prov_rsa_full,
- crypt_verifycontext) == 0
- raise SystemCallError, "CryptAcquireContext failed: #{lastWin32ErrorMessage}"
- end
- hProv, = hProvStr.unpack('L')
- bytes = " " * 64
- if crypt_gen_random.call(hProv, bytes.size, bytes) == 0
- raise SystemCallError, "CryptGenRandom failed: #{lastWin32ErrorMessage}"
- end
- if crypt_release_context.call(hProv, 0) == 0
- raise SystemCallError, "CryptReleaseContext failed: #{lastWin32ErrorMessage}"
- end
- bytes.unpack("H*")[0]
- end
-
- # Generate a random secret key with Ruby 1.9's SecureRandom module.
- # Raises LoadError if the current Ruby version does not support
- # SecureRandom.
- def generate_secret_with_secure_random
- require 'securerandom'
- return SecureRandom.hex(64)
- end
-
- # Generate a random secret key with OpenSSL. If OpenSSL is not
- # already loaded, then this method will attempt to load it.
- # LoadError will be raised if that fails.
- def generate_secret_with_openssl
- require 'openssl'
- if !File.exist?("/dev/urandom")
- # OpenSSL transparently seeds the random number generator with
- # data from /dev/urandom. On platforms where that is not
- # available, such as Windows, we have to provide OpenSSL with
- # our own seed. Unfortunately there's no way to provide a
- # secure seed without OS support, so we'll have to do with
- # rand() and Time.now.usec().
- OpenSSL::Random.seed(rand(0).to_s + Time.now.usec.to_s)
- end
- data = OpenSSL::BN.rand(2048, -1, false).to_s
-
- if OpenSSL::OPENSSL_VERSION_NUMBER > 0x00908000
- OpenSSL::Digest::SHA512.new(data).hexdigest
- else
- generate_secret_with_prng
- end
+ ActiveSupport::SecureRandom.hex(64)
end
-
- # Generate a random secret key with /dev/urandom.
- # Raises SystemCallError on failure.
- def generate_secret_with_urandom
- return File.read("/dev/urandom", 64).unpack("H*")[0]
- end
-
- # Generate a random secret key with Ruby's pseudo random number generator,
- # as well as some environment information.
- #
- # This is the least cryptographically secure way to generate a secret key,
- # and should be avoided whenever possible.
- def generate_secret_with_prng
- require 'digest/sha2'
- sha = Digest::SHA2.new(512)
- now = Time.now
- sha << now.to_s
- sha << String(now.usec)
- sha << String(rand(0))
- sha << String($$)
- sha << @identifier
- return sha.hexdigest
- end
-
- private
- def lastWin32ErrorMessage
- # Following code is based on David Garamond's GUID library for Ruby.
- get_last_error = Win32API.new("kernel32", "GetLastError", '', 'L')
- format_message = Win32API.new("kernel32", "FormatMessageA",
- 'LPLLPLPPPPPPPP', 'L')
- format_message_ignore_inserts = 0x00000200
- format_message_from_system = 0x00001000
-
- code = get_last_error.call
- msg = "\0" * 1024
- len = format_message.call(format_message_ignore_inserts +
- format_message_from_system, 0,
- code, 0, msg, 1024, nil, nil,
- nil, nil, nil, nil, nil, nil)
- msg[0, len].tr("\r", '').chomp
- end
-
- def self.supports_secure_random?
- begin
- require 'securerandom'
- true
- rescue LoadError
- false
- end
- end
-
- def self.supports_win32_api?
- return false unless RUBY_PLATFORM =~ /(:?mswin|mingw)/
- begin
- require 'Win32API'
- true
- rescue LoadError
- false
- end
- end
-
- def self.supports_urandom?
- File.exist?('/dev/urandom')
- end
-
- def self.supports_openssl?
- begin
- require 'openssl'
- true
- rescue LoadError
- false
- end
- end
-
- def self.supports_prng?
- true
- end
+ deprecate :generate_secret=>"You should use ActiveSupport::SecureRandom.hex(64)"
end
end
diff --git a/railties/lib/tasks/documentation.rake b/railties/lib/tasks/documentation.rake
index 331b2450a3..f4927a0ef1 100644
--- a/railties/lib/tasks/documentation.rake
+++ b/railties/lib/tasks/documentation.rake
@@ -62,6 +62,7 @@ namespace :doc do
options << "-o doc/plugins/#{plugin}"
options << "--title '#{plugin.titlecase} Plugin Documentation'"
options << '--line-numbers' << '--inline-source'
+ options << '--charset' << 'utf-8'
options << '-T html'
files.include("#{plugin_base}/lib/**/*.rb")
diff --git a/railties/lib/tasks/framework.rake b/railties/lib/tasks/framework.rake
index 71aea09867..66ab78c3b2 100644
--- a/railties/lib/tasks/framework.rake
+++ b/railties/lib/tasks/framework.rake
@@ -43,9 +43,12 @@ namespace :rails do
require 'open-uri'
version = ENV["RELEASE"] || "edge"
target = "rails_#{version}.zip"
+ commits = "http://github.com/api/v1/yaml/rails/rails/commits/master"
url = "http://dev.rubyonrails.org/archives/#{target}"
chdir 'vendor' do
+ latest_revision = YAML.load(open(commits))["commits"].first["id"]
+
puts "Downloading Rails from #{url}"
File.open('rails.zip', 'wb') do |dst|
open url do |src|
@@ -61,6 +64,8 @@ namespace :rails do
%w(rails.zip rails/Rakefile rails/cleanlogs.sh rails/pushgems.rb rails/release.rb).each do |goner|
rm_f goner
end
+
+ touch "rails/REVISION_#{latest_revision}"
end
puts 'Updating current scripts, javascripts, and configuration settings'
diff --git a/railties/lib/tasks/misc.rake b/railties/lib/tasks/misc.rake
index 33bbba1101..5c99725203 100644
--- a/railties/lib/tasks/misc.rake
+++ b/railties/lib/tasks/misc.rake
@@ -3,10 +3,9 @@ task :environment do
require(File.join(RAILS_ROOT, 'config', 'environment'))
end
-require 'rails_generator/secret_key_generator'
-desc 'Generate a crytographically secure secret key. This is typically used to generate a secret for cookie sessions. Pass a unique identifier to the generator using ID="some unique identifier" for greater security.'
+desc 'Generate a crytographically secure secret key. This is typically used to generate a secret for cookie sessions.'
task :secret do
- puts Rails::SecretKeyGenerator.new(ENV['ID']).generate_secret
+ puts ActiveSupport::SecureRandom.hex(64)
end
require 'active_support'
@@ -54,4 +53,4 @@ namespace :time do
puts "\n"
end
end
-end \ No newline at end of file
+end
diff --git a/railties/test/error_page_test.rb b/railties/test/error_page_test.rb
new file mode 100644
index 0000000000..844f889aad
--- /dev/null
+++ b/railties/test/error_page_test.rb
@@ -0,0 +1,43 @@
+require 'abstract_unit'
+require 'action_controller'
+require 'action_controller/test_process'
+
+RAILS_ENV = "test"
+CURRENT_DIR = File.expand_path(File.dirname(__FILE__))
+HTML_DIR = File.expand_path(File.join(CURRENT_DIR, "..", "html"))
+
+module Rails
+ def self.public_path
+ CURRENT_DIR
+ end
+end
+
+class ErrorPageController < ActionController::Base
+ def crash
+ raise StandardError, "crash!"
+ end
+end
+
+ActionController::Routing::Routes.draw do |map|
+ map.connect ':controller/:action/:id'
+end
+
+class ErrorPageControllerTest < Test::Unit::TestCase
+ def setup
+ @controller = ErrorPageController.new
+ @request = ActionController::TestRequest.new
+ @response = ActionController::TestResponse.new
+
+ ActionController::Base.consider_all_requests_local = false
+ end
+
+ def test_500_error_page_instructs_system_administrator_to_check_log_file
+ template = ERB.new(File.read(File.join(HTML_DIR, "500.html")))
+ File.open(File.join(CURRENT_DIR, "500.html"), "w") do |f|
+ f.write(template.result)
+ end
+ get :crash
+ expected_log_file = "#{RAILS_ENV}.log"
+ assert_not_nil @response.body.index(expected_log_file)
+ end
+end
diff --git a/railties/test/fixtures/eager/zoo.rb b/railties/test/fixtures/eager/zoo.rb
new file mode 100644
index 0000000000..8b10ef984b
--- /dev/null
+++ b/railties/test/fixtures/eager/zoo.rb
@@ -0,0 +1,3 @@
+class Zoo
+ include ReptileHouse
+end \ No newline at end of file
diff --git a/railties/test/fixtures/eager/zoo/reptile_house.rb b/railties/test/fixtures/eager/zoo/reptile_house.rb
new file mode 100644
index 0000000000..82bbafce79
--- /dev/null
+++ b/railties/test/fixtures/eager/zoo/reptile_house.rb
@@ -0,0 +1,2 @@
+module Zoo::ReptileHouse
+end \ No newline at end of file
diff --git a/railties/test/generators/rails_model_generator_test.rb b/railties/test/generators/rails_model_generator_test.rb
index 0bfc338566..aea2abafba 100644
--- a/railties/test/generators/rails_model_generator_test.rb
+++ b/railties/test/generators/rails_model_generator_test.rb
@@ -29,4 +29,20 @@ class RailsModelGeneratorTest < GeneratorTestCase
assert_generated_column t, :created_at, :timestamp
end
end
+
+ def test_model_with_reference_attributes_generates_belongs_to_associations
+ run_generator('model', %w(Product name:string supplier:references))
+
+ assert_generated_model_for :product do |body|
+ assert body =~ /^\s+belongs_to :supplier/, "#{body.inspect} should contain 'belongs_to :supplier'"
+ end
+ end
+
+ def test_model_with_belongs_to_attributes_generates_belongs_to_associations
+ run_generator('model', %w(Product name:string supplier:belongs_to))
+
+ assert_generated_model_for :product do |body|
+ assert body =~ /^\s+belongs_to :supplier/, "#{body.inspect} should contain 'belongs_to :supplier'"
+ end
+ end
end
diff --git a/railties/test/initializer_test.rb b/railties/test/initializer_test.rb
index 07303a510e..5147eeb482 100644
--- a/railties/test/initializer_test.rb
+++ b/railties/test/initializer_test.rb
@@ -30,6 +30,24 @@ class Initializer_load_environment_Test < Test::Unit::TestCase
end
+class Initializer_eager_loading_Test < Test::Unit::TestCase
+ def setup
+ @config = ConfigurationMock.new("")
+ @config.cache_classes = true
+ @config.load_paths = [File.expand_path(File.dirname(__FILE__) + "/fixtures/eager")]
+ @config.eager_load_paths = [File.expand_path(File.dirname(__FILE__) + "/fixtures/eager")]
+ @initializer = Rails::Initializer.new(@config)
+ @initializer.set_load_path
+ @initializer.set_autoload_paths
+ end
+
+ def test_eager_loading_loads_parent_classes_before_children
+ assert_nothing_raised do
+ @initializer.load_application_classes
+ end
+ end
+end
+
uses_mocha 'Initializer after_initialize' do
class Initializer_after_initialize_with_blocks_environment_Test < Test::Unit::TestCase
def setup
diff --git a/railties/test/secret_key_generation_test.rb b/railties/test/secret_key_generation_test.rb
index ea1b0dae31..2a04cff856 100644
--- a/railties/test/secret_key_generation_test.rb
+++ b/railties/test/secret_key_generation_test.rb
@@ -33,12 +33,4 @@ class SecretKeyGenerationTest < Test::Unit::TestCase
def test_secret_key_generation
assert @generator.generate_secret.length >= SECRET_KEY_MIN_LENGTH
end
-
- Rails::SecretKeyGenerator::GENERATORS.each do |generator|
- if Rails::SecretKeyGenerator.send("supports_#{generator}?")
- define_method("test_secret_key_generation_with_#{generator}") do
- assert @generator.send("generate_secret_with_#{generator}").length >= SECRET_KEY_MIN_LENGTH
- end
- end
- end
end