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

Ticket #10055: in_place_editing_should_work_with_csrf_and_rjs.patch

File in_place_editing_should_work_with_csrf_and_rjs.patch, 3.7 kB (added by moses, 7 months ago)
  • test/in_place_editing_test.rb

    old new  
    2020      end 
    2121    end 
    2222    @controller = @controller.new 
     23    @protect_against_forgery = false 
    2324  end 
    24    
     25 
     26  def protect_against_forgery? 
     27    @protect_against_forgery 
     28  end 
     29 
    2530  def test_in_place_editor_external_control 
    2631      assert_dom_equal %(<script type=\"text/javascript\">\n//<![CDATA[\nnew Ajax.InPlaceEditor('some_input', 'http://www.example.com/inplace_edit', {externalControl:'blah'})\n//]]>\n</script>), 
    2732        in_place_editor('some_input', {:url => {:action => 'inplace_edit'}, :external_control => 'blah'}) 
     
    5964      :load_text_url => { :action => "action_to_get_value" }) 
    6065  end 
    6166   
    62   def test_in_place_editor_eval_scripts 
    63     assert_match "Ajax.InPlaceEditor('id-goes-here', 'http://www.example.com/action_to_set_value', {evalScripts:true})", 
     67  def test_in_place_editor_html_response 
     68    assert_match "Ajax.InPlaceEditor('id-goes-here', 'http://www.example.com/action_to_set_value', {htmlResponse:false})", 
    6469    in_place_editor( 'id-goes-here',  
    6570      :url => { :action => "action_to_set_value" },  
    6671      :script => true ) 
    6772  end 
    6873   
     74  def form_authenticity_token 
     75    "authenticity token" 
     76  end 
     77 
     78  def test_in_place_editor_with_forgery_protection 
     79    @protect_against_forgery = true 
     80    assert_match "Ajax.InPlaceEditor('id-goes-here', 'http://www.example.com/action_to_set_value', {callback:function(form) { return Form.serialize(form) + '&authenticity_token=' + encodeURIComponent('authenticity token') }})", 
     81    in_place_editor( 'id-goes-here', :url => { :action => "action_to_set_value" }) 
     82  end 
     83   
     84  def test_in_place_editor_text_between_controls 
     85    assert_match "Ajax.InPlaceEditor('id-goes-here', 'http://www.example.com/action_to_set_value', {textBetweenControls:'or'})", 
     86    in_place_editor( 'id-goes-here',  
     87      :url => { :action => "action_to_set_value" },  
     88      :text_between_controls => "or" ) 
     89  end 
    6990end 
  • lib/in_place_macros_helper.rb

    old new  
    4040    function << "'#{url_for(options[:url])}'" 
    4141 
    4242    js_options = {} 
     43 
     44    if protect_against_forgery? 
     45      options[:with] ||= "Form.serialize(form)" 
     46      options[:with] += " + '&authenticity_token=' + encodeURIComponent('#{form_authenticity_token}')" 
     47    end 
     48     
    4349    js_options['cancelText'] = %('#{options[:cancel_text]}') if options[:cancel_text] 
    4450    js_options['okText'] = %('#{options[:save_text]}') if options[:save_text] 
    4551    js_options['loadingText'] = %('#{options[:loading_text]}') if options[:loading_text] 
     
    5056    js_options['externalControl'] = "'#{options[:external_control]}'" if options[:external_control] 
    5157    js_options['loadTextURL'] = "'#{url_for(options[:load_text_url])}'" if options[:load_text_url]         
    5258    js_options['ajaxOptions'] = options[:options] if options[:options] 
    53     js_options['evalScripts'] = options[:script] if options[:script] 
     59    js_options['htmlResponse'] = !options[:script] if options[:script] 
    5460    js_options['callback']   = "function(form) { return #{options[:with]} }" if options[:with] 
    5561    js_options['clickToEditText'] = %('#{options[:click_to_edit_text]}') if options[:click_to_edit_text] 
     62    js_options['textBetweenControls'] = %('#{options[:text_between_controls]}') if options[:text_between_controls] 
    5663    function << (', ' + options_for_javascript(js_options)) unless js_options.empty? 
    5764     
    5865    function << ')'