Skip to content
This repository
Browse code

use autoload instead of explicit requires for ActionView

  • Loading branch information...
commit 2c01f2b4e9d4a95bb2baca8ae57209eb10aa78b2 1 parent e201fc7
Joshua Peek authored November 23, 2008
36  actionpack/lib/action_view.rb
@@ -31,23 +31,29 @@
31 31
   end
32 32
 end
33 33
 
34  
-require 'action_view/template_handlers'
35  
-require 'action_view/renderable'
36  
-require 'action_view/renderable_partial'
  34
+module ActionView
  35
+  def self.load_all!
  36
+    [Base, InlineTemplate, TemplateError]
  37
+  end
37 38
 
38  
-require 'action_view/template'
39  
-require 'action_view/inline_template'
40  
-require 'action_view/paths'
  39
+  autoload :Base, 'action_view/base'
  40
+  autoload :Helpers, 'action_view/helpers'
  41
+  autoload :InlineTemplate, 'action_view/inline_template'
  42
+  autoload :Partials, 'action_view/partials'
  43
+  autoload :PathSet, 'action_view/paths'
  44
+  autoload :Renderable, 'action_view/renderable'
  45
+  autoload :RenderablePartial, 'action_view/renderable_partial'
  46
+  autoload :Template, 'action_view/template'
  47
+  autoload :TemplateError, 'action_view/template_error'
  48
+  autoload :TemplateHandler, 'action_view/template_handler'
  49
+  autoload :TemplateHandlers, 'action_view/template_handlers'
  50
+  autoload :Helpers, 'action_view/helpers'
  51
+end
41 52
 
42  
-require 'action_view/base'
43  
-require 'action_view/partials'
44  
-require 'action_view/template_error'
  53
+class ERB
  54
+  autoload :Util, 'action_view/erb/util'
  55
+end
45 56
 
46 57
 I18n.load_path << "#{File.dirname(__FILE__)}/action_view/locale/en.yml"
47 58
 
48  
-require 'action_view/helpers'
49  
-
50  
-ActionView::Base.class_eval do
51  
-  include ActionView::Partials
52  
-  include ActionView::Helpers
53  
-end
  59
+ActionView.load_all!
2  actionpack/lib/action_view/base.rb
@@ -157,7 +157,7 @@ def initialize(paths, path, template_format = nil)
157 157
   #
158 158
   # See the ActionView::Helpers::PrototypeHelper::GeneratorMethods documentation for more details.
159 159
   class Base
160  
-    include ERB::Util
  160
+    include Helpers, Partials, ::ERB::Util
161 161
     extend ActiveSupport::Memoizable
162 162
 
163 163
     attr_accessor :base_path, :assigns, :template_extension
38  actionpack/lib/action_view/erb/util.rb
... ...
@@ -0,0 +1,38 @@
  1
+require 'erb'
  2
+
  3
+class ERB
  4
+  module Util
  5
+    HTML_ESCAPE = { '&' => '&amp;',  '>' => '&gt;',   '<' => '&lt;', '"' => '&quot;' }
  6
+    JSON_ESCAPE = { '&' => '\u0026', '>' => '\u003E', '<' => '\u003C' }
  7
+
  8
+    # A utility method for escaping HTML tag characters.
  9
+    # This method is also aliased as <tt>h</tt>.
  10
+    #
  11
+    # In your ERb templates, use this method to escape any unsafe content. For example:
  12
+    #   <%=h @person.name %>
  13
+    #
  14
+    # ==== Example:
  15
+    #   puts html_escape("is a > 0 & a < 10?")
  16
+    #   # => is a &gt; 0 &amp; a &lt; 10?
  17
+    def html_escape(s)
  18
+      s.to_s.gsub(/[&"><]/) { |special| HTML_ESCAPE[special] }
  19
+    end
  20
+
  21
+    # A utility method for escaping HTML entities in JSON strings.
  22
+    # This method is also aliased as <tt>j</tt>.
  23
+    #
  24
+    # In your ERb templates, use this method to escape any HTML entities:
  25
+    #   <%=j @person.to_json %>
  26
+    #
  27
+    # ==== Example:
  28
+    #   puts json_escape("is a > 0 & a < 10?")
  29
+    #   # => is a \u003E 0 \u0026 a \u003C 10?
  30
+    def json_escape(s)
  31
+      s.to_s.gsub(/[&"><]/) { |special| JSON_ESCAPE[special] }
  32
+    end
  33
+
  34
+    alias j json_escape
  35
+    module_function :j
  36
+    module_function :json_escape
  37
+  end
  38
+end
28  actionpack/lib/action_view/helpers.rb
... ...
@@ -1,10 +1,28 @@
1  
-Dir.entries(File.expand_path("#{File.dirname(__FILE__)}/helpers")).sort.each do |file|
2  
-  next unless file =~ /^([a-z][a-z_]*_helper).rb$/
3  
-  require "action_view/helpers/#{$1}"
4  
-end
5  
-
6 1
 module ActionView #:nodoc:
7 2
   module Helpers #:nodoc:
  3
+    autoload :ActiveRecordHelper, 'action_view/helpers/active_record_helper'
  4
+    autoload :AssetTagHelper, 'action_view/helpers/asset_tag_helper'
  5
+    autoload :AtomFeedHelper, 'action_view/helpers/atom_feed_helper'
  6
+    autoload :BenchmarkHelper, 'action_view/helpers/benchmark_helper'
  7
+    autoload :CacheHelper, 'action_view/helpers/cache_helper'
  8
+    autoload :CaptureHelper, 'action_view/helpers/capture_helper'
  9
+    autoload :DateHelper, 'action_view/helpers/date_helper'
  10
+    autoload :DebugHelper, 'action_view/helpers/debug_helper'
  11
+    autoload :FormHelper, 'action_view/helpers/form_helper'
  12
+    autoload :FormOptionsHelper, 'action_view/helpers/form_options_helper'
  13
+    autoload :FormTagHelper, 'action_view/helpers/form_tag_helper'
  14
+    autoload :JavascriptHelper, 'action_view/helpers/javascript_helper'
  15
+    autoload :NumberHelper, 'action_view/helpers/number_helper'
  16
+    autoload :PrototypeHelper, 'action_view/helpers/prototype_helper'
  17
+    autoload :RecordIdentificationHelper, 'action_view/helpers/record_identification_helper'
  18
+    autoload :RecordTagHelper, 'action_view/helpers/record_tag_helper'
  19
+    autoload :SanitizeHelper, 'action_view/helpers/sanitize_helper'
  20
+    autoload :ScriptaculousHelper, 'action_view/helpers/scriptaculous_helper'
  21
+    autoload :TagHelper, 'action_view/helpers/tag_helper'
  22
+    autoload :TextHelper, 'action_view/helpers/text_helper'
  23
+    autoload :TranslationHelper, 'action_view/helpers/translation_helper'
  24
+    autoload :UrlHelper, 'action_view/helpers/url_helper'
  25
+
8 26
     def self.included(base)
9 27
       base.extend(ClassMethods)
10 28
     end
5  actionpack/lib/action_view/template_handler.rb
... ...
@@ -1,11 +1,6 @@
1 1
 # Legacy TemplateHandler stub
2 2
 
3 3
 module ActionView
4  
-  module TemplateHandlers
5  
-    module Compilable
6  
-    end
7  
-  end
8  
-
9 4
   class TemplateHandler
10 5
     def self.call(template)
11 6
       new.compile(template)
13  actionpack/lib/action_view/template_handlers.rb
... ...
@@ -1,10 +1,13 @@
1  
-require 'action_view/template_handler'
2  
-require 'action_view/template_handlers/builder'
3  
-require 'action_view/template_handlers/erb'
4  
-require 'action_view/template_handlers/rjs'
5  
-
6 1
 module ActionView #:nodoc:
7 2
   module TemplateHandlers #:nodoc:
  3
+    autoload :ERB, 'action_view/template_handlers/erb'
  4
+    autoload :RJS, 'action_view/template_handlers/rjs'
  5
+    autoload :Builder, 'action_view/template_handlers/builder'
  6
+
  7
+    # Legacy Compilable stub
  8
+    module Compilable
  9
+    end
  10
+
8 11
     def self.extended(base)
9 12
       base.register_default_template_handler :erb, TemplateHandlers::ERB
10 13
       base.register_template_handler :rjs, TemplateHandlers::RJS
39  actionpack/lib/action_view/template_handlers/erb.rb
... ...
@@ -1,42 +1,3 @@
1  
-require 'erb'
2  
-
3  
-class ERB
4  
-  module Util
5  
-    HTML_ESCAPE = { '&' => '&amp;',  '>' => '&gt;',   '<' => '&lt;', '"' => '&quot;' }
6  
-    JSON_ESCAPE = { '&' => '\u0026', '>' => '\u003E', '<' => '\u003C' }
7  
-
8  
-    # A utility method for escaping HTML tag characters.
9  
-    # This method is also aliased as <tt>h</tt>.
10  
-    #
11  
-    # In your ERb templates, use this method to escape any unsafe content. For example:
12  
-    #   <%=h @person.name %>
13  
-    #
14  
-    # ==== Example:
15  
-    #   puts html_escape("is a > 0 & a < 10?")
16  
-    #   # => is a &gt; 0 &amp; a &lt; 10?
17  
-    def html_escape(s)
18  
-      s.to_s.gsub(/[&"><]/) { |special| HTML_ESCAPE[special] }
19  
-    end
20  
-
21  
-    # A utility method for escaping HTML entities in JSON strings.
22  
-    # This method is also aliased as <tt>j</tt>.
23  
-    #
24  
-    # In your ERb templates, use this method to escape any HTML entities:
25  
-    #   <%=j @person.to_json %>
26  
-    #
27  
-    # ==== Example:
28  
-    #   puts json_escape("is a > 0 & a < 10?")
29  
-    #   # => is a \u003E 0 \u0026 a \u003C 10?
30  
-    def json_escape(s)
31  
-      s.to_s.gsub(/[&"><]/) { |special| JSON_ESCAPE[special] }
32  
-    end
33  
-
34  
-    alias j json_escape
35  
-    module_function :j
36  
-    module_function :json_escape
37  
-  end
38  
-end
39  
-
40 1
 module ActionView
41 2
   module TemplateHandlers
42 3
     class ERB < TemplateHandler

2 notes on commit 2c01f2b

Roger Pack

isn’t use of autoload deemed non thread safe? http://www.ruby-forum.com/topic/172385 Thanks!

Joshua Peek
Collaborator

correct, it is not thread safe

threaded mode will eager those these files at boot

Please sign in to comment.
Something went wrong with that request. Please try again.