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

Ticket #11401: change_insertion_new_for_element_insert.diff

File change_insertion_new_for_element_insert.diff, 8.6 kB (added by miloops, 2 years ago)

Patch with tests and docs updated.

  • test/template/prototype_helper_test.rb

    old new  
    303303  end 
    304304   
    305305  def test_insert_html_with_string 
    306     assert_equal 'new Insertion.Top("element", "\\u003Cp\\u003EThis is a test\\u003C/p\\u003E");', 
     306    assert_equal 'Element.insert("element", { top: "\\u003Cp\\u003EThis is a test\\u003C/p\\u003E" });', 
    307307      @generator.insert_html(:top, 'element', '<p>This is a test</p>') 
    308     assert_equal 'new Insertion.Bottom("element", "\\u003Cp\u003EThis is a test\\u003C/p\u003E");', 
     308    assert_equal 'Element.insert("element", { bottom: "\\u003Cp\u003EThis is a test\\u003C/p\u003E" });', 
    309309      @generator.insert_html(:bottom, 'element', '<p>This is a test</p>') 
    310     assert_equal 'new Insertion.Before("element", "\\u003Cp\u003EThis is a test\\u003C/p\u003E");', 
     310    assert_equal 'Element.insert("element", { before: "\\u003Cp\u003EThis is a test\\u003C/p\u003E" });', 
    311311      @generator.insert_html(:before, 'element', '<p>This is a test</p>') 
    312     assert_equal 'new Insertion.After("element", "\\u003Cp\u003EThis is a test\\u003C/p\u003E");', 
     312    assert_equal 'Element.insert("element", { after: "\\u003Cp\u003EThis is a test\\u003C/p\u003E" });', 
    313313      @generator.insert_html(:after, 'element', '<p>This is a test</p>') 
    314314  end 
    315315   
     
    375375    @generator.replace_html('baz', '<p>This is a test</p>') 
    376376     
    377377    assert_equal <<-EOS.chomp, @generator.to_s 
    378 new Insertion.Top("element", "\\u003Cp\\u003EThis is a test\\u003C/p\\u003E"); 
    379 new Insertion.Bottom("element", "\\u003Cp\\u003EThis is a test\\u003C/p\\u003E"); 
     378Element.insert("element", { top: "\\u003Cp\\u003EThis is a test\\u003C/p\\u003E" }); 
     379Element.insert("element", { bottom: "\\u003Cp\\u003EThis is a test\\u003C/p\\u003E" }); 
    380380["foo", "bar"].each(Element.remove); 
    381381Element.update("baz", "\\u003Cp\\u003EThis is a test\\u003C/p\\u003E"); 
    382382    EOS 
  • lib/action_controller/assertions/selector_assertions.rb

    old new  
    406406 
    407407          if rjs_type == :insert 
    408408            arg = args.shift 
     409            position  = arg 
    409410            insertion = "insert_#{arg}".to_sym 
    410411            raise ArgumentError, "Unknown RJS insertion type #{arg}" unless RJS_STATEMENTS[insertion] 
    411             statement = "(#{RJS_STATEMENTS[insertion]})" 
    412412          else 
    413413            raise ArgumentError, "Unknown RJS statement type #{rjs_type}" unless RJS_STATEMENTS[rjs_type] 
    414414            statement = "(#{RJS_STATEMENTS[rjs_type]})" 
     
    417417        else 
    418418          statement = "#{RJS_STATEMENTS[:any]}" 
    419419        end 
     420        position ||= Regexp.new(RJS_INSERTIONS.join('|')) 
    420421 
    421422        # Next argument we're looking for is the element identifier. If missing, we pick 
    422423        # any element. 
     
    433434              Regexp.new("\\$\\(\"#{id}\"\\)#{statement}\\(#{RJS_PATTERN_HTML}\\)", Regexp::MULTILINE) 
    434435            when :remove, :show, :hide, :toggle 
    435436              Regexp.new("#{statement}\\(\"#{id}\"\\)") 
     437            when :replace, :replace_html 
     438              Regexp.new("#{statement}\\(\"#{id}\", #{RJS_PATTERN_HTML}\\)") 
     439            when :insert, :insert_html 
     440              Regexp.new("Element.insert\\(\\\"#{id}\\\", \\{ #{position}: #{RJS_PATTERN_HTML} \\}\\);") 
    436441            else 
    437               Regexp.new("#{statement}\\(\"#{id}\", #{RJS_PATTERN_HTML}\\)", Regexp::MULTILINE
     442              Regexp.union(Regexp.new("#{statement}\\(\"#{id}\", #{RJS_PATTERN_HTML}\\)"), Regexp.new("Element.insert\\(\\\"#{id}\\\", \\{ #{position}: #{RJS_PATTERN_HTML} \\}\\);")
    438443          end 
    439444 
    440445        # Duplicate the body since the next step involves destroying it. 
     
    444449            matches = @response.body.match(pattern) 
    445450          else 
    446451            @response.body.gsub(pattern) do |match| 
    447               html = unescape_rjs($2
     452              html = unescape_rjs(match
    448453              matches ||= [] 
    449454              matches.concat HTML::Document.new(html).root.children.select { |n| n.tag? } 
    450455              "" 
     
    584589            :hide                 => /Element\.hide/, 
    585590            :toggle                 => /Element\.toggle/ 
    586591          } 
     592          RJS_STATEMENTS[:any] = Regexp.new("(#{RJS_STATEMENTS.values.join('|')})") 
     593          RJS_PATTERN_HTML = /"((\\"|[^"])*)"/ 
    587594          RJS_INSERTIONS = [:top, :bottom, :before, :after] 
    588595          RJS_INSERTIONS.each do |insertion| 
    589             RJS_STATEMENTS["insert_#{insertion}".to_sym] = Regexp.new(Regexp.quote("new Insertion.#{insertion.to_s.camelize}")) 
     596            RJS_STATEMENTS["insert_#{insertion}".to_sym] = /Element.insert\(\"([^\"]*)\", \{ #{insertion.to_s.downcase}: #{RJS_PATTERN_HTML} \}\);/ 
    590597          end 
    591           RJS_STATEMENTS[:any] = Regexp.new("(#{RJS_STATEMENTS.values.join('|')})") 
    592598          RJS_STATEMENTS[:insert_html] = Regexp.new(RJS_INSERTIONS.collect do |insertion| 
    593             Regexp.quote("new Insertion.#{insertion.to_s.camelize}") 
     599             /Element.insert\(\"([^\"]*)\", \{ #{insertion.to_s.downcase}: #{RJS_PATTERN_HTML} \}\);/ 
    594600          end.join('|')) 
    595           RJS_PATTERN_HTML = /"((\\"|[^"])*)"/ 
    596           RJS_PATTERN_EVERYTHING = Regexp.new("#{RJS_STATEMENTS[:any]}\\(\"([^\"]*)\", #{RJS_PATTERN_HTML}\\)", 
    597                                               Regexp::MULTILINE) 
     601          RJS_PATTERN_EVERYTHING = Regexp.new("#{RJS_STATEMENTS[:any]}\\(\"([^\"]*)\", #{RJS_PATTERN_HTML}\\)", Regexp::MULTILINE) 
    598602          RJS_PATTERN_UNICODE_ESCAPED_CHAR = /\\u([0-9a-zA-Z]{4})/ 
    599603        end 
    600604 
  • lib/action_view/helpers/prototype_helper.rb

    old new  
    603603        # Example: 
    604604        # 
    605605        #   # Generates: 
    606         #   #     new Insertion.Bottom("list", "<li>Some item</li>"); 
     606        #   #     new Element.insert("list", { bottom: <li>Some item</li>" }); 
    607607        #   #     new Effect.Highlight("list"); 
    608608        #   #     ["status-indicator", "cancel-link"].each(Element.hide); 
    609609        #   update_page do |page| 
     
    715715          # 
    716716          #   # Insert the rendered 'navigation' partial just before the DOM 
    717717          #   # element with ID 'content'. 
    718           #   # Generates: new Insertion.Before("content", "-- Contents of 'navigation' partial --"); 
     718          #   # Generates: Element.insert("content", { before: "-- Contents of 'navigation' partial --" }); 
    719719          #   insert_html :before, 'content', :partial => 'navigation' 
    720720          # 
    721721          #   # Add a list item to the bottom of the <ul> with ID 'list'. 
    722           #   # Generates: new Insertion.Bottom("list", "<li>Last item</li>"); 
     722          #   # Generates: Element.insert("list", { bottom: "<li>Last item</li>" }); 
    723723          #   insert_html :bottom, 'list', '<li>Last item</li>' 
    724724          # 
    725725          def insert_html(position, id, *options_for_render) 
    726             insertion = position.to_s.camelize 
    727             call "new Insertion.#{insertion}", id, render(*options_for_render) 
     726            content = javascript_object_for(render(*options_for_render)) 
     727            record "Element.insert(\"#{id}\", { #{position.to_s.downcase}: #{content} });" 
    728728          end 
    729729           
    730730          # Replaces the inner HTML of the DOM element with the given +id+. 
     
    761761          #     <%= render :partial => 'person', :collection => @people %> 
    762762          #   </div> 
    763763          # 
    764           #   # Insert a new person 
    765           #   #  
    766           #   # Generates: new Insertion.Bottom({object: "Matz", partial: "person"}, ""); 
    767           #   page.insert_html :bottom, :partial => 'person', :object => @person 
    768           # 
    769764          #   # Replace an existing person 
    770765          # 
    771766          #   # Generates: Element.replace("person_45", "-- Contents of partial --"); 
     
    10051000    protected 
    10061001      def options_for_ajax(options) 
    10071002        js_options = build_callbacks(options) 
    1008        
     1003         
    10091004        js_options['asynchronous'] = options[:type] != :synchronous 
    10101005        js_options['method']       = method_option_to_s(options[:method]) if options[:method] 
    1011         js_options['insertion']    = "Insertion.#{options[:position].to_s.camelize}" if options[:position] 
     1006        js_options['insertion']    = "#{options[:position].to_s.downcase}" if options[:position] 
    10121007        js_options['evalScripts']  = options[:script].nil? || options[:script] 
    10131008 
    10141009        if options[:form]