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

Changeset 1588

Show
Ignore:
Timestamp:
07/01/05 23:44:14 (3 years ago)
Author:
stefan
Message:

alternative render implementation

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/performance/actionpack/lib/action_controller/base.rb

    r1377 r1588  
    443443 
    444444      # A unified replacement for the individual renders (work-in-progress). 
    445       def render(options = {}, deprecated_status = nil) 
    446         # puts "Rendering: #{options.inspect}" 
     445      def render(options = nil, deprecated_status = nil) 
     446        # puts "Rendering: #{options.inspect}" if options 
    447447        raise DoubleRenderError, "Can only render or redirect once per action" if performed? 
    448448 
    449449        # Backwards compatibility 
    450         if !options.is_a?(Hash) 
    451           template = options || default_template_name 
    452           assert_existance_of_template_file(template) 
    453           logger.info("Rendering #{template} (#{options[:status]})") if logger 
    454           add_variables_to_assigns 
    455           return render_text(@template.render_file(template, true), options[:status]) 
    456         end 
    457  
    458         if text=options[:text] 
     450        unless options.is_a?(Hash) 
     451          return render_file(options || default_template_name, deprecated_status, true) 
     452        end 
     453 
     454        if text = options[:text] 
    459455          render_text(text, options[:status]) 
    460456 
    461457        else 
    462           add_variables_to_assigns 
    463           if file=options[:file] 
    464             use_full_path = options[:use_full_path] 
    465             assert_existance_of_template_file(file) if use_full_path 
    466             logger.info("Rendering #{file} (#{options[:status]})") if logger 
    467             render_text(@template.render_file(file, use_full_path), options[:status]) 
     458          if file = options[:file] 
     459            render_file(file, options[:status], options[:use_full_path]) 
    468460             
    469           elsif template=options[:template] 
    470             assert_existance_of_template_file(template) 
    471             logger.info("Rendering #{template} (#{options[:status]})") if logger 
    472             render_text(@template.render_file(template, true), options[:status]) 
     461          elsif template = options[:template] 
     462            render_file(template, options[:status], true) 
    473463             
    474           elsif inline=options[:inline] 
    475             render_text(@template.render_template(options[:type] || :rhtml, inline), options[:status]) 
     464          elsif inline = options[:inline] 
     465            render_template(inline, options[:status], options[:type]) 
    476466             
    477           elsif action=options[:action] 
    478             template = default_template_name(action) 
    479             assert_existance_of_template_file(template) 
    480             logger.info("Rendering #{template} (#{options[:status]})") if logger 
    481             render_text(@template.render_file(template, true), options[:status]) 
     467          elsif action_name = options[:action] 
     468            render_action(action_name, options[:status])  
    482469             
    483           elsif partial=options[:partial] 
    484             if collection=options[:collection] 
    485               render_text( 
    486                           @template.render_partial_collection( 
    487                                   partial == true ? default_template_name : partial, 
    488                                   collection, options[:spacer_template], 
    489                                   options[:locals] || {} 
    490                                                            ) || '', 
    491                           options[:status] 
    492                           ) 
     470          elsif partial = options[:partial] 
     471            partial = default_template_name if partial == true 
     472            if collection = options[:collection] 
     473              render_partial_collection(partial, collection, options[:spacer_template], options[:locals], options[:status]) 
    493474            else 
    494               render_text(@template.render_partial( 
    495                             partial == true ? default_template_name : partial, 
    496                             options[:object], options[:locals] || {} 
    497                                                 ), 
    498                           options[:status] 
    499                           ) 
     475              render_partial(partial, options[:object], options[:locals], options[:status]) 
    500476            end 
     477 
    501478          elsif options[:nothing] 
    502479            render_text("", options[:status]) 
    503480             
    504481          else 
    505             template = default_template_name 
    506             assert_existance_of_template_file(template) 
    507             logger.info("Rendering #{file} (#{options[:status]})") if logger 
    508             render_text(@template.render_file(template, true), options[:status]) 
     482            render_file(default_template_name, options[:status], true) 
    509483             
    510484          end 
  • branches/performance/actionpack/lib/action_controller/benchmarking.rb

    r1377 r1588  
    1616    end 
    1717 
    18     def render_with_benchmark(options = {}, deprecated_status = nil) 
     18    def render_with_benchmark(options = nil, deprecated_status = nil) 
    1919      if logger.nil? 
    2020        render_without_benchmark(options, deprecated_status) 
  • branches/performance/actionpack/lib/action_controller/deprecated_renders_and_redirects.rb

    r1377 r1588  
    66      # "#{template_root}/weblog/show_many.rxml". 
    77      def render_action(action_name, status = nil) #:doc: 
    8         render :action => action_name, :status => status 
     8        add_variables_to_assigns 
     9        template = default_template_name(action_name) 
     10        assert_existance_of_template_file(template) 
     11        logger.info("Rendering #{template} (#{status})") if logger 
     12        render_text(@template.render_file(template, true), status) 
    913      end 
    1014 
     
    1317      # "/Users/david/Code/Ruby/template.rxml". 
    1418      def render_file(template_path, status = nil, use_full_path = false) #:doc: 
    15         render :file => template_path, :status => status, :use_full_path => use_full_path 
     19        add_variables_to_assigns 
     20        assert_existance_of_template_file(template_path) if use_full_path 
     21        logger.info("Rendering #{template_path} (#{status})") if logger 
     22        render_text(@template.render_file(template_path, use_full_path), status) 
    1623      end 
    1724 
     
    1926      # you'd call <tt>render_template "Hello, <%= @user.name %>"</tt> to greet the current user. Or if you want to render as Builder 
    2027      # template, you could do <tt>render_template "xml.h1 @user.name", nil, "rxml"</tt>. 
    21       def render_template(template, status = nil, type = "rhtml") #:doc: 
    22         render :inline => template, :status => status, :type => type 
     28      def render_template(template, status = nil, type = :rhtml) #:doc: 
     29        add_variables_to_assigns 
     30        render_text(@template.render_template(type, template), status) 
    2331      end 
    2432 
     
    4654      #     end 
    4755      #   end 
    48       def render_partial(partial_path = default_template_name, object = nil, local_assigns = {}) #:doc: 
    49         render :partial => partial_path, :object => object, :locals => local_assigns 
     56      def render_partial(partial_path = default_template_name, object = nil, local_assigns = nil, status = nil) #:doc: 
     57        add_variables_to_assigns 
     58        render_text(@template.render_partial(partial_path, object, local_assigns), status) 
    5059      end 
    5160 
    5261      # Renders a collection of partials using <tt>partial_name</tt> to iterate over the +collection+. 
    53       def render_partial_collection(partial_name, collection, partial_spacer_template = nil, local_assigns = {})#:doc: 
    54         render :partial => partial_name, :collection => collection, :spacer_template => partial_spacer_template, :locals => local_assigns 
     62      def render_partial_collection(partial_name, collection, partial_spacer_template = nil, local_assigns = nil, status = nil)#:doc: 
     63        add_variables_to_assigns 
     64        render_text(@template.render_partial_collection(partial_name, collection, partial_spacer_template, local_assigns), status) 
    5565      end 
    5666 
  • branches/performance/actionpack/lib/action_controller/layout.rb

    r1377 r1588  
    203203    end 
    204204 
    205     def render_with_a_layout(options = {}, deprecated_status = nil, deprecated_layout = nil) #:nodoc: 
     205    def render_with_a_layout(options = nil, deprecated_status = nil, deprecated_layout = nil) #:nodoc: 
    206206      options = render_with_a_layout_options(options) 
    207207      if (layout = pick_layout(options, deprecated_layout)) 
    208         logger.info("Rendering #{options[:template]} within #{layout}") if logger 
    209  
    210         @content_for_layout = render_with_no_layout(options.merge(:layout => false)) 
     208        logger.info("Rendering #{options} within #{layout}") if logger 
     209 
     210        unless options 
     211          @content_for_layout = render_with_no_layout() 
     212        else 
     213          @content_for_layout = render_with_no_layout(options.merge(:layout => false)) 
     214          deprecated_status = options[:status] || deprecated_status 
     215        end 
    211216        erase_render_results 
    212217 
    213         @assigns['content_for_layout'] = @content_for_layout 
    214         render_text(@template.render_file(layout, true), options[:status] || deprecated_status) 
     218        @assigns["content_for_layout"] = @content_for_layout 
     219        render_text(@template.render_file(layout, true), deprecated_status) 
    215220      else 
    216221        render_with_no_layout(options, deprecated_status) 
     
    220225    private 
    221226      def render_with_a_layout_options(options) 
    222         return options unless options.is_a?(Hash) 
    223         case 
    224         when options[:text], options[:partial], options[:nothing], options[:inline] 
    225           # by default, :text, :partial, :inline, and :nothing never use a layout 
    226           { :layout => false }.merge(options) 
    227         else 
    228           options 
    229         end 
    230       end 
    231  
    232       def pick_layout(options = {}, deprecated_layout = nil) 
     227        if options.is_a?(Hash) 
     228          case 
     229          when options[:text], options[:partial], options[:inline], options[:nothing] 
     230            # by default, :text, :partial, :inline, and :nothing never use a layout 
     231            options = options.clone 
     232            options[:layout] = false 
     233          end 
     234        end 
     235        options 
     236      end 
     237 
     238      def pick_layout(options = nil, deprecated_layout = nil) 
    233239        return deprecated_layout if !deprecated_layout.nil? 
    234240 
     
    248254     
    249255      def action_has_layout? 
    250         conditions = self.class.layout_conditions || {} 
    251         case 
    252           when conditions[:only] 
    253             conditions[:only].include?(action_name) 
    254           when conditions[:except] 
    255             !conditions[:except].include?(action_name)  
     256        if conditions = self.class.layout_conditions 
     257          case 
     258          when only = conditions[:only] 
     259            only.include?(action_name) 
     260          when except = conditions[:except] 
     261            !except.include?(action_name)  
    256262          else 
    257263            true 
     264          end 
     265        else 
     266          true 
    258267        end 
    259268      end 
  • branches/performance/actionpack/lib/action_view/partials.rb

    r1302 r1588  
    4343  # This will render the partial "advertisement/_ad.rhtml" regardless of which controller this is being called from. 
    4444  module Partials 
    45     def render_partial(partial_path, local_assigns = {}, deprecated_local_assigns = {}
     45    def render_partial(partial_path, local_assigns = nil, deprecated_local_assigns = nil
    4646      path, partial_name = partial_pieces(partial_path) 
    4747      object = extracting_object(partial_name, local_assigns, deprecated_local_assigns) 
    4848      local_assigns = extract_local_assigns(local_assigns, deprecated_local_assigns) 
     49      local_assigns = local_assigns ? local_assigns.clone : {} 
    4950      add_counter_to_local_assigns!(partial_name, local_assigns) 
     51      local_assigns[partial_name] = object 
    5052 
    51       render("#{path}/_#{partial_name}", { partial_name => object }.merge(local_assigns)
     53      render("#{path}/_#{partial_name}", local_assigns
    5254    end 
    5355 
    54     def render_partial_collection(partial_name, collection, partial_spacer_template = nil, local_assigns = {}
     56    def render_partial_collection(partial_name, collection, partial_spacer_template = nil, local_assigns = nil
    5557      collection_of_partials = Array.new 
    5658      counter_name = partial_counter_name(partial_name) 
     59      local_assigns = local_assigns ? local_assigns.clone : {} 
    5760      collection.each_with_index do |element, counter| 
    58         collection_of_partials.push(render_partial(partial_name, element, { counter_name => counter }.merge(local_assigns))) 
     61        local_assigns[counter_name] = counter 
     62        collection_of_partials.push(render_partial(partial_name, element, local_assigns)) 
    5963      end 
    6064