Permalink
Browse files

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

 [jade]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7400 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
NZKoz committed Sep 2, 2007
1 parent b18d956 commit 21837821a804f6c26a0672f9ce5697e198624968
@@ -180,16 +180,54 @@ def form_remote_tag(options = {}, &block)
form_tag(options[:html].delete(:action) || url_for(options[:url]), options[:html], &block)
end
- # Works like form_remote_tag, but uses form_for semantics.
- def remote_form_for(record_or_name, *args, &proc)
+ # Creates a form that will submit using XMLHttpRequest in the background
+ # instead of the regular reloading POST arrangement and a scope around a
+ # specific resource that is used as a base for questioning about
+ # values for the fields.
+ #
+ # === Resource
+ #
+ # Example:
+ # <% remote_form_for(@post) do |f| %>
+ # ...
+ # <% end %>
+ #
+ # This will expand to be the same as:
+ #
+ # <% remote_form_for :post, @post, :url => post_path(@post), :html => { :method => :put, :class => "edit_post", :id => "edit_post_45" } do |f| %>
+ # ...
+ # <% end %>
+ #
+ # === Nested Resource
+ #
+ # Example:
+ # <% remote_form_for([@post, @comment]) do |f| %>
+ # ...
+ # <% end %>
+ #
+ # This will expand to be the same as:
+ #
+ # <% remote_form_for :comment, @comment, :url => post_comment_path(@post, @comment), :html => { :method => :put, :class => "edit_comment", :id => "edit_comment_45" } do |f| %>
+ # ...
+ # <% end %>
+ #
+ # If you don't need to attach a form to a resource, then check out form_remote_tag.
+ #
+ # See FormHelper#form_for for additional semantics.
+ def remote_form_for(record_or_name_or_array, *args, &proc)
options = args.extract_options!
- case record_or_name
+ case record_or_name_or_array
when String, Symbol
- object_name = record_or_name
+ object_name = record_or_name_or_array
+ when Array
+ object = record_or_name_or_array.last
+ object_name = ActionController::RecordIdentifier.singular_class_name(object)
+ apply_form_for_options!(record_or_name_or_array, options)
+ args.unshift object
else
- object = record_or_name
- object_name = ActionController::RecordIdentifier.singular_class_name(record_or_name)
+ object = record_or_name_or_array
+ object_name = ActionController::RecordIdentifier.singular_class_name(record_or_name_or_array)
apply_form_for_options!(object, options)
args.unshift object
end
@@ -11,6 +11,16 @@ def name
end
end
+class Article
+ attr_reader :id
+ attr_reader :author_id
+ def save; @id = 1; @author_id = 1 end
+ def new_record?; @id.nil? end
+ def name
+ @id.nil? ? 'new article' : "article ##{@id}"
+ end
+end
+
class Author::Nested < Author; end
@@ -30,6 +40,7 @@ def self.included(base)
include ActionView::Helpers::FormHelper
include ActionView::Helpers::CaptureHelper
include ActionView::Helpers::RecordIdentificationHelper
+ include ActionController::PolymorphicRoutes
def setup
@template = nil
@@ -59,7 +70,8 @@ class PrototypeHelperTest < Test::Unit::TestCase
include BaseTest
def setup
- @record = Author.new
+ @record = @author = Author.new
+ @article = Article.new
super
end
@@ -114,15 +126,15 @@ def test_remote_form_for_with_record_identification_with_new_record
_erbout = ''
remote_form_for(@record, {:html => { :id => 'create-author' }}) {}
- 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>)
+ 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>)
assert_dom_equal expected, _erbout
end
def test_remote_form_for_with_record_identification_without_html_options
_erbout = ''
remote_form_for(@record) {}
- 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>)
+ 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>)
assert_dom_equal expected, _erbout
end
@@ -131,7 +143,25 @@ def test_remote_form_for_with_record_identification_with_existing_record
_erbout = ''
remote_form_for(@record) {}
- 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>)
+ 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>)
+ assert_dom_equal expected, _erbout
+ end
+
+ def test_remote_form_for_with_new_object_in_list
+ _erbout = ''
+ remote_form_for([@author, @article]) {}
+
+ 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>)
+ assert_dom_equal expected, _erbout
+ end
+
+ def test_remote_form_for_with_existing_object_in_list
+ @author.save
+ @article.save
+ _erbout = ''
+ remote_form_for([@author, @article]) {}
+
+ 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>)
assert_dom_equal expected, _erbout
end
@@ -233,20 +263,20 @@ def test_update_page_tag_with_html_options
protected
- def author_url(record)
+ def author_path(record)
"/authors/#{record.id}"
end
- def authors_url
+ def authors_path
"/authors"
end
-
- def polymorphic_path(record)
- if record.new_record?
- "/authors"
- else
- "/authors/#{record.id}"
- end
+
+ def author_articles_path(author)
+ "/authors/#{author.id}/articles"
+ end
+
+ def author_article_path(author, article)
+ "/authors/#{author.id}/articles/#{article.id}"
end
end

0 comments on commit 2183782

Please sign in to comment.