Changeset 5699
- Timestamp:
- 12/06/06 23:26:18 (2 years ago)
- Files:
-
- branches/1-2-pre-release/actionpack/CHANGELOG (modified) (1 diff)
- branches/1-2-pre-release/actionpack/lib/action_controller/base.rb (modified) (3 diffs)
- branches/1-2-pre-release/actionpack/lib/action_controller/mime_type.rb (modified) (3 diffs)
- branches/1-2-pre-release/actionpack/test/controller/mime_responds_test.rb (modified) (2 diffs)
- branches/1-2-pre-release/actionpack/test/controller/render_test.rb (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/1-2-pre-release/actionpack/CHANGELOG
r5686 r5699 1 1 *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 2 8 3 9 * 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 651 651 # _Deprecation_ _notice_: This used to have the signature <tt>render_text("text", status = 200)</tt> 652 652 # 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 # 653 667 # === Rendering an inline template 654 668 # … … 735 749 render_xml(xml, options[:status]) 736 750 751 elsif json = options[:json] 752 render_json(json, options[:callback], options[:status]) 753 737 754 elsif partial = options[:partial] 738 755 partial = default_template_name if partial == true … … 813 830 response.content_type = Mime::XML 814 831 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) 815 839 end 816 840 branches/1-2-pre-release/actionpack/lib/action_controller/mime_type.rb
r5620 r5699 141 141 ATOM = Type.new "application/atom+xml", :atom 142 142 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 ] 145 146 146 147 LOOKUP = Hash.new { |h, k| h[k] = Type.new(k) unless k == "" } … … 171 172 LOOKUP["application/atom+xml"] = ATOM 172 173 173 174 LOOKUP["application/json"] = JSON 175 LOOKUP["text/x-json"] = JSON 176 177 174 178 EXTENSION_LOOKUP = Hash.new { |h, k| h[k] = Type.new(k) unless k == "" } 175 179 … … 192 196 EXTENSION_LOOKUP["rss"] = RSS 193 197 EXTENSION_LOOKUP["atom"] = ATOM 198 199 EXTENSION_LOOKUP["json"] = JSON 194 200 end branches/1-2-pre-release/actionpack/test/controller/mime_responds_test.rb
r5232 r5699 18 18 type.js { render :text => "JS" } 19 19 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" } 20 27 end 21 28 end … … 163 170 get :just_xml 164 171 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 165 193 end 166 194 branches/1-2-pre-release/actionpack/test/controller/render_test.rb
r5253 r5699 38 38 def render_text_hello_world 39 39 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') 40 48 end 41 49 … … 165 173 end 166 174 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 167 187 def test_do_with_render_custom_code 168 188 get :render_custom_code