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

Changeset 5699

Show
Ignore:
Timestamp:
12/06/06 23:26:18 (2 years ago)
Author:
bitsweat
Message:

Merge [5694]-[5698] from trunk: respond_to json and render :json => ...

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/1-2-pre-release/actionpack/CHANGELOG

    r5686 r5699  
    11*1.13.0 RC2* 
     2 
     3* respond_to recognizes JSON. render :json => @person.to_json automatically sets the content type and takes a :callback option to specify a client-side function to call using the rendered JSON as an argument.  #4185 [Scott Raymond, eventualbuddha] 
     4    # application/json response with body 'Element.show({:name: "David"})' 
     5    respond_to do |format| 
     6      format.json { render :json => { :name => "David" }.to_json, :callback => 'Element.show' } 
     7    end 
    28 
    39* Makes :discard_year work without breaking multi-attribute parsing in AR.  #1260, #3800 [sean@ardismg.com, jmartin@desertflood.com, stephen@touset.org, Bob Silva] 
  • branches/1-2-pre-release/actionpack/lib/action_controller/base.rb

    r5636 r5699  
    651651      # _Deprecation_ _notice_: This used to have the signature <tt>render_text("text", status = 200)</tt> 
    652652      # 
     653      # === Rendering JSON 
     654      # 
     655      # Rendering JSON sets the content type to text/x-json and optionally wraps the JSON in a callback. It is expected 
     656      # that the response will be eval'd for use as a data structure. 
     657      # 
     658      #   # Renders '{name: "David"}' 
     659      #   render :json => {:name => "David"}.to_json 
     660      # 
     661      # Sometimes the result isn't handled directly by a script (such as when the request comes from a SCRIPT tag), 
     662      # so the callback option is provided for these cases. 
     663      # 
     664      #   # Renders 'show({name: "David"})' 
     665      #   render :json => {:name => "David"}.to_json, :callback => 'show' 
     666      # 
    653667      # === Rendering an inline template 
    654668      # 
     
    735749            render_xml(xml, options[:status]) 
    736750 
     751          elsif json = options[:json] 
     752            render_json(json, options[:callback], options[:status]) 
     753 
    737754          elsif partial = options[:partial] 
    738755            partial = default_template_name if partial == true 
     
    813830        response.content_type = Mime::XML 
    814831        render_text(xml, status) 
     832      end 
     833 
     834      def render_json(json, callback = nil, status = nil) #:nodoc: 
     835        json = "#{callback}(#{json})" unless callback.blank? 
     836 
     837        response.content_type = Mime::JSON 
     838        render_text(json, status) 
    815839      end 
    816840 
  • branches/1-2-pre-release/actionpack/lib/action_controller/mime_type.rb

    r5620 r5699  
    141141  ATOM  = Type.new "application/atom+xml", :atom 
    142142  YAML  = Type.new "application/x-yaml", :yaml, %w( text/yaml ) 
    143  
    144   SET   = [ ALL, TEXT, HTML, JS, ICS, XML, RSS, ATOM, YAML ] 
     143  JSON  = Type.new "application/json", :json, %w( text/x-json ) 
     144 
     145  SET   = [ ALL, TEXT, HTML, JS, ICS, XML, RSS, ATOM, YAML, JSON ] 
    145146 
    146147  LOOKUP = Hash.new { |h, k| h[k] = Type.new(k) unless k == "" } 
     
    171172  LOOKUP["application/atom+xml"]     = ATOM 
    172173 
    173    
     174  LOOKUP["application/json"]         = JSON 
     175  LOOKUP["text/x-json"]              = JSON 
     176 
     177 
    174178  EXTENSION_LOOKUP = Hash.new { |h, k| h[k] = Type.new(k) unless k == "" } 
    175179 
     
    192196  EXTENSION_LOOKUP["rss"]   = RSS 
    193197  EXTENSION_LOOKUP["atom"]  = ATOM 
     198 
     199  EXTENSION_LOOKUP["json"]  = JSON 
    194200end 
  • branches/1-2-pre-release/actionpack/test/controller/mime_responds_test.rb

    r5232 r5699  
    1818      type.js   { render :text => "JS"      } 
    1919      type.all  { render :text => "Nothing" } 
     20    end 
     21  end 
     22 
     23  def json_or_yaml 
     24    respond_to do |type| 
     25      type.json { render :text => "JSON" } 
     26      type.yaml { render :text => "YAML" } 
    2027    end 
    2128  end 
     
    163170    get :just_xml 
    164171    assert_response 406 
     172  end 
     173 
     174  def test_json_or_yaml 
     175    get :json_or_yaml 
     176    assert_equal 'JSON', @response.body 
     177 
     178    get :json_or_yaml, :format => 'json' 
     179    assert_equal 'JSON', @response.body 
     180 
     181    get :json_or_yaml, :format => 'yaml' 
     182    assert_equal 'YAML', @response.body 
     183 
     184    { 'YAML' => %w(text/yaml), 
     185      'JSON' => %w(application/json text/x-json) 
     186    }.each do |body, content_types| 
     187      content_types.each do |content_type| 
     188        @request.env['HTTP_ACCEPT'] = content_type 
     189        get :json_or_yaml 
     190        assert_equal body, @response.body 
     191      end 
     192    end 
    165193  end 
    166194 
  • branches/1-2-pre-release/actionpack/test/controller/render_test.rb

    r5253 r5699  
    3838  def render_text_hello_world 
    3939    render_text "hello world" 
     40  end 
     41 
     42  def render_json_hello_world 
     43    render_json({:hello => 'world'}.to_json) 
     44  end 
     45 
     46  def render_json_hello_world_with_callback 
     47    render_json({:hello => 'world'}.to_json, 'alert') 
    4048  end 
    4149 
     
    165173  end 
    166174 
     175  def test_do_with_render_json 
     176    get :render_json_hello_world 
     177    assert_equal '{hello: "world"}', @response.body 
     178    assert_equal 'application/json', @response.content_type 
     179  end 
     180 
     181  def test_do_with_render_json_with_callback 
     182    get :render_json_hello_world_with_callback 
     183    assert_equal 'alert({hello: "world"})', @response.body 
     184    assert_equal 'application/json', @response.content_type 
     185  end 
     186 
    167187  def test_do_with_render_custom_code 
    168188    get :render_custom_code