From c1a8690d582c08777055caf449c03f85b4c8aa4b Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Mon, 18 Aug 2008 22:38:58 -0500 Subject: Consistently use the framework's configured logger and avoid reverting to RAILS_DEFAULT_LOGGER unless necessary. --- railties/lib/fcgi_handler.rb | 4 +--- railties/lib/initializer.rb | 15 ++++++++++----- 2 files changed, 11 insertions(+), 8 deletions(-) (limited to 'railties/lib') 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..70c6a629ec 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 @@ -403,7 +407,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 +435,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 +536,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 -- cgit v1.2.3 From e9ae2b2f4cae3e9ba4fc8ce91de951d18879af8b Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Tue, 19 Aug 2008 00:18:26 -0500 Subject: Added rack logger middleware that tails the environment log --- railties/lib/rails/rack.rb | 1 + railties/lib/rails/rack/logger.rb | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 railties/lib/rails/rack/logger.rb (limited to 'railties/lib') 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 -- cgit v1.2.3 From bd7edcf286421b090b8de5674422a7316ed043a9 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Tue, 19 Aug 2008 16:46:15 -0500 Subject: Removed config.ru template from app generator --- .../lib/rails_generator/generators/applications/app/app_generator.rb | 1 - 1 file changed, 1 deletion(-) (limited to 'railties/lib') 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..80e8eabfd3 100644 --- a/railties/lib/rails_generator/generators/applications/app/app_generator.rb +++ b/railties/lib/rails_generator/generators/applications/app/app_generator.rb @@ -46,7 +46,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 } -- cgit v1.2.3 From 5df8ff1d6b6c35a7b5924f95ad12693984323513 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Tue, 19 Aug 2008 17:14:17 -0500 Subject: Touch file with git revision when freezing edge --- railties/lib/tasks/framework.rake | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'railties/lib') 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' -- cgit v1.2.3 From 89d1c77dd012f087c091e0f23874c582ea4e3703 Mon Sep 17 00:00:00 2001 From: Tom Ward Date: Tue, 19 Aug 2008 13:19:41 +0100 Subject: Initializer to sort files before eager loading. [#859 state:resolved] Changed Rails::Initializer to sort files before eager loading them. This ensures that any files in a parent directory will be loaded before files in a subdirectory of the 'same' name. i.e. zoo.rb will be loaded before zoo/reptile_house.rb Signed-off-by: Pratik Naik --- railties/lib/initializer.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'railties/lib') diff --git a/railties/lib/initializer.rb b/railties/lib/initializer.rb index 70c6a629ec..ee847e5561 100644 --- a/railties/lib/initializer.rb +++ b/railties/lib/initializer.rb @@ -356,7 +356,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 -- cgit v1.2.3 From 4e4277b9e0a77c62dd1e253156bcde9e4a1a16b2 Mon Sep 17 00:00:00 2001 From: Chris Lloyd Date: Thu, 7 Aug 2008 11:00:18 +1000 Subject: Fixed that rake doc:plugins to uses UTF-8. [#573 state:resolved] Signed-off-by: Pratik Naik --- railties/lib/tasks/documentation.rake | 1 + 1 file changed, 1 insertion(+) (limited to 'railties/lib') 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") -- cgit v1.2.3 From cf28109158054fbab91de2d6d86efe1b40e68d93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tarmo=20T=C3=A4nav?= Date: Sat, 23 Aug 2008 18:05:52 +0300 Subject: Always require activesupport, even if its constant already exists This is needed because the existance of the ActiveSupport constant by itself does not guarantee that the whole library has been loaded. Also load the StringInquirer in the Rails#env method as the it might be called inside the initializer block before activesupport itself has been loaded. --- railties/lib/initializer.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'railties/lib') diff --git a/railties/lib/initializer.rb b/railties/lib/initializer.rb index ee847e5561..008f1de8fa 100644 --- a/railties/lib/initializer.rb +++ b/railties/lib/initializer.rb @@ -49,6 +49,7 @@ module Rails end def env + require 'active_support/string_inquirer' ActiveSupport::StringInquirer.new(RAILS_ENV) end -- cgit v1.2.3 From 9223a919111867e6b47b2627c30d8eb21e8ac7d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tarmo=20T=C3=A4nav?= Date: Sat, 23 Aug 2008 20:58:44 +0300 Subject: Generate belongs_to associations automatically for 'references' types [#640 state:resolved] --- .../lib/rails_generator/generators/components/model/templates/model.rb | 3 +++ 1 file changed, 3 insertions(+) (limited to 'railties/lib') 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..8bf93b9e8b 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 { |a| a.type.to_s == 'references' }.each do |attribute| -%> + belongs_to :<%= attribute.name %> +<% end -%> end -- cgit v1.2.3 From b1f3c6e6ec08a40fc7153ee3ba78d51b466a58e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tarmo=20T=C3=A4nav?= Date: Sat, 23 Aug 2008 21:54:43 +0300 Subject: Generate belongs_to association when generating a model --- railties/lib/rails_generator/generated_attribute.rb | 4 ++++ .../rails_generator/generators/components/model/templates/model.rb | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) (limited to 'railties/lib') 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/components/model/templates/model.rb b/railties/lib/rails_generator/generators/components/model/templates/model.rb index 8bf93b9e8b..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,5 +1,5 @@ class <%= class_name %> < ActiveRecord::Base -<% attributes.select { |a| a.type.to_s == 'references' }.each do |attribute| -%> +<% attributes.select(&:reference?).each do |attribute| -%> belongs_to :<%= attribute.name %> <% end -%> end -- cgit v1.2.3 From eb2b81c766a6f9bf3c27ae0a494ae82956fb8555 Mon Sep 17 00:00:00 2001 From: Michael Koziarski Date: Sun, 24 Aug 2008 13:19:38 +0200 Subject: Reverse the priority of the mysql commands in dbconsole --- railties/lib/commands/dbconsole.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'railties/lib') diff --git a/railties/lib/commands/dbconsole.rb b/railties/lib/commands/dbconsole.rb index 442526ae32..5be3b5dd8e 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"] -- cgit v1.2.3 From b3411ff59eb1e1c31f98f58f117a2ffaaf0c3ff5 Mon Sep 17 00:00:00 2001 From: "Hongli Lai (Phusion" Date: Wed, 27 Aug 2008 12:42:08 +0200 Subject: Deprecate Rails::SecretKeyGenerator in favor of ActiveSupport::SecureRandom. SecureRandom has a few minor security enhancements and can be used as a drop-in replacement Signed-off-by: Michael Koziarski [#913 state:committed] --- .../generators/applications/app/app_generator.rb | 3 +- .../lib/rails_generator/secret_key_generator.rb | 151 +-------------------- railties/lib/tasks/misc.rake | 7 +- 3 files changed, 8 insertions(+), 153 deletions(-) (limited to 'railties/lib') 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 80e8eabfd3..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. diff --git a/railties/lib/rails_generator/secret_key_generator.rb b/railties/lib/rails_generator/secret_key_generator.rb index 5ae492312e..e92ca125a2 100644 --- a/railties/lib/rails_generator/secret_key_generator.rb +++ b/railties/lib/rails_generator/secret_key_generator.rb @@ -5,160 +5,17 @@ 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 end end 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 -- cgit v1.2.3 From b7cd4ded9350fff5edee2298dc04a55e6b285233 Mon Sep 17 00:00:00 2001 From: Michael Koziarski Date: Wed, 27 Aug 2008 15:18:07 +0200 Subject: Formally deprecate the old secret key generator --- railties/lib/rails_generator/secret_key_generator.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'railties/lib') diff --git a/railties/lib/rails_generator/secret_key_generator.rb b/railties/lib/rails_generator/secret_key_generator.rb index e92ca125a2..553811d35d 100644 --- a/railties/lib/rails_generator/secret_key_generator.rb +++ b/railties/lib/rails_generator/secret_key_generator.rb @@ -17,5 +17,6 @@ module Rails def generate_secret ActiveSupport::SecureRandom.hex(64) end + deprecate :generate_secret=>"You should use ActiveSupport::SecureRandom.hex(64)" end end -- cgit v1.2.3