Skip to content

Commit

Permalink
Reorganize autoloads:
Browse files Browse the repository at this point in the history
  * A new module (ActiveSupport::Autoload) is provide that extends
    autoloading with new behavior.
  * All autoloads in modules that have extended ActiveSupport::Autoload
    will be eagerly required in threadsafe environments
  * Autoloads can optionally leave off the path if the path is the same
    as full_constant_name.underscore
  * It is possible to specify that a group of autoloads live under an
    additional path. For instance, all of ActionDispatch's middlewares
    are ActionDispatch::MiddlewareName, but they live under 
    "action_dispatch/middlewares/middleware_name"
  * It is possible to specify that a group of autoloads are all found
    at the same path. For instance, a number of exceptions might all
    be declared there.
  * One consequence of this is that testing-related constants are not
    autoloaded. To get the testing helpers for a given component,
    require "component_name/test_case". For instance, "action_controller/test_case".
  * test_help.rb, which is automatically required by a Rails application's
    test helper, requires the test_case.rb for all active components, so
    this change will not be disruptive in existing or new applications.
  • Loading branch information
Carlhuda committed Dec 3, 2009
1 parent 399909b commit c130409
Show file tree
Hide file tree
Showing 52 changed files with 989 additions and 793 deletions.
34 changes: 18 additions & 16 deletions actionmailer/lib/action_mailer.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -25,32 +25,34 @@
$:.unshift(actionpack_path) if File.directory?(actionpack_path) $:.unshift(actionpack_path) if File.directory?(actionpack_path)
require 'action_controller' require 'action_controller'
require 'action_view' require 'action_view'
require 'active_support/autoload'


module ActionMailer module ActionMailer
def self.load_all! extend ::ActiveSupport::Autoload
[Base, Part, ::Text::Format, ::Net::SMTP]
end autoload :AdvAttrAccessor

autoload :DeprecatedBody
autoload :AdvAttrAccessor, 'action_mailer/adv_attr_accessor' autoload :Base
autoload :DeprecatedBody, 'action_mailer/deprecated_body' autoload :DeliveryMethod
autoload :Base, 'action_mailer/base' autoload :MailHelper
autoload :DeliveryMethod, 'action_mailer/delivery_method' autoload :Part
autoload :Part, 'action_mailer/part' autoload :PartContainer
autoload :PartContainer, 'action_mailer/part_container' autoload :Quoting
autoload :Quoting, 'action_mailer/quoting' autoload :TestHelper
autoload :TestCase, 'action_mailer/test_case' autoload :Utils
autoload :TestHelper, 'action_mailer/test_helper'
autoload :Utils, 'action_mailer/utils'
end end


module Text module Text
extend ActiveSupport::Autoload

autoload :Format, 'action_mailer/vendor/text_format' autoload :Format, 'action_mailer/vendor/text_format'
end end


module Net module Net
autoload :SMTP, 'net/smtp' extend ActiveSupport::Autoload

autoload :SMTP
end end


autoload :MailHelper, 'action_mailer/mail_helper'


require 'action_mailer/vendor/tmail' require 'action_mailer/vendor/tmail'
2 changes: 1 addition & 1 deletion actionmailer/lib/action_mailer/base.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ class Base
include AbstractController::Layouts include AbstractController::Layouts


include AbstractController::Helpers include AbstractController::Helpers
helper MailHelper helper ActionMailer::MailHelper


if Object.const_defined?(:ActionController) if Object.const_defined?(:ActionController)
include ActionController::UrlWriter include ActionController::UrlWriter
Expand Down
2 changes: 1 addition & 1 deletion actionmailer/lib/action_mailer/delivery_method/file.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module DeliveryMethod
# A delivery method implementation which writes all mails to a file. # A delivery method implementation which writes all mails to a file.
class File < Method class File < Method
self.settings = { self.settings = {
:location => defined?(Rails) ? "#{Rails.root}/tmp/mails" : "#{Dir.tmpdir}/mails" :location => defined?(Rails.root) ? "#{Rails.root}/tmp/mails" : "#{Dir.tmpdir}/mails"
} }


def perform_delivery(mail) def perform_delivery(mail)
Expand Down
30 changes: 16 additions & 14 deletions actionmailer/lib/action_mailer/mail_helper.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,17 +1,19 @@
module MailHelper module ActionMailer
# Uses Text::Format to take the text and format it, indented two spaces for module MailHelper
# each line, and wrapped at 72 columns. # Uses Text::Format to take the text and format it, indented two spaces for
def block_format(text) # each line, and wrapped at 72 columns.
formatted = text.split(/\n\r\n/).collect { |paragraph| def block_format(text)
Text::Format.new( formatted = text.split(/\n\r\n/).collect { |paragraph|
:columns => 72, :first_indent => 2, :body_indent => 2, :text => paragraph Text::Format.new(
).format :columns => 72, :first_indent => 2, :body_indent => 2, :text => paragraph
}.join("\n") ).format
}.join("\n")


# Make list points stand on their own line # Make list points stand on their own line
formatted.gsub!(/[ ]*([*]+) ([^*]*)/) { |s| " #{$1} #{$2.strip}\n" } formatted.gsub!(/[ ]*([*]+) ([^*]*)/) { |s| " #{$1} #{$2.strip}\n" }
formatted.gsub!(/[ ]*([#]+) ([^#]*)/) { |s| " #{$1} #{$2.strip}\n" } formatted.gsub!(/[ ]*([#]+) ([^#]*)/) { |s| " #{$1} #{$2.strip}\n" }


formatted formatted
end
end end
end end
25 changes: 15 additions & 10 deletions actionpack/lib/abstract_controller.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@
require "active_support/core_ext/module/delegation" require "active_support/core_ext/module/delegation"


module AbstractController module AbstractController
autoload :Base, "abstract_controller/base" extend ActiveSupport::Autoload
autoload :Callbacks, "abstract_controller/callbacks"
autoload :Helpers, "abstract_controller/helpers" autoload :Base
autoload :Layouts, "abstract_controller/layouts" autoload :Callbacks
autoload :LocalizedCache, "abstract_controller/localized_cache" autoload :Helpers
autoload :Logger, "abstract_controller/logger" autoload :Layouts
autoload :RenderingController, "abstract_controller/rendering_controller" autoload :LocalizedCache
autoload :Logger
autoload :RenderingController

# === Exceptions # === Exceptions
autoload :ActionNotFound, "abstract_controller/exceptions" autoload_at "abstract_controller/exceptions" do
autoload :DoubleRenderError, "abstract_controller/exceptions" autoload :ActionNotFound
autoload :Error, "abstract_controller/exceptions" autoload :DoubleRenderError
autoload :Error
end
end end
2 changes: 1 addition & 1 deletion actionpack/lib/abstract_controller/rendering_controller.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def self.body_to_s(body)
# _partial<TrueClass, FalseClass>:: Whether or not the file to look up is a partial # _partial<TrueClass, FalseClass>:: Whether or not the file to look up is a partial
def _determine_template(options) def _determine_template(options)
if options.key?(:text) if options.key?(:text)
options[:_template] = ActionView::TextTemplate.new(options[:text], format_for_text) options[:_template] = ActionView::Template::Text.new(options[:text], format_for_text)
elsif options.key?(:inline) elsif options.key?(:inline)
handler = ActionView::Template.handler_class_for_extension(options[:type] || "erb") handler = ActionView::Template.handler_class_for_extension(options[:type] || "erb")
template = ActionView::Template.new(options[:inline], "inline #{options[:inline].inspect}", handler, {}) template = ActionView::Template.new(options[:inline], "inline #{options[:inline].inspect}", handler, {})
Expand Down
116 changes: 61 additions & 55 deletions actionpack/lib/action_controller.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,66 +1,72 @@
require "active_support"

module ActionController module ActionController
autoload :Base, "action_controller/base" extend ActiveSupport::Autoload
autoload :Benchmarking, "action_controller/metal/benchmarking"
autoload :ConditionalGet, "action_controller/metal/conditional_get"
autoload :Configuration, "action_controller/metal/configuration"
autoload :Head, "action_controller/metal/head"
autoload :Helpers, "action_controller/metal/helpers"
autoload :HideActions, "action_controller/metal/hide_actions"
autoload :Layouts, "action_controller/metal/layouts"
autoload :Metal, "action_controller/metal"
autoload :Middleware, "action_controller/middleware"
autoload :RackConvenience, "action_controller/metal/rack_convenience"
autoload :Rails2Compatibility, "action_controller/metal/compatibility"
autoload :Redirector, "action_controller/metal/redirector"
autoload :RenderingController, "action_controller/metal/rendering_controller"
autoload :RenderOptions, "action_controller/metal/render_options"
autoload :Rescue, "action_controller/metal/rescuable"
autoload :Responder, "action_controller/metal/responder"
autoload :Session, "action_controller/metal/session"
autoload :Testing, "action_controller/metal/testing"
autoload :UrlFor, "action_controller/metal/url_for"


autoload :Caching, 'action_controller/caching' autoload :Base
autoload :Dispatcher, 'action_controller/dispatch/dispatcher' autoload :Caching
autoload :Integration, 'action_controller/deprecated/integration_test' autoload :PolymorphicRoutes
autoload :IntegrationTest, 'action_controller/deprecated/integration_test' autoload :RecordIdentifier
autoload :MimeResponds, 'action_controller/metal/mime_responds' autoload :UrlRewriter
autoload :PerformanceTest, 'action_controller/deprecated/performance_test' autoload :Translation
autoload :PolymorphicRoutes, 'action_controller/polymorphic_routes' autoload :Metal
autoload :RecordIdentifier, 'action_controller/record_identifier' autoload :Middleware
autoload :Routing, 'action_controller/deprecated'
autoload :SessionManagement, 'action_controller/metal/session_management'
autoload :TestCase, 'action_controller/testing/test_case'
autoload :TestProcess, 'action_controller/testing/process'
autoload :UrlRewriter, 'action_controller/url_rewriter'
autoload :UrlWriter, 'action_controller/url_rewriter'


autoload :Verification, 'action_controller/metal/verification' autoload_under "metal" do
autoload :Flash, 'action_controller/metal/flash' autoload :Benchmarking
autoload :RequestForgeryProtection, 'action_controller/metal/request_forgery_protection' autoload :ConditionalGet
autoload :Streaming, 'action_controller/metal/streaming' autoload :Configuration
autoload :HttpAuthentication, 'action_controller/metal/http_authentication' autoload :Head
autoload :FilterParameterLogging, 'action_controller/metal/filter_parameter_logging' autoload :Helpers
autoload :Translation, 'action_controller/translation' autoload :HideActions
autoload :Cookies, 'action_controller/metal/cookies' autoload :Layouts
autoload :MimeResponds
autoload :RackConvenience
autoload :Compatibility
autoload :Redirector
autoload :RenderingController
autoload :RenderOptions
autoload :Rescue
autoload :Responder
autoload :Session
autoload :SessionManagement
autoload :UrlFor
autoload :Verification
autoload :Flash
autoload :RequestForgeryProtection
autoload :Streaming
autoload :HttpAuthentication
autoload :FilterParameterLogging
autoload :Cookies
end


autoload :ActionControllerError, 'action_controller/metal/exceptions' autoload :Dispatcher, 'action_controller/dispatch/dispatcher'
autoload :RenderError, 'action_controller/metal/exceptions' autoload :PerformanceTest, 'action_controller/deprecated/performance_test'
autoload :RoutingError, 'action_controller/metal/exceptions' autoload :Routing, 'action_controller/deprecated'
autoload :MethodNotAllowed, 'action_controller/metal/exceptions' autoload :Integration, 'action_controller/deprecated/integration_test'
autoload :NotImplemented, 'action_controller/metal/exceptions' autoload :IntegrationTest, 'action_controller/deprecated/integration_test'
autoload :UnknownController, 'action_controller/metal/exceptions'
autoload :MissingFile, 'action_controller/metal/exceptions'
autoload :RenderError, 'action_controller/metal/exceptions'
autoload :SessionOverflowError, 'action_controller/metal/exceptions'
autoload :UnknownHttpMethod, 'action_controller/metal/exceptions'
end


autoload :HTML, 'action_controller/vendor/html-scanner' autoload :UrlWriter, 'action_controller/url_rewriter'
autoload :AbstractController, 'abstract_controller'
autoload_at "action_controller/metal/exceptions" do
autoload :ActionControllerError
autoload :RenderError
autoload :RoutingError
autoload :MethodNotAllowed
autoload :NotImplemented
autoload :UnknownController
autoload :MissingFile
autoload :RenderError
autoload :SessionOverflowError
autoload :UnknownHttpMethod
end
end


# All of these simply register additional autoloads
require 'abstract_controller'
require 'action_dispatch' require 'action_dispatch'
require 'action_view' require 'action_view'
require 'action_controller/vendor/html-scanner'


# Common ActiveSupport usage in ActionController # Common ActiveSupport usage in ActionController
require "active_support/concern" require "active_support/concern"
Expand Down
2 changes: 1 addition & 1 deletion actionpack/lib/action_controller/base.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Base < Metal
include ActionController::MimeResponds include ActionController::MimeResponds


# Rails 2.x compatibility # Rails 2.x compatibility
include ActionController::Rails2Compatibility include ActionController::Compatibility


include ActionController::Cookies include ActionController::Cookies
include ActionController::Session include ActionController::Session
Expand Down
7 changes: 4 additions & 3 deletions actionpack/lib/action_controller/caching.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ module ActionController #:nodoc:
# config.action_controller.cache_store = MyOwnStore.new("parameter") # config.action_controller.cache_store = MyOwnStore.new("parameter")
module Caching module Caching
extend ActiveSupport::Concern extend ActiveSupport::Concern
extend ActiveSupport::Autoload


autoload :Actions, 'action_controller/caching/actions' autoload :Actions
autoload :Fragments, 'action_controller/caching/fragments' autoload :Fragments
autoload :Pages, 'action_controller/caching/pages' autoload :Pages
autoload :Sweeper, 'action_controller/caching/sweeping' autoload :Sweeper, 'action_controller/caching/sweeping'
autoload :Sweeping, 'action_controller/caching/sweeping' autoload :Sweeping, 'action_controller/caching/sweeping'


Expand Down
Original file line number Original file line Diff line number Diff line change
@@ -1,2 +1,4 @@
require "action_dispatch/testing/integration"

ActionController::Integration = ActionDispatch::Integration ActionController::Integration = ActionDispatch::Integration
ActionController::IntegrationTest = ActionDispatch::IntegrationTest ActionController::IntegrationTest = ActionDispatch::IntegrationTest
2 changes: 1 addition & 1 deletion actionpack/lib/action_controller/metal/compatibility.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,5 +1,5 @@
module ActionController module ActionController
module Rails2Compatibility module Compatibility
extend ActiveSupport::Concern extend ActiveSupport::Concern


class ::ActionController::ActionControllerError < StandardError #:nodoc: class ::ActionController::ActionControllerError < StandardError #:nodoc:
Expand Down
2 changes: 1 addition & 1 deletion actionpack/lib/action_controller/metal/helpers.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ module Helpers
included do included do
# Set the default directory for helpers # Set the default directory for helpers
extlib_inheritable_accessor(:helpers_dir) do extlib_inheritable_accessor(:helpers_dir) do
defined?(Rails) ? "#{Rails.root}/app/helpers" : "app/helpers" defined?(Rails.root) ? "#{Rails.root}/app/helpers" : "app/helpers"
end end
end end


Expand Down
Original file line number Original file line Diff line number Diff line change
@@ -1,5 +1,8 @@
require 'active_support/test_case' require 'active_support/test_case'
require 'rack/session/abstract/id' require 'rack/session/abstract/id'
require 'action_controller/metal/testing'
require 'action_controller/testing/process'
require 'action_dispatch/test_case'


module ActionController module ActionController
class TestRequest < ActionDispatch::TestRequest #:nodoc: class TestRequest < ActionDispatch::TestRequest #:nodoc:
Expand Down
2 changes: 2 additions & 0 deletions actionpack/lib/action_controller/vendor/html-scanner.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,6 +1,8 @@
$LOAD_PATH << "#{File.dirname(__FILE__)}/html-scanner" $LOAD_PATH << "#{File.dirname(__FILE__)}/html-scanner"


module HTML module HTML
extend ActiveSupport::Autoload

autoload :CDATA, 'html/node' autoload :CDATA, 'html/node'
autoload :Document, 'html/document' autoload :Document, 'html/document'
autoload :FullSanitizer, 'html/sanitizer' autoload :FullSanitizer, 'html/sanitizer'
Expand Down
41 changes: 21 additions & 20 deletions actionpack/lib/action_dispatch.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -28,37 +28,38 @@ module Rack
end end


module ActionDispatch module ActionDispatch
autoload :Request, 'action_dispatch/http/request' extend ActiveSupport::Autoload
autoload :Response, 'action_dispatch/http/response'
autoload :StatusCodes, 'action_dispatch/http/status_codes'
autoload :Utils, 'action_dispatch/http/utils'


autoload :Callbacks, 'action_dispatch/middleware/callbacks' autoload_under "http" do
autoload :MiddlewareStack, 'action_dispatch/middleware/stack' autoload :Request
autoload :ParamsParser, 'action_dispatch/middleware/params_parser' autoload :Response
autoload :Rescue, 'action_dispatch/middleware/rescue' autoload :StatusCodes
autoload :ShowExceptions, 'action_dispatch/middleware/show_exceptions' autoload :Utils
autoload :Static, 'action_dispatch/middleware/static' end
autoload :StringCoercion, 'action_dispatch/middleware/string_coercion'


autoload :Routing, 'action_dispatch/routing' autoload_under "middleware" do
autoload :Callbacks
autoload :ParamsParser
autoload :Rescue
autoload :ShowExceptions
autoload :Static
autoload :StringCoercion
end


autoload :Assertions, 'action_dispatch/testing/assertions' autoload :MiddlewareStack, 'action_dispatch/middleware/stack'
autoload :Integration, 'action_dispatch/testing/integration' autoload :Routing
autoload :IntegrationTest, 'action_dispatch/testing/integration'
autoload :PerformanceTest, 'action_dispatch/testing/performance_test'
autoload :TestRequest, 'action_dispatch/testing/test_request'
autoload :TestResponse, 'action_dispatch/testing/test_response'


autoload :HTML, 'action_controller/vendor/html-scanner' autoload :HTML, 'action_controller/vendor/html-scanner'


module Http module Http
autoload :Headers, 'action_dispatch/http/headers' extend ActiveSupport::Autoload

autoload :Headers
end end


module Session module Session
autoload :AbstractStore, 'action_dispatch/middleware/session/abstract_store' autoload :AbstractStore, 'action_dispatch/middleware/session/abstract_store'
autoload :CookieStore, 'action_dispatch/middleware/session/cookie_store' autoload :CookieStore, 'action_dispatch/middleware/session/cookie_store'
autoload :MemCacheStore, 'action_dispatch/middleware/session/mem_cache_store' autoload :MemCacheStore, 'action_dispatch/middleware/session/mem_cache_store'
end end
end end
Expand Down
Loading

1 comment on commit c130409

@matthuhiggins
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way for a plugin to use autoload? In my case, I have a plugin that only needs to load when ActionController::Base is referenced, and another plugin that is included into ActiveRecord::ConnectionAdapters. Since most plugins today currently do not autoload, they force the entire environment to load when running tests and starting up.

It seems what I want is the ability to autoload multiple files for a given module. Is there another way?

Please sign in to comment.