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

Changeset 3778

Show
Ignore:
Timestamp:
03/05/06 19:16:55 (3 years ago)
Author:
xal
Message:

ActionController::Base.param_parsers now accept symbols. currently supported are :xml_node, :xml_simple and :yaml

Files:

Legend:

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

    r3777 r3778  
    279279    # this: 
    280280    # 
    281     #   ActionController::Base.param_parsers['application/xml'] = Proc.new do |data|  
    282     #     XmlSimple.xml_in(data, 'ForceArray' => false) 
    283     #   end 
    284     # 
    285     #   ActionController::Base.param_parsers['application/x-yaml'] = Proc.new do |data|  
    286     #     |post| YAML.load(post) 
    287     #   end 
     281    #   ActionController::Base.param_parsers['application/xml'] = :xml_simple 
     282    #   ActionController::Base.param_parsers['application/x-yaml'] = :yaml 
    288283    # 
    289284    @@param_parsers = { 
    290       'application/xml'     => Proc.new { |post| node = XmlNode.from_xml(post); { node.node_name => node } }, 
     285      'application/xml'     => :xml_node 
    291286    } 
    292287    cattr_accessor :param_parsers  
  • trunk/actionpack/lib/action_controller/cgi_ext/cgi_methods.rb

    r3777 r3778  
    6060 
    6161    def self.parse_formatted_request_parameters(format, raw_post_data) 
    62       ActionController::Base.param_parsers[format].call(raw_post_data) || {} 
     62      params = case strategy = ActionController::Base.param_parsers[format] 
     63      when Proc 
     64        strategy.call(raw_post_data) 
     65      when :xml_node 
     66        node = XmlNode.from_xml(raw_post_data) 
     67        { node.node_name => node } 
     68      when :xml_simple 
     69        XmlSimple.xml_in(raw_post_data, 'ForceArray' => false) 
     70      when :yaml 
     71        YAML.load(raw_post_data) 
     72      end 
     73       
     74      params || {} 
    6375    rescue Object => e 
    6476      { "exception" => "#{e.message} (#{e.class})", "backtrace" => e.backtrace,  
  • trunk/actionpack/test/controller/webservice_test.rb

    r3777 r3778  
    2828  def setup 
    2929    @controller = TestController.new 
     30    ActionController::Base.param_parsers.clear 
     31    ActionController::Base.param_parsers['application/xml'] = :xml_node 
    3032  end 
    3133   
     
    5658    ActionController::Base.param_parsers['application/x-yaml'] = Proc.new { |d| YAML.load(d) } 
    5759    process('POST', 'application/x-yaml', {"entry" => "loaded from yaml"}.to_yaml) 
     60    assert_equal 'entry', @controller.response.body 
    5861    assert @controller.params.has_key?(:entry) 
    5962    assert_equal 'loaded from yaml', @controller.params["entry"] 
    60   ensure 
    61     ActionController::Base.param_parsers['application/x-yaml'] = nil 
    6263  end 
    6364   
     65  def test_register_and_use_yaml_as_symbol 
     66    ActionController::Base.param_parsers['application/x-yaml'] = :yaml 
     67    process('POST', 'application/x-yaml', {"entry" => "loaded from yaml"}.to_yaml) 
     68    assert_equal 'entry', @controller.response.body 
     69    assert @controller.params.has_key?(:entry) 
     70    assert_equal 'loaded from yaml', @controller.params["entry"] 
     71  end 
     72 
     73  def test_register_and_use_xml_simple 
     74    ActionController::Base.param_parsers['application/xml'] = :xml_simple 
     75    process('POST', 'application/xml', '<request><summary>content...</summary><title>SimpleXml</title></request>' ) 
     76    assert_equal 'summary, title', @controller.response.body 
     77    assert @controller.params.has_key?(:summary) 
     78    assert @controller.params.has_key?(:title) 
     79    assert_equal 'content...', @controller.params["summary"] 
     80    assert_equal 'SimpleXml', @controller.params["title"] 
     81  end 
    6482   
    6583  def test_deprecated_request_methods