aboutsummaryrefslogtreecommitdiffstats
path: root/railties/lib
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/lib
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/lib')
-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
12 files changed, 65 insertions, 164 deletions
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