Changeset 1320

Show
Ignore:
Timestamp:
01/18/08 13:03:46 (9 months ago)
Author:
v..@exdolo.com
Message:

allow for explicitly rendering an object, which might otherwise not be renderable (such as a Hash, which would be taken for an options hash)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/lib/merb/mixins/render.rb

    r1249 r1320  
    118118    #   end 
    119119    #    
    120     #  This will first check to see if a index.xml.* template extists, if not 
    121     #  it will call @people.to_xml (as defined in the add_mime_type method) on the passed 
    122     #  in object if such a method exists for the current content_type   
     120    # This will first check to see if a index.xml.* template extists, if not 
     121    # it will call @people.to_xml (as defined in the add_mime_type method) on the passed 
     122    # in object if such a method exists for the current content_type   
     123    # 
     124    # Conversely, there may be situations where you prefer to be more literal 
     125    # such as when you desire to render a Hash, for those occasions, the 
     126    # the following syntax exists: 
     127    #    
     128    #   class People < Application 
     129    #     provides :xml 
     130    # 
     131    #     def index 
     132    #       @people = User.all 
     133    #       render :obj => @people 
     134    #     end 
     135    #   end 
    123136    #  
    124137    # When using multiple calls to render in one action, the context of the render is cached for performance reasons 
     
    140153       
    141154      # Handles the case where render is called with an object 
    142       if obj = args.first 
     155      if obj = args.first || opts[:obj] 
    143156        # Check for a template 
    144157        unless find_template({:action => action}.merge(opts)) 
  • trunk/spec/fixtures/controllers/render_spec_controllers.rb

    r1254 r1320  
    5959    @foo = FakeModel.new 
    6060    render @foo 
     61  end 
     62     
     63end 
     64 
     65class RenderHashObjectController < Merb::Controller 
     66   
     67  def render_object 
     68    provides :xml,:json 
     69    @foo = {:foo => 'bar', :baz => 'quuz'} 
     70    render :obj => @foo 
    6171  end 
    6272     
  • trunk/spec/merb/render_spec.rb

    r1228 r1320  
    413413end 
    414414 
     415describe "Merb rendering with a hash object calls to_json or to_xml on the object" do 
     416   
     417  it "render :obj => @foo should call @foo.to_json when json is requested" do 
     418    c = new_spec_controller(:format => 'json', :controller => 'RenderHashObjectController') 
     419    c.dispatch(:render_object) 
     420    c.body.should == "{\"foo\": \"bar\", \"baz\": \"quuz\"}" 
     421  end 
     422   
     423  it "render @foo should call @foo.to_xml when xml is requested" do 
     424    c = new_spec_controller(:format => 'xml', :controller => 'RenderHashObjectController') 
     425    c.dispatch(:render_object) 
     426    c.body.should == "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<hash>\n  <foo>bar</foo>\n  <baz>quuz</baz>\n</hash>\n" 
     427  end 
     428     
     429end 
     430 
    415431describe "Merb rendering with an object and using a block/lambda for provides" do 
    416432