From 2c01f2b4e9d4a95bb2baca8ae57209eb10aa78b2 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Sun, 23 Nov 2008 13:42:07 -0600 Subject: use autoload instead of explicit requires for ActionView --- actionpack/lib/action_view/base.rb | 2 +- actionpack/lib/action_view/erb/util.rb | 38 +++++++++++++++++++++ actionpack/lib/action_view/helpers.rb | 28 +++++++++++++--- actionpack/lib/action_view/template_handler.rb | 5 --- actionpack/lib/action_view/template_handlers.rb | 13 +++++--- .../lib/action_view/template_handlers/erb.rb | 39 ---------------------- 6 files changed, 70 insertions(+), 55 deletions(-) create mode 100644 actionpack/lib/action_view/erb/util.rb (limited to 'actionpack/lib/action_view') diff --git a/actionpack/lib/action_view/base.rb b/actionpack/lib/action_view/base.rb index 0d3752d875..7697848713 100644 --- a/actionpack/lib/action_view/base.rb +++ b/actionpack/lib/action_view/base.rb @@ -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 diff --git a/actionpack/lib/action_view/erb/util.rb b/actionpack/lib/action_view/erb/util.rb new file mode 100644 index 0000000000..3c77c5ce76 --- /dev/null +++ b/actionpack/lib/action_view/erb/util.rb @@ -0,0 +1,38 @@ +require 'erb' + +class ERB + module Util + HTML_ESCAPE = { '&' => '&', '>' => '>', '<' => '<', '"' => '"' } + JSON_ESCAPE = { '&' => '\u0026', '>' => '\u003E', '<' => '\u003C' } + + # A utility method for escaping HTML tag characters. + # This method is also aliased as h. + # + # 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 > 0 & a < 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 j. + # + # 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 diff --git a/actionpack/lib/action_view/helpers.rb b/actionpack/lib/action_view/helpers.rb index ff97df204c..de8682560f 100644 --- a/actionpack/lib/action_view/helpers.rb +++ b/actionpack/lib/action_view/helpers.rb @@ -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 diff --git a/actionpack/lib/action_view/template_handler.rb b/actionpack/lib/action_view/template_handler.rb index d7e7c9b199..5efe9459b5 100644 --- a/actionpack/lib/action_view/template_handler.rb +++ b/actionpack/lib/action_view/template_handler.rb @@ -1,11 +1,6 @@ # Legacy TemplateHandler stub module ActionView - module TemplateHandlers - module Compilable - end - end - class TemplateHandler def self.call(template) new.compile(template) diff --git a/actionpack/lib/action_view/template_handlers.rb b/actionpack/lib/action_view/template_handlers.rb index 6c8aa4c2a7..1052c4e75c 100644 --- a/actionpack/lib/action_view/template_handlers.rb +++ b/actionpack/lib/action_view/template_handlers.rb @@ -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 diff --git a/actionpack/lib/action_view/template_handlers/erb.rb b/actionpack/lib/action_view/template_handlers/erb.rb index 3def949f1e..f8d3da15be 100644 --- a/actionpack/lib/action_view/template_handlers/erb.rb +++ b/actionpack/lib/action_view/template_handlers/erb.rb @@ -1,42 +1,3 @@ -require 'erb' - -class ERB - module Util - HTML_ESCAPE = { '&' => '&', '>' => '>', '<' => '<', '"' => '"' } - JSON_ESCAPE = { '&' => '\u0026', '>' => '\u003E', '<' => '\u003C' } - - # A utility method for escaping HTML tag characters. - # This method is also aliased as h. - # - # 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 > 0 & a < 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 j. - # - # 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 -- cgit v1.2.3 From bc4d05b244c78f03ade51d8b95f16dd48ceb63d3 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Sun, 23 Nov 2008 13:57:01 -0600 Subject: A back support for legacy TemplateHandler#render API --- actionpack/lib/action_view/template_handler.rb | 29 +++++++++++++++++++++++-- actionpack/lib/action_view/template_handlers.rb | 4 ---- 2 files changed, 27 insertions(+), 6 deletions(-) (limited to 'actionpack/lib/action_view') diff --git a/actionpack/lib/action_view/template_handler.rb b/actionpack/lib/action_view/template_handler.rb index 5efe9459b5..672da0ed2b 100644 --- a/actionpack/lib/action_view/template_handler.rb +++ b/actionpack/lib/action_view/template_handler.rb @@ -1,9 +1,34 @@ # Legacy TemplateHandler stub - module ActionView + module TemplateHandlers #:nodoc: + module Compilable + def self.included(base) + base.extend(ClassMethods) + end + + module ClassMethods + def call(template) + new.compile(template) + end + end + + def compile(template) + raise "Need to implement #{self.class.name}#compile(template)" + end + end + end + class TemplateHandler def self.call(template) - new.compile(template) + "#{name}.new(self).render(template, local_assigns)" + end + + def initialize(view = nil) + @view = view + end + + def render(template, local_assigns) + raise "Need to implement #{self.class.name}#render(template, local_assigns)" end end end diff --git a/actionpack/lib/action_view/template_handlers.rb b/actionpack/lib/action_view/template_handlers.rb index 1052c4e75c..d06ddd5fb5 100644 --- a/actionpack/lib/action_view/template_handlers.rb +++ b/actionpack/lib/action_view/template_handlers.rb @@ -4,10 +4,6 @@ module ActionView #:nodoc: 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 -- cgit v1.2.3 From d36158794b19ee8ea49d74061218b37d4301f0f9 Mon Sep 17 00:00:00 2001 From: Yaroslav Markin Date: Sun, 23 Nov 2008 17:30:59 +0300 Subject: Add i18n for number_to_human_size() helper storage units. Translation key is number.human.storage_units. [#1448 state:committed] Signed-off-by: Jeremy Kemper --- actionpack/lib/action_view/helpers/number_helper.rb | 7 +++---- actionpack/lib/action_view/locale/en.yml | 1 + 2 files changed, 4 insertions(+), 4 deletions(-) (limited to 'actionpack/lib/action_view') diff --git a/actionpack/lib/action_view/helpers/number_helper.rb b/actionpack/lib/action_view/helpers/number_helper.rb index 77f19b36a6..3e734ccaab 100644 --- a/actionpack/lib/action_view/helpers/number_helper.rb +++ b/actionpack/lib/action_view/helpers/number_helper.rb @@ -220,8 +220,6 @@ module ActionView end end - STORAGE_UNITS = %w( Bytes KB MB GB TB ).freeze - # Formats the bytes in +size+ into a more understandable representation # (e.g., giving it 1500 yields 1.5 KB). This method is useful for # reporting file sizes to users. This method returns nil if @@ -257,6 +255,7 @@ module ActionView defaults = I18n.translate(:'number.format', :locale => options[:locale], :raise => true) rescue {} human = I18n.translate(:'number.human.format', :locale => options[:locale], :raise => true) rescue {} defaults = defaults.merge(human) + storage_units = I18n.translate(:'number.human.storage_units', :locale => options[:locale], :raise => true) unless args.empty? ActiveSupport::Deprecation.warn('number_to_human_size takes an option hash ' + @@ -268,12 +267,12 @@ module ActionView separator ||= (options[:separator] || defaults[:separator]) delimiter ||= (options[:delimiter] || defaults[:delimiter]) - max_exp = STORAGE_UNITS.size - 1 + max_exp = storage_units.size - 1 number = Float(number) exponent = (Math.log(number) / Math.log(1024)).to_i # Convert to base 1024 exponent = max_exp if exponent > max_exp # we need this to avoid overflow for the highest unit number /= 1024 ** exponent - unit = STORAGE_UNITS[exponent] + unit = storage_units[exponent] begin escaped_separator = Regexp.escape(separator) diff --git a/actionpack/lib/action_view/locale/en.yml b/actionpack/lib/action_view/locale/en.yml index 002226fd9c..9542b035aa 100644 --- a/actionpack/lib/action_view/locale/en.yml +++ b/actionpack/lib/action_view/locale/en.yml @@ -44,6 +44,7 @@ # separator: delimiter: "" precision: 1 + storage_units: [Bytes, KB, MB, GB, TB] # Used in distance_of_time_in_words(), distance_of_time_in_words_to_now(), time_ago_in_words() datetime: -- cgit v1.2.3 From d75a2345015046e08f8191748f0e246e1b9f9703 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Sun, 23 Nov 2008 15:15:05 -0600 Subject: simplify console with helpers --- actionpack/lib/action_view/helpers.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'actionpack/lib/action_view') diff --git a/actionpack/lib/action_view/helpers.rb b/actionpack/lib/action_view/helpers.rb index de8682560f..693ab7c2e0 100644 --- a/actionpack/lib/action_view/helpers.rb +++ b/actionpack/lib/action_view/helpers.rb @@ -42,6 +42,7 @@ module ActionView #:nodoc: include FormHelper include FormOptionsHelper include FormTagHelper + include JavaScriptHelper include NumberHelper include PrototypeHelper include RecordIdentificationHelper -- cgit v1.2.3 From 31ce92f7b5784bc5b6a441e88cd734c7b8b1c58f Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Sun, 23 Nov 2008 16:35:13 -0600 Subject: Use autoload instead of explicit requires for ActionController --- actionpack/lib/action_view/test_case.rb | 2 -- 1 file changed, 2 deletions(-) (limited to 'actionpack/lib/action_view') diff --git a/actionpack/lib/action_view/test_case.rb b/actionpack/lib/action_view/test_case.rb index cdea1def92..4ab4ed233f 100644 --- a/actionpack/lib/action_view/test_case.rb +++ b/actionpack/lib/action_view/test_case.rb @@ -1,5 +1,3 @@ -require 'action_controller/test_case' - module ActionView class TestCase < ActiveSupport::TestCase include ActionController::TestCase::Assertions -- cgit v1.2.3 From 2dd0ec48a5068a095e362fad2a77d63b86fdfd95 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sun, 23 Nov 2008 19:12:00 -0800 Subject: Autoload HTML::Document and sanitizers --- actionpack/lib/action_view/helpers/sanitize_helper.rb | 11 +---------- actionpack/lib/action_view/helpers/text_helper.rb | 10 ---------- 2 files changed, 1 insertion(+), 20 deletions(-) (limited to 'actionpack/lib/action_view') diff --git a/actionpack/lib/action_view/helpers/sanitize_helper.rb b/actionpack/lib/action_view/helpers/sanitize_helper.rb index 435ba936e1..200c1d6085 100644 --- a/actionpack/lib/action_view/helpers/sanitize_helper.rb +++ b/actionpack/lib/action_view/helpers/sanitize_helper.rb @@ -1,14 +1,5 @@ require 'action_view/helpers/tag_helper' - -begin - require 'html/document' -rescue LoadError - html_scanner_path = "#{File.dirname(__FILE__)}/../../action_controller/vendor/html-scanner" - if File.directory?(html_scanner_path) - $:.unshift html_scanner_path - require 'html/document' - end -end +require 'action_controller/vendor/html-scanner' module ActionView module Helpers #:nodoc: diff --git a/actionpack/lib/action_view/helpers/text_helper.rb b/actionpack/lib/action_view/helpers/text_helper.rb index 510c1a6a76..506138a735 100644 --- a/actionpack/lib/action_view/helpers/text_helper.rb +++ b/actionpack/lib/action_view/helpers/text_helper.rb @@ -1,15 +1,5 @@ require 'action_view/helpers/tag_helper' -begin - require 'html/document' -rescue LoadError - html_scanner_path = "#{File.dirname(__FILE__)}/../../action_controller/vendor/html-scanner" - if File.directory?(html_scanner_path) - $:.unshift html_scanner_path - require 'html/document' - end -end - module ActionView module Helpers #:nodoc: # The TextHelper module provides a set of methods for filtering, formatting -- cgit v1.2.3