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

Changeset 7400

Show
Ignore:
Timestamp:
09/02/07 23:53:31 (3 years ago)
Author:
nzkoz
Message:

Add array support to remote_form_for for polymorphic urls. Closes #8654 [jade]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/actionpack/lib/action_view/helpers/prototype_helper.rb

    r7220 r7400  
    181181      end 
    182182 
    183       # Works like form_remote_tag, but uses form_for semantics. 
    184       def remote_form_for(record_or_name, *args, &proc) 
     183      # Creates a form that will submit using XMLHttpRequest in the background  
     184      # instead of the regular reloading POST arrangement and a scope around a  
     185      # specific resource that is used as a base for questioning about 
     186      # values for the fields.   
     187      # 
     188      # === Resource  
     189      # 
     190      # Example: 
     191      #   <% remote_form_for(@post) do |f| %> 
     192      #     ... 
     193      #   <% end %> 
     194      # 
     195      # This will expand to be the same as: 
     196      # 
     197      #   <% remote_form_for :post, @post, :url => post_path(@post), :html => { :method => :put, :class => "edit_post", :id => "edit_post_45" } do |f| %> 
     198      #     ... 
     199      #   <% end %> 
     200      # 
     201      # === Nested Resource  
     202      # 
     203      # Example: 
     204      #   <% remote_form_for([@post, @comment]) do |f| %> 
     205      #     ... 
     206      #   <% end %> 
     207      # 
     208      # This will expand to be the same as: 
     209      # 
     210      #   <% remote_form_for :comment, @comment, :url => post_comment_path(@post, @comment), :html => { :method => :put, :class => "edit_comment", :id => "edit_comment_45" } do |f| %> 
     211      #     ... 
     212      #   <% end %> 
     213      # 
     214      # If you don't need to attach a form to a resource, then check out form_remote_tag. 
     215      # 
     216      # See FormHelper#form_for for additional semantics. 
     217      def remote_form_for(record_or_name_or_array, *args, &proc) 
    185218        options = args.extract_options! 
    186219 
    187         case record_or_name 
     220        case record_or_name_or_array 
    188221        when String, Symbol 
    189           object_name = record_or_name 
     222          object_name = record_or_name_or_array 
     223        when Array 
     224          object = record_or_name_or_array.last 
     225          object_name = ActionController::RecordIdentifier.singular_class_name(object) 
     226          apply_form_for_options!(record_or_name_or_array, options) 
     227          args.unshift object 
    190228        else 
    191           object      = record_or_name 
    192           object_name = ActionController::RecordIdentifier.singular_class_name(record_or_name
     229          object      = record_or_name_or_array 
     230          object_name = ActionController::RecordIdentifier.singular_class_name(record_or_name_or_array
    193231          apply_form_for_options!(object, options) 
    194232          args.unshift object 
  • trunk/actionpack/test/template/prototype_helper_test.rb

    r6967 r7400  
    99  def name 
    1010    @id.nil? ? 'new author' : "author ##{@id}" 
     11  end 
     12end 
     13 
     14class Article 
     15  attr_reader :id 
     16  attr_reader :author_id 
     17  def save; @id = 1; @author_id = 1 end 
     18  def new_record?; @id.nil? end 
     19  def name 
     20    @id.nil? ? 'new article' : "article ##{@id}" 
    1121  end 
    1222end 
     
    3141  include ActionView::Helpers::CaptureHelper 
    3242  include ActionView::Helpers::RecordIdentificationHelper 
     43  include ActionController::PolymorphicRoutes 
    3344   
    3445  def setup 
     
    6071   
    6172  def setup 
    62     @record = Author.new 
     73    @record = @author = Author.new 
     74    @article = Article.new 
    6375    super 
    6476  end 
     
    115127    remote_form_for(@record, {:html => { :id => 'create-author' }}) {} 
    116128     
    117     expected = %(<form action='#{authors_url}' onsubmit="new Ajax.Request('#{authors_url}', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;" class='new_author' id='create-author' method='post'></form>) 
     129    expected = %(<form action='#{authors_path}' onsubmit="new Ajax.Request('#{authors_path}', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;" class='new_author' id='create-author' method='post'></form>) 
    118130    assert_dom_equal expected, _erbout 
    119131  end 
     
    123135    remote_form_for(@record) {} 
    124136     
    125     expected = %(<form action='#{authors_url}' onsubmit="new Ajax.Request('#{authors_url}', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;" class='new_author' method='post' id='new_author'></form>) 
     137    expected = %(<form action='#{authors_path}' onsubmit="new Ajax.Request('#{authors_path}', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;" class='new_author' method='post' id='new_author'></form>) 
    126138    assert_dom_equal expected, _erbout 
    127139  end 
     
    132144    remote_form_for(@record) {} 
    133145     
    134     expected = %(<form action='#{author_url(@record)}' id='edit_author_1' method='post' onsubmit="new Ajax.Request('#{author_url(@record)}', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;" class='edit_author'><div style='margin:0;padding:0'><input name='_method' type='hidden' value='put' /></div></form>) 
     146    expected = %(<form action='#{author_path(@record)}' id='edit_author_1' method='post' onsubmit="new Ajax.Request('#{author_path(@record)}', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;" class='edit_author'><div style='margin:0;padding:0'><input name='_method' type='hidden' value='put' /></div></form>) 
     147    assert_dom_equal expected, _erbout 
     148  end 
     149 
     150  def test_remote_form_for_with_new_object_in_list 
     151    _erbout = '' 
     152    remote_form_for([@author, @article]) {} 
     153     
     154    expected = %(<form action='#{author_articles_path(@author)}' onsubmit="new Ajax.Request('#{author_articles_path(@author)}', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;" class='new_article' method='post' id='new_article'></form>) 
     155    assert_dom_equal expected, _erbout 
     156  end 
     157   
     158  def test_remote_form_for_with_existing_object_in_list 
     159    @author.save 
     160    @article.save 
     161    _erbout = '' 
     162    remote_form_for([@author, @article]) {} 
     163     
     164    expected = %(<form action='#{author_article_path(@author, @article)}' id='edit_article_1' method='post' onsubmit="new Ajax.Request('#{author_article_path(@author, @article)}', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;" class='edit_article'><div style='margin:0;padding:0'><input name='_method' type='hidden' value='put' /></div></form>) 
    135165    assert_dom_equal expected, _erbout 
    136166  end 
     
    234264 
    235265  protected 
    236     def author_url(record) 
     266    def author_path(record) 
    237267      "/authors/#{record.id}" 
    238268    end 
    239269     
    240     def authors_url 
     270    def authors_path 
    241271      "/authors" 
    242272    end 
    243    
    244     def polymorphic_path(record
    245       if record.new_record? 
    246         "/authors" 
    247       else 
    248         "/authors/#{record.id}" 
    249       end 
     273     
     274    def author_articles_path(author
     275      "/authors/#{author.id}/articles" 
     276    end 
     277     
     278    def author_article_path(author, article) 
     279      "/authors/#{author.id}/articles/#{article.id}" 
    250280    end 
    251281end