use autoload instead of explicit requires for ActionView

This commit is contained in:
Joshua Peek
2008-11-23 13:42:07 -06:00
parent e201fc750b
commit 2c01f2b4e9
7 changed files with 91 additions and 70 deletions

View File

@@ -31,23 +31,29 @@ rescue LoadError
end
end
require 'action_view/template_handlers'
require 'action_view/renderable'
require 'action_view/renderable_partial'
module ActionView
def self.load_all!
[Base, InlineTemplate, TemplateError]
end
require 'action_view/template'
require 'action_view/inline_template'
require 'action_view/paths'
autoload :Base, 'action_view/base'
autoload :Helpers, 'action_view/helpers'
autoload :InlineTemplate, 'action_view/inline_template'
autoload :Partials, 'action_view/partials'
autoload :PathSet, 'action_view/paths'
autoload :Renderable, 'action_view/renderable'
autoload :RenderablePartial, 'action_view/renderable_partial'
autoload :Template, 'action_view/template'
autoload :TemplateError, 'action_view/template_error'
autoload :TemplateHandler, 'action_view/template_handler'
autoload :TemplateHandlers, 'action_view/template_handlers'
autoload :Helpers, 'action_view/helpers'
end
require 'action_view/base'
require 'action_view/partials'
require 'action_view/template_error'
class ERB
autoload :Util, 'action_view/erb/util'
end
I18n.load_path << "#{File.dirname(__FILE__)}/action_view/locale/en.yml"
require 'action_view/helpers'
ActionView::Base.class_eval do
include ActionView::Partials
include ActionView::Helpers
end
ActionView.load_all!

View File

@@ -157,7 +157,7 @@ module ActionView #:nodoc:
#
# See the ActionView::Helpers::PrototypeHelper::GeneratorMethods documentation for more details.
class Base
include ERB::Util
include Helpers, Partials, ::ERB::Util
extend ActiveSupport::Memoizable
attr_accessor :base_path, :assigns, :template_extension

View File

@@ -0,0 +1,38 @@
require 'erb'
class ERB
module Util
HTML_ESCAPE = { '&' => '&amp;', '>' => '&gt;', '<' => '&lt;', '"' => '&quot;' }
JSON_ESCAPE = { '&' => '\u0026', '>' => '\u003E', '<' => '\u003C' }
# A utility method for escaping HTML tag characters.
# This method is also aliased as <tt>h</tt>.
#
# In your ERb templates, use this method to escape any unsafe content. For example:
# <%=h @person.name %>
#
# ==== Example:
# puts html_escape("is a > 0 & a < 10?")
# # => is a &gt; 0 &amp; a &lt; 10?
def html_escape(s)
s.to_s.gsub(/[&"><]/) { |special| HTML_ESCAPE[special] }
end
# A utility method for escaping HTML entities in JSON strings.
# This method is also aliased as <tt>j</tt>.
#
# In your ERb templates, use this method to escape any HTML entities:
# <%=j @person.to_json %>
#
# ==== Example:
# puts json_escape("is a > 0 & a < 10?")
# # => is a \u003E 0 \u0026 a \u003C 10?
def json_escape(s)
s.to_s.gsub(/[&"><]/) { |special| JSON_ESCAPE[special] }
end
alias j json_escape
module_function :j
module_function :json_escape
end
end

View File

@@ -1,10 +1,28 @@
Dir.entries(File.expand_path("#{File.dirname(__FILE__)}/helpers")).sort.each do |file|
next unless file =~ /^([a-z][a-z_]*_helper).rb$/
require "action_view/helpers/#{$1}"
end
module ActionView #:nodoc:
module Helpers #:nodoc:
autoload :ActiveRecordHelper, 'action_view/helpers/active_record_helper'
autoload :AssetTagHelper, 'action_view/helpers/asset_tag_helper'
autoload :AtomFeedHelper, 'action_view/helpers/atom_feed_helper'
autoload :BenchmarkHelper, 'action_view/helpers/benchmark_helper'
autoload :CacheHelper, 'action_view/helpers/cache_helper'
autoload :CaptureHelper, 'action_view/helpers/capture_helper'
autoload :DateHelper, 'action_view/helpers/date_helper'
autoload :DebugHelper, 'action_view/helpers/debug_helper'
autoload :FormHelper, 'action_view/helpers/form_helper'
autoload :FormOptionsHelper, 'action_view/helpers/form_options_helper'
autoload :FormTagHelper, 'action_view/helpers/form_tag_helper'
autoload :JavascriptHelper, 'action_view/helpers/javascript_helper'
autoload :NumberHelper, 'action_view/helpers/number_helper'
autoload :PrototypeHelper, 'action_view/helpers/prototype_helper'
autoload :RecordIdentificationHelper, 'action_view/helpers/record_identification_helper'
autoload :RecordTagHelper, 'action_view/helpers/record_tag_helper'
autoload :SanitizeHelper, 'action_view/helpers/sanitize_helper'
autoload :ScriptaculousHelper, 'action_view/helpers/scriptaculous_helper'
autoload :TagHelper, 'action_view/helpers/tag_helper'
autoload :TextHelper, 'action_view/helpers/text_helper'
autoload :TranslationHelper, 'action_view/helpers/translation_helper'
autoload :UrlHelper, 'action_view/helpers/url_helper'
def self.included(base)
base.extend(ClassMethods)
end

View File

@@ -1,11 +1,6 @@
# Legacy TemplateHandler stub
module ActionView
module TemplateHandlers
module Compilable
end
end
class TemplateHandler
def self.call(template)
new.compile(template)

View File

@@ -1,10 +1,13 @@
require 'action_view/template_handler'
require 'action_view/template_handlers/builder'
require 'action_view/template_handlers/erb'
require 'action_view/template_handlers/rjs'
module ActionView #:nodoc:
module TemplateHandlers #:nodoc:
autoload :ERB, 'action_view/template_handlers/erb'
autoload :RJS, 'action_view/template_handlers/rjs'
autoload :Builder, 'action_view/template_handlers/builder'
# Legacy Compilable stub
module Compilable
end
def self.extended(base)
base.register_default_template_handler :erb, TemplateHandlers::ERB
base.register_template_handler :rjs, TemplateHandlers::RJS

View File

@@ -1,42 +1,3 @@
require 'erb'
class ERB
module Util
HTML_ESCAPE = { '&' => '&amp;', '>' => '&gt;', '<' => '&lt;', '"' => '&quot;' }
JSON_ESCAPE = { '&' => '\u0026', '>' => '\u003E', '<' => '\u003C' }
# A utility method for escaping HTML tag characters.
# This method is also aliased as <tt>h</tt>.
#
# In your ERb templates, use this method to escape any unsafe content. For example:
# <%=h @person.name %>
#
# ==== Example:
# puts html_escape("is a > 0 & a < 10?")
# # => is a &gt; 0 &amp; a &lt; 10?
def html_escape(s)
s.to_s.gsub(/[&"><]/) { |special| HTML_ESCAPE[special] }
end
# A utility method for escaping HTML entities in JSON strings.
# This method is also aliased as <tt>j</tt>.
#
# In your ERb templates, use this method to escape any HTML entities:
# <%=j @person.to_json %>
#
# ==== Example:
# puts json_escape("is a > 0 & a < 10?")
# # => is a \u003E 0 \u0026 a \u003C 10?
def json_escape(s)
s.to_s.gsub(/[&"><]/) { |special| JSON_ESCAPE[special] }
end
alias j json_escape
module_function :j
module_function :json_escape
end
end
module ActionView
module TemplateHandlers
class ERB < TemplateHandler