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

Changeset 8619

Show
Ignore:
Timestamp:
01/10/08 03:17:20 (8 months ago)
Author:
bitsweat
Message:

Move fragment caching from special helper methods to TemplateHandler. Closes #10754 [Josh Peek]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/actionpack/lib/action_controller/caching/fragments.rb

    r8546 r8619  
    7575      end 
    7676 
    77       # Called by CacheHelper#cache 
    78       def cache_rxml_fragment(block, name = {}, options = nil) #:nodoc: 
    79         fragment_for(block, name, options) do 
    80           eval('xml.target!', block.binding) 
    81         end 
    82       end 
    83  
    84       # Called by CacheHelper#cache 
    85       def cache_rjs_fragment(block, name = {}, options = nil) #:nodoc: 
    86         fragment_for(block, name, options) do 
    87           begin 
    88             debug_mode, ActionView::Base.debug_rjs = ActionView::Base.debug_rjs, false 
    89             eval('page.to_s', block.binding) 
    90           ensure 
    91             ActionView::Base.debug_rjs = debug_mode 
    92           end 
    93         end 
    94       end 
    95  
    96       # Called by CacheHelper#cache 
    97       def cache_erb_fragment(block, name = {}, options = nil) #:nodoc: 
    98         fragment_for(block, name, options) do 
    99           eval(ActionView::Base.erb_variable, block.binding) 
    100         end 
    101       end 
    102  
    10377      # Writes <tt>content</tt> to the location signified by <tt>key</tt> (see <tt>expire_fragment</tt> for acceptable formats) 
    10478      def write_fragment(key, content, options = nil) 
  • trunk/actionpack/lib/action_view/helpers/cache_helper.rb

    r8614 r8619  
    3434      def cache(name = {}, options = nil, &block) 
    3535        template_extension = find_template_extension_for(first_render)[/\.(\w+)$/, 1].to_sym 
    36  
    37         case template_extension 
    38         when :erb, :rhtml 
    39           @controller.cache_erb_fragment(block, name, options) 
    40         when :rjs 
    41           @controller.cache_rjs_fragment(block, name, options) 
    42         when :builder, :rxml 
    43           @controller.cache_rxml_fragment(block, name, options) 
    44         else 
    45           # Give template engine writers a hook to implement their own caching approach 
    46           if @controller.respond_to?("cache_#{template_extension}_fragment") 
    47             @controller.send!("cache_#{template_extension}_fragment", block, name, options) 
    48           else 
    49             # Let the ERb approach be the default one if the plugin doesn't specify it's own 
    50             @controller.cache_erb_fragment(block, name, options) 
    51           end 
    52         end 
     36        handler = Base.handler_for_extension(template_extension) 
     37        handler.new(@controller).cache_fragment(block, name, options) 
    5338      end 
    5439    end 
  • trunk/actionpack/lib/action_view/template_handler.rb

    r8374 r8619  
    1414    def compile(template) 
    1515    end 
     16 
     17    # Called by CacheHelper#cache 
     18    def cache_fragment(block, name = {}, options = nil) 
     19    end 
    1620  end 
    1721end 
  • trunk/actionpack/lib/action_view/template_handlers/builder.rb

    r8374 r8619  
    1515        "\nxml.target!\n" 
    1616      end 
     17 
     18      def cache_fragment(block, name = {}, options = nil) 
     19        @view.fragment_for(block, name, options) do 
     20          eval('xml.target!', block.binding) 
     21        end 
     22      end 
    1723    end 
    1824  end 
  • trunk/actionpack/lib/action_view/template_handlers/erb.rb

    r8578 r8619  
    2626        ::ERB.new(template, nil, @view.erb_trim_mode).src 
    2727      end 
     28 
     29      def cache_fragment(block, name = {}, options = nil) #:nodoc: 
     30        @view.fragment_for(block, name, options) do 
     31          eval(ActionView::Base.erb_variable, block.binding) 
     32        end 
     33      end 
    2834    end 
    2935  end 
  • trunk/actionpack/lib/action_view/template_handlers/rjs.rb

    r8374 r8619  
    1010        "update_page do |page|\n#{template}\nend" 
    1111      end 
     12 
     13      def cache_fragment(block, name = {}, options = nil) #:nodoc: 
     14        @view.fragment_for(block, name, options) do 
     15          begin 
     16            debug_mode, ActionView::Base.debug_rjs = ActionView::Base.debug_rjs, false 
     17            eval('page.to_s', block.binding) 
     18          ensure 
     19            ActionView::Base.debug_rjs = debug_mode 
     20          end 
     21        end 
     22      end 
    1223    end 
    1324  end 
  • trunk/actionpack/test/controller/caching_test.rb

    r8564 r8619  
    446446 
    447447    assert_equal( 'generated till now -> fragment content', 
    448                   @controller.cache_erb_fragment(Proc.new{ }, 'expensive')) 
     448                  ActionView::TemplateHandlers::ERB.new(@controller).cache_fragment(Proc.new{ }, 'expensive')) 
    449449  end 
    450450 
     
    455455 
    456456    assert_equal( 'generated till now -> fragment content', 
    457                   @controller.cache_rxml_fragment(Proc.new{ }, 'expensive')) 
     457                  ActionView::TemplateHandlers::Builder.new(@controller).cache_fragment(Proc.new{ }, 'expensive')) 
    458458  end 
    459459 
     
    463463 
    464464    assert_equal( 'generated till now -> fragment content', 
    465                   @controller.cache_rjs_fragment(Proc.new{ }, 'expensive')) 
     465                  ActionView::TemplateHandlers::RJS.new(@controller).cache_fragment(Proc.new{ }, 'expensive')) 
    466466  end 
    467467 
     
    473473      debug_mode, ActionView::Base.debug_rjs = ActionView::Base.debug_rjs, true 
    474474      assert_equal( 'generated till now -> fragment content', 
    475                     @controller.cache_rjs_fragment(Proc.new{ }, 'expensive')) 
     475                    ActionView::TemplateHandlers::RJS.new(@controller).cache_fragment(Proc.new{ }, 'expensive')) 
    476476      assert ActionView::Base.debug_rjs 
    477477    ensure