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

Changeset 8624

Show
Ignore:
Timestamp:
01/11/08 04:45:06 (8 months ago)
Author:
nzkoz
Message:
  • Pass around handler instances, not their classes [Koz]
  • Move compilation, rendering and 'compilable?' checks into the Handlers [Koz]
  • Remove delegate_* methods as the handler is now an instance [Koz]
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/actionpack/lib/action_view/base.rb

    r8541 r8624  
    253253    end 
    254254 
    255     def self.handler_for_extension(extension) 
     255    def self.handler_class_for_extension(extension) 
    256256      (extension && @@template_handlers[extension.to_sym]) || @@default_template_handlers 
    257257    end 
     
    362362    # The hash in <tt>local_assigns</tt> is made available as local variables. 
    363363    def render_template(template_extension, template, file_path = nil, local_assigns = {}) #:nodoc: 
    364       handler = self.class.handler_for_extension(template_extension
    365  
    366       if template_handler_is_compilable?(handler) 
     364      handler = self.class.handler_class_for_extension(template_extension).new(self
     365 
     366      if handler.compilable? 
    367367        compile_and_render_template(handler, template, file_path, local_assigns) 
    368368      else 
    369369        template ||= read_template_file(file_path, template_extension) # Make sure that a lazyily-read template is loaded. 
    370         delegate_render(handler, template, local_assigns) 
     370        handler.render(template, local_assigns) 
    371371      end 
    372372    end 
     
    512512          @assigns_added = true 
    513513        end 
    514       end 
    515  
    516       def delegate_render(handler, template, local_assigns) 
    517         handler.new(self).render(template, local_assigns) 
    518       end 
    519  
    520       def delegate_compile(handler, template) 
    521         handler.new(self).compile(template) 
    522       end 
    523  
    524       def template_handler_is_compilable?(handler) 
    525         handler.new(self).respond_to?(:compile) 
    526514      end 
    527515 
     
    566554      # Method to create the source code for a given template. 
    567555      def create_template_source(handler, template, render_symbol, locals) 
    568         body = delegate_compile(handler, template) 
     556        body = handler.compile(template) 
    569557 
    570558        @@template_args[render_symbol] ||= {} 
     
    586574 
    587575      def compiled_method_name(handler, template, file_name) 
    588         ['_run', handler.to_s.demodulize.underscore, compiled_method_name_file_path_segment(file_name)].compact.join('_').to_sym 
     576        ['_run', handler.class.to_s.demodulize.underscore, compiled_method_name_file_path_segment(file_name)].compact.join('_').to_sym 
    589577      end 
    590578 
  • trunk/actionpack/lib/action_view/template_handler.rb

    r8619 r8624  
    33    def self.line_offset 
    44      0 
     5    end 
     6 
     7    def self.compilable? 
     8      false 
    59    end 
    610 
     
    1519    end 
    1620 
     21    def compilable? 
     22      self.class.compilable? 
     23    end 
     24 
     25    def line_offset 
     26      self.class.line_offset 
     27    end 
     28 
    1729    # Called by CacheHelper#cache 
    1830    def cache_fragment(block, name = {}, options = nil) 
  • trunk/actionpack/lib/action_view/template_handlers/builder.rb

    r8619 r8624  
    66      def self.line_offset 
    77        2 
     8      end 
     9 
     10      def self.compilable? 
     11        true 
    812      end 
    913 
  • trunk/actionpack/lib/action_view/template_handlers/erb.rb

    r8619 r8624  
    2727      end 
    2828 
     29      def self.compilable? 
     30        true 
     31      end 
     32 
    2933      def cache_fragment(block, name = {}, options = nil) #:nodoc: 
    3034        @view.fragment_for(block, name, options) do 
  • trunk/actionpack/lib/action_view/template_handlers/rjs.rb

    r8619 r8624  
    99        "controller.response.content_type ||= Mime::JS\n" + 
    1010        "update_page do |page|\n#{template}\nend" 
     11      end 
     12 
     13      def self.compilable? 
     14        true 
    1115      end 
    1216 
  • trunk/actionpack/test/controller/custom_handler_test.rb

    r8564 r8624  
    11require 'abstract_unit' 
    22 
    3 class CustomHandler 
     3class CustomHandler < ActionView::TemplateHandler 
    44  def initialize( view ) 
    55    @view = view 
  • trunk/actionpack/test/controller/layout_test.rb

    r8564 r8624  
    3232end 
    3333 
    34 class MabView 
     34class MabView < ActionView::TemplateHandler 
    3535  def initialize(view) 
    3636  end 
     
    6868    assert_equal 'layouts/third_party_template_library', @controller.active_layout 
    6969    assert_equal 'layouts/third_party_template_library', @response.layout 
     70    assert_response :success 
    7071    assert_equal 'Mab', @response.body 
    7172  end 
  • trunk/actionpack/test/template/compiled_templates_test.rb

    r8564 r8624  
    7474 
    7575  uses_mocha 'test_compile_time' do 
    76   def test_compile_time 
    77     t = Time.now 
    7876 
    79     File.open(@a, "w"){|f| f.puts @a} 
    80     File.open(@b, "w"){|f| f.puts @b} 
    81     # windows doesn't support symlinks (even under cygwin) 
    82     windows = (RUBY_PLATFORM =~ /win32/) 
    83     `ln -s #{@a} #{@s}` unless windows 
     77    def test_compile_time 
     78      t = Time.now 
    8479 
    85     v = ActionView::Base.new 
    86     v.base_path = '.' 
    87     v.cache_template_loading = false 
     80      File.open(@a, "w"){|f| f.puts @a} 
     81      File.open(@b, "w"){|f| f.puts @b} 
     82      # windows doesn't support symlinks (even under cygwin) 
     83      windows = (RUBY_PLATFORM =~ /win32/) 
     84      `ln -s #{@a} #{@s}` unless windows 
    8885 
    89     # All templates were created at t+1 
    90     File::Stat.any_instance.expects(:mtime).times(windows ? 2 : 3).returns(t + 1.second) 
     86      v = ActionView::Base.new 
     87      v.base_path = '.' 
     88      v.cache_template_loading = false 
    9189 
    92     # private methods template_changed_since? and compile_template? 
    93     # should report true for all since they have not been compiled 
    94     assert v.send(:template_changed_since?, @a, t) 
    95     assert v.send(:template_changed_since?, @b, t) 
    96     assert v.send(:template_changed_since?, @s, t) unless windows 
     90      # All templates were created at t+1 
     91      File::Stat.any_instance.expects(:mtime).times(windows ? 2 : 3).returns(t + 1.second) 
    9792 
    98     assert v.send(:compile_template?, nil, @a, {}) 
    99     assert v.send(:compile_template?, nil, @b, {}) 
    100     assert v.send(:compile_template?, nil, @s, {}) unless windows 
     93      # private methods template_changed_since? and compile_template? 
     94      # should report true for all since they have not been compiled 
     95      assert v.send(:template_changed_since?, @a, t) 
     96      assert v.send(:template_changed_since?, @b, t) 
     97      assert v.send(:template_changed_since?, @s, t) unless windows 
    10198 
    102     @handler = ActionView::Base.handler_for_extension(:rhtml) 
     99      assert v.send(:compile_template?, nil, @a, {}) 
     100      assert v.send(:compile_template?, nil, @b, {}) 
     101      assert v.send(:compile_template?, nil, @s, {}) unless windows 
    103102 
    104     # All templates are rendered at t+2 
    105     Time.expects(:now).times(windows ? 2 : 3).returns(t + 2.seconds) 
    106     v.send(:compile_and_render_template, @handler, '', @a) 
    107     v.send(:compile_and_render_template, @handler, '', @b) 
    108     v.send(:compile_and_render_template, @handler, '', @s) unless windows 
    109     a_n = v.method_names[@a] 
    110     b_n = v.method_names[@b] 
    111     s_n = v.method_names[@s] unless windows 
    112     # all of the files have changed since last compile 
    113     assert v.compile_time[a_n] > t 
    114     assert v.compile_time[b_n] > t 
    115     assert v.compile_time[s_n] > t unless windows 
     103      @handler_class = ActionView::Base.handler_class_for_extension(:rhtml) 
     104      @handler       = @handler_class.new(v) 
     105      # All templates are rendered at t+2 
     106      Time.expects(:now).times(windows ? 2 : 3).returns(t + 2.seconds) 
     107      v.send(:compile_and_render_template, @handler, '', @a) 
     108      v.send(:compile_and_render_template, @handler, '', @b) 
     109      v.send(:compile_and_render_template, @handler, '', @s) unless windows 
     110      a_n = v.method_names[@a] 
     111      b_n = v.method_names[@b] 
     112      s_n = v.method_names[@s] unless windows 
     113      # all of the files have changed since last compile 
     114      assert v.compile_time[a_n] > t 
     115      assert v.compile_time[b_n] > t 
     116      assert v.compile_time[s_n] > t unless windows 
    116117 
    117     # private methods template_changed_since? and compile_template? 
    118     # should report false for all since none have changed since compile 
    119     File::Stat.any_instance.expects(:mtime).times(windows ? 6 : 12).returns(t + 1.second) 
    120     assert !v.send(:template_changed_since?, @a, v.compile_time[a_n]) 
    121     assert !v.send(:template_changed_since?, @b, v.compile_time[b_n]) 
    122     assert !v.send(:template_changed_since?, @s, v.compile_time[s_n]) unless windows 
    123     assert !v.send(:compile_template?, nil, @a, {}) 
    124     assert !v.send(:compile_template?, nil, @b, {}) 
    125     assert !v.send(:compile_template?, nil, @s, {}) unless windows 
    126     v.send(:compile_and_render_template, @handler, '', @a) 
    127     v.send(:compile_and_render_template, @handler, '', @b) 
    128     v.send(:compile_and_render_template, @handler, '', @s)  unless windows 
    129     # none of the files have changed since last compile 
    130     assert v.compile_time[a_n] < t + 3.seconds 
    131     assert v.compile_time[b_n] < t + 3.seconds 
    132     assert v.compile_time[s_n] < t + 3.seconds  unless windows 
     118      # private methods template_changed_since? and compile_template? 
     119      # should report false for all since none have changed since compile 
     120      File::Stat.any_instance.expects(:mtime).times(windows ? 6 : 12).returns(t + 1.second) 
     121      assert !v.send(:template_changed_since?, @a, v.compile_time[a_n]) 
     122      assert !v.send(:template_changed_since?, @b, v.compile_time[b_n]) 
     123      assert !v.send(:template_changed_since?, @s, v.compile_time[s_n]) unless windows 
     124      assert !v.send(:compile_template?, nil, @a, {}) 
     125      assert !v.send(:compile_template?, nil, @b, {}) 
     126      assert !v.send(:compile_template?, nil, @s, {}) unless windows 
     127      v.send(:compile_and_render_template, @handler, '', @a) 
     128      v.send(:compile_and_render_template, @handler, '', @b) 
     129      v.send(:compile_and_render_template, @handler, '', @s)  unless windows 
     130      # none of the files have changed since last compile 
     131      assert v.compile_time[a_n] < t + 3.seconds 
     132      assert v.compile_time[b_n] < t + 3.seconds 
     133      assert v.compile_time[s_n] < t + 3.seconds  unless windows 
    133134 
    134     `rm #{@s}; ln -s #{@b} #{@s}` unless windows 
    135     # private methods template_changed_since? and compile_template? 
    136     # should report true for symlink since it has changed since compile 
     135      `rm #{@s}; ln -s #{@b} #{@s}` unless windows 
     136      # private methods template_changed_since? and compile_template? 
     137      # should report true for symlink since it has changed since compile 
    137138     
    138     # t + 3.seconds is for the symlink 
    139     File::Stat.any_instance.expects(:mtime).times(windows ? 6 : 9).returns( 
    140       *(windows ? [ t + 1.second, t + 1.second ] : 
    141         [ t + 1.second, t + 1.second, t + 3.second ]) * 3) 
    142     assert !v.send(:template_changed_since?, @a, v.compile_time[a_n]) 
    143     assert !v.send(:template_changed_since?, @b, v.compile_time[b_n]) 
    144     assert v.send(:template_changed_since?, @s, v.compile_time[s_n]) unless windows 
    145     assert !v.send(:compile_template?, nil, @a, {}) 
    146     assert !v.send(:compile_template?, nil, @b, {}) 
    147     assert v.send(:compile_template?, nil, @s, {}) unless windows 
     139      # t + 3.seconds is for the symlink 
     140      File::Stat.any_instance.expects(:mtime).times(windows ? 6 : 9).returns( 
     141        *(windows ? [ t + 1.second, t + 1.second ] : 
     142          [ t + 1.second, t + 1.second, t + 3.second ]) * 3) 
     143      assert !v.send(:template_changed_since?, @a, v.compile_time[a_n]) 
     144      assert !v.send(:template_changed_since?, @b, v.compile_time[b_n]) 
     145      assert v.send(:template_changed_since?, @s, v.compile_time[s_n]) unless windows 
     146      assert !v.send(:compile_template?, nil, @a, {}) 
     147      assert !v.send(:compile_template?, nil, @b, {}) 
     148      assert v.send(:compile_template?, nil, @s, {}) unless windows 
    148149 
    149     # Only the symlink template gets rendered at t+3 
    150     Time.stubs(:now).returns(t + 3.seconds) unless windows 
    151     v.send(:compile_and_render_template, @handler, '', @a) 
    152     v.send(:compile_and_render_template, @handler, '', @b) 
    153     v.send(:compile_and_render_template, @handler, '', @s) unless windows 
    154     # the symlink has changed since last compile 
    155     assert v.compile_time[a_n] < t + 3.seconds 
    156     assert v.compile_time[b_n] < t + 3.seconds 
    157     assert_equal v.compile_time[s_n], t + 3.seconds unless windows 
     150      # Only the symlink template gets rendered at t+3 
     151      Time.stubs(:now).returns(t + 3.seconds) unless windows 
     152      v.send(:compile_and_render_template, @handler, '', @a) 
     153      v.send(:compile_and_render_template, @handler, '', @b) 
     154      v.send(:compile_and_render_template, @handler, '', @s) unless windows 
     155      # the symlink has changed since last compile 
     156      assert v.compile_time[a_n] < t + 3.seconds 
     157      assert v.compile_time[b_n] < t + 3.seconds 
     158      assert_equal v.compile_time[s_n], t + 3.seconds unless windows 
    158159 
    159     FileUtils.touch @b 
    160     # private methods template_changed_since? and compile_template? 
    161     # should report true for symlink and file at end of symlink 
    162     # since it has changed since last compile 
    163    
    164     # t+4 is for @b and also for the file that @s points to, which is @b 
    165     File::Stat.any_instance.expects(:mtime).times(windows ? 6 : 12).returns( 
    166       *(windows ? [ t + 1.second, t + 4.seconds ] : 
    167         [ t + 1.second, t + 4.seconds, t + 3.second, t + 4.seconds ]) * 3) 
    168     assert !v.send(:template_changed_since?, @a, v.compile_time[a_n]) 
    169     assert v.send(:template_changed_since?, @b, v.compile_time[b_n]) 
    170     assert v.send(:template_changed_since?, @s, v.compile_time[s_n]) unless windows 
    171     assert !v.send(:compile_template?, nil, @a, {}) 
    172     assert v.send(:compile_template?, nil, @b, {}) 
    173     assert v.send(:compile_template?, nil, @s, {}) unless windows 
     160      FileUtils.touch @b 
     161      # private methods template_changed_since? and compile_template? 
     162      # should report true for symlink and file at end of symlink 
     163      # since it has changed since last compile 
     164     
     165      # t+4 is for @b and also for the file that @s points to, which is @b 
     166      File::Stat.any_instance.expects(:mtime).times(windows ? 6 : 12).returns( 
     167        *(windows ? [ t + 1.second, t + 4.seconds ] : 
     168          [ t + 1.second, t + 4.seconds, t + 3.second, t + 4.seconds ]) * 3) 
     169      assert !v.send(:template_changed_since?, @a, v.compile_time[a_n]) 
     170      assert v.send(:template_changed_since?, @b, v.compile_time[b_n]) 
     171      assert v.send(:template_changed_since?, @s, v.compile_time[s_n]) unless windows 
     172      assert !v.send(:compile_template?, nil, @a, {}) 
     173      assert v.send(:compile_template?, nil, @b, {}) 
     174      assert v.send(:compile_template?, nil, @s, {}) unless windows 
    174175 
    175     Time.expects(:now).times(windows ? 1 : 2).returns(t + 5.seconds) 
    176     v.send(:compile_and_render_template, @handler, '', @a) 
    177     v.send(:compile_and_render_template, @handler, '', @b) 
    178     v.send(:compile_and_render_template, @handler, '', @s) unless windows 
    179     # the file at the end of the symlink has changed since last compile 
    180     # both the symlink and the file at the end of it should be recompiled 
    181     assert v.compile_time[a_n] < t + 5.seconds 
    182     assert_equal v.compile_time[b_n], t + 5.seconds 
    183     assert_equal v.compile_time[s_n], t + 5.seconds unless windows 
    184   end 
     176      Time.expects(:now).times(windows ? 1 : 2).returns(t + 5.seconds) 
     177      v.send(:compile_and_render_template, @handler, '', @a) 
     178      v.send(:compile_and_render_template, @handler, '', @b) 
     179      v.send(:compile_and_render_template, @handler, '', @s) unless windows 
     180      # the file at the end of the symlink has changed since last compile 
     181      # both the symlink and the file at the end of it should be recompiled 
     182      assert v.compile_time[a_n] < t + 5.seconds 
     183      assert_equal v.compile_time[b_n], t + 5.seconds 
     184      assert_equal v.compile_time[s_n], t + 5.seconds unless windows 
     185    end 
    185186  end 
    186187end