Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 8981

Show
Ignore:
Timestamp:
03/05/08 02:03:24 (1 year ago)
Author:
pratik
Message:

Moved template handlers related code from ActionView::Base to ActionView::Template

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/actionpack/CHANGELOG

    r8980 r8981  
    11*SVN* 
     2 
     3* Moved template handlers related code from ActionView::Base to ActionView::Template. [Pratik] 
    24 
    35* Tests for div_for and content_tag_for helpers. Closes #11223 [thechrisoshow] 
  • trunk/actionpack/lib/action_controller/base.rb

    r8862 r8981  
    66require 'action_controller/url_rewriter' 
    77require 'action_controller/status_codes' 
    8 require 'action_view/template' 
    9 require 'action_view/template_finder' 
     8require 'action_view' 
    109require 'drb' 
    1110require 'set' 
  • trunk/actionpack/lib/action_view/base.rb

    r8976 r8981  
    184184     
    185185    delegate :request_forgery_protection_token, :to => :controller 
    186  
    187     @@template_handlers = HashWithIndifferentAccess.new 
    188186  
    189187    module CompiledTemplates #:nodoc: 
     
    201199    cattr_reader :computed_public_paths 
    202200    @@computed_public_paths = {} 
    203  
    204     @@template_handlers = {} 
    205     @@default_template_handlers = nil 
    206201 
    207202    class ObjectWrapper < Struct.new(:value) #:nodoc: 
     
    218213      end 
    219214    end 
    220  
    221     # Register a class that knows how to handle template files with the given 
    222     # extension. This can be used to implement new template types. 
    223     # The constructor for the class must take the ActiveView::Base instance 
    224     # as a parameter, and the class must implement a #render method that 
    225     # takes the contents of the template to render as well as the Hash of 
    226     # local assigns available to the template. The #render method ought to 
    227     # return the rendered template as a string. 
    228     def self.register_template_handler(extension, klass) 
    229       @@template_handlers[extension.to_sym] = klass 
    230       TemplateFinder.update_extension_cache_for(extension.to_s) 
    231     end 
    232  
    233     def self.template_handler_extensions 
    234       @@template_handlers.keys.map(&:to_s).sort 
    235     end 
    236  
    237     def self.register_default_template_handler(extension, klass) 
    238       register_template_handler(extension, klass) 
    239       @@default_template_handlers = klass 
    240     end 
    241  
    242     def self.handler_class_for_extension(extension) 
    243       (extension && @@template_handlers[extension.to_sym]) || @@default_template_handlers 
    244     end 
    245  
    246     register_default_template_handler :erb, TemplateHandlers::ERB 
    247     register_template_handler :rjs, TemplateHandlers::RJS 
    248     register_template_handler :builder, TemplateHandlers::Builder 
    249  
    250     # TODO: Depreciate old template extensions 
    251     register_template_handler :rhtml, TemplateHandlers::ERB 
    252     register_template_handler :rxml, TemplateHandlers::Builder 
    253215 
    254216    def initialize(view_paths = [], assigns_for_first_render = {}, controller = nil)#:nodoc: 
  • trunk/actionpack/lib/action_view/helpers/cache_helper.rb

    r8791 r8981  
    3333      #    <% end %> 
    3434      def cache(name = {}, options = nil, &block) 
    35         handler = Base.handler_class_for_extension(current_render_extension.to_sym) 
     35        handler = Template.handler_class_for_extension(current_render_extension.to_sym) 
    3636        handler.new(@controller).cache_fragment(block, name, options) 
    3737      end 
  • trunk/actionpack/lib/action_view/template_finder.rb

    r8809 r8981  
    5151 
    5252      def template_handler_extensions 
    53         ActionView::Base.template_handler_extensions 
     53        ActionView::Template.template_handler_extensions 
    5454      end 
    5555 
  • trunk/actionpack/lib/action_view/template.rb

    r8976 r8981  
    2020      end 
    2121      @locals = locals || {} 
    22       @handler = @view.class.handler_class_for_extension(@extension).new(@view) 
     22      @handler = self.class.handler_class_for_extension(@extension).new(@view) 
    2323    end 
    2424     
     
    7474    end 
    7575 
     76    # Template Handlers 
     77     
     78    @@template_handlers = HashWithIndifferentAccess.new 
     79    @@default_template_handlers = nil 
     80     
     81    # Register a class that knows how to handle template files with the given 
     82    # extension. This can be used to implement new template types. 
     83    # The constructor for the class must take the ActiveView::Base instance 
     84    # as a parameter, and the class must implement a #render method that 
     85    # takes the contents of the template to render as well as the Hash of 
     86    # local assigns available to the template. The #render method ought to 
     87    # return the rendered template as a string. 
     88    def self.register_template_handler(extension, klass) 
     89      @@template_handlers[extension.to_sym] = klass 
     90      TemplateFinder.update_extension_cache_for(extension.to_s) 
     91    end 
     92 
     93    def self.template_handler_extensions 
     94      @@template_handlers.keys.map(&:to_s).sort 
     95    end 
     96 
     97    def self.register_default_template_handler(extension, klass) 
     98      register_template_handler(extension, klass) 
     99      @@default_template_handlers = klass 
     100    end 
     101 
     102    def self.handler_class_for_extension(extension) 
     103      (extension && @@template_handlers[extension.to_sym]) || @@default_template_handlers 
     104    end 
     105 
     106    register_default_template_handler :erb, TemplateHandlers::ERB 
     107    register_template_handler :rjs, TemplateHandlers::RJS 
     108    register_template_handler :builder, TemplateHandlers::Builder 
     109 
     110    # TODO: Depreciate old template extensions 
     111    register_template_handler :rhtml, TemplateHandlers::ERB 
     112    register_template_handler :rxml, TemplateHandlers::Builder 
     113     
    76114  end 
    77115end 
  • trunk/actionpack/test/controller/custom_handler_test.rb

    r8976 r8981  
    1515class CustomHandlerTest < Test::Unit::TestCase 
    1616  def setup 
    17     ActionView::Base.register_template_handler "foo", CustomHandler 
    18     ActionView::Base.register_template_handler :foo2, CustomHandler 
     17    ActionView::Template.register_template_handler "foo", CustomHandler 
     18    ActionView::Template.register_template_handler :foo2, CustomHandler 
    1919    @view = ActionView::Base.new 
    2020  end 
  • trunk/actionpack/test/controller/layout_test.rb

    r8976 r8981  
    4141end 
    4242 
    43 ActionView::Base::register_template_handler :mab, MabView 
     43ActionView::Template::register_template_handler :mab, MabView 
    4444 
    4545class LayoutAutoDiscoveryTest < Test::Unit::TestCase 
  • trunk/actionpack/test/template/compiled_templates_test.rb

    r8976 r8981  
    9292      ts = ActionView::Template.new(v, @s, false, {}) 
    9393 
    94       @handler_class = ActionView::Base.handler_class_for_extension(:rhtml) 
     94      @handler_class = ActionView::Template.handler_class_for_extension(:rhtml) 
    9595      @handler       = @handler_class.new(v) 
    9696 
  • trunk/actionpack/test/template/template_finder_test.rb

    r8959 r8981  
    77  def setup 
    88    ActionView::TemplateFinder.process_view_paths(LOAD_PATH_ROOT) 
    9     ActionView::Base::register_template_handler :mab, Class.new(ActionView::TemplateHandler) 
     9    ActionView::Template::register_template_handler :mab, Class.new(ActionView::TemplateHandler) 
    1010    @template = ActionView::Base.new 
    1111    @finder = ActionView::TemplateFinder.new(@template, LOAD_PATH_ROOT) 
     
    3636    def test_should_update_extension_cache_when_template_handler_is_registered 
    3737      ActionView::TemplateFinder.expects(:update_extension_cache_for).with("funky") 
    38       ActionView::Base::register_template_handler :funky, Class.new(ActionView::TemplateHandler) 
     38      ActionView::Template::register_template_handler :funky, Class.new(ActionView::TemplateHandler) 
    3939    end 
    4040