This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

Import form_remote_tag and remote_form_for

  • Loading branch information...
1 parent 11f0fa7 commit 4b71a541c92dea223844341548715e1e84fc78e2 @josh josh committed Jan 30, 2010
Showing with 209 additions and 0 deletions.
  1. +109 −0 lib/prototype_legacy_helper.rb
  2. +100 −0 test/test_prototype_legacy_helper.rb
@@ -232,6 +232,115 @@ def link_to_remote(name, options = {}, html_options = nil)
link_to_function(name, remote_function(options), html_options || options.delete(:html))
end
+ # Returns a form tag that will submit using XMLHttpRequest in the
+ # background instead of the regular reloading POST arrangement. Even
+ # though it's using JavaScript to serialize the form elements, the form
+ # submission will work just like a regular submission as viewed by the
+ # receiving side (all elements available in <tt>params</tt>). The options for
+ # specifying the target with <tt>:url</tt> and defining callbacks is the same as
+ # +link_to_remote+.
+ #
+ # A "fall-through" target for browsers that doesn't do JavaScript can be
+ # specified with the <tt>:action</tt>/<tt>:method</tt> options on <tt>:html</tt>.
+ #
+ # Example:
+ # # Generates:
+ # # <form action="/some/place" method="post" onsubmit="new Ajax.Request('',
+ # # {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;">
+ # form_remote_tag :html => { :action =>
+ # url_for(:controller => "some", :action => "place") }
+ #
+ # The Hash passed to the <tt>:html</tt> key is equivalent to the options (2nd)
+ # argument in the FormTagHelper.form_tag method.
+ #
+ # By default the fall-through action is the same as the one specified in
+ # the <tt>:url</tt> (and the default method is <tt>:post</tt>).
+ #
+ # form_remote_tag also takes a block, like form_tag:
+ # # Generates:
+ # # <form action="/" method="post" onsubmit="new Ajax.Request('/',
+ # # {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)});
+ # # return false;"> <div><input name="commit" type="submit" value="Save" /></div>
+ # # </form>
+ # <% form_remote_tag :url => '/posts' do -%>
+ # <div><%= submit_tag 'Save' %></div>
+ # <% end -%>
+ def form_remote_tag(options = {}, &block)
+ options[:form] = true
+
+ options[:html] ||= {}
+ options[:html][:onsubmit] =
+ (options[:html][:onsubmit] ? options[:html][:onsubmit] + "; " : "") +
+ "#{remote_function(options)}; return false;"
+
+ form_tag(options[:html].delete(:action) || url_for(options[:url]), options[:html], &block)
+ end
+
+ # 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_or_array
+ when String, Symbol
+ 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_or_array
+ object_name = ActionController::RecordIdentifier.singular_class_name(record_or_name_or_array)
+ apply_form_for_options!(object, options)
+ args.unshift object
+ end
+
+ concat(form_remote_tag(options))
+ fields_for(object_name, *(args << options), &proc)
+ concat('</form>'.html_safe!)
+ end
+ alias_method :form_remote_for, :remote_form_for
+
+ # Returns '<tt>eval(request.responseText)</tt>' which is the JavaScript function
+ # that +form_remote_tag+ can call in <tt>:complete</tt> to evaluate a multiple
+ # update return document using +update_element_function+ calls.
+ def evaluate_remote_response
+ "eval(request.responseText)"
+ end
+
# Observes the field with the DOM ID specified by +field_id+ and calls a
# callback when its contents have changed. The default callback is an
# Ajax call. By default the value of the observed field is sent as a
@@ -52,6 +52,106 @@ def test_periodically_call_remote_with_frequency
end
+ def test_form_remote_tag
+ assert_dom_equal %(<form action=\"http://www.example.com/fast\" method=\"post\" onsubmit=\"new Ajax.Updater('glass_of_beer', 'http://www.example.com/fast', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;\">),
+ form_remote_tag(:update => "glass_of_beer", :url => { :action => :fast })
+ assert_dom_equal %(<form action=\"http://www.example.com/fast\" method=\"post\" onsubmit=\"new Ajax.Updater({success:'glass_of_beer'}, 'http://www.example.com/fast', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;\">),
+ form_remote_tag(:update => { :success => "glass_of_beer" }, :url => { :action => :fast })
+ assert_dom_equal %(<form action=\"http://www.example.com/fast\" method=\"post\" onsubmit=\"new Ajax.Updater({failure:'glass_of_water'}, 'http://www.example.com/fast', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;\">),
+ form_remote_tag(:update => { :failure => "glass_of_water" }, :url => { :action => :fast })
+ assert_dom_equal %(<form action=\"http://www.example.com/fast\" method=\"post\" onsubmit=\"new Ajax.Updater({success:'glass_of_beer',failure:'glass_of_water'}, 'http://www.example.com/fast', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;\">),
+ form_remote_tag(:update => { :success => 'glass_of_beer', :failure => "glass_of_water" }, :url => { :action => :fast })
+ end
+
+ def test_form_remote_tag_with_method
+ assert_dom_equal %(<form action=\"http://www.example.com/fast\" method=\"post\" onsubmit=\"new Ajax.Updater('glass_of_beer', 'http://www.example.com/fast', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;\"><div style='margin:0;padding:0;display:inline'><input name='_method' type='hidden' value='put' /></div>),
+ form_remote_tag(:update => "glass_of_beer", :url => { :action => :fast }, :html => { :method => :put })
+ end
+
+ def test_form_remote_tag_with_block_in_erb
+ __in_erb_template = ''
+ form_remote_tag(:update => "glass_of_beer", :url => { :action => :fast }) { concat "Hello world!" }
+ assert_dom_equal %(<form action=\"http://www.example.com/fast\" method=\"post\" onsubmit=\"new Ajax.Updater('glass_of_beer', 'http://www.example.com/fast', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;\">Hello world!</form>), output_buffer
+ end
+
+ def test_on_callbacks
+ callbacks = [:uninitialized, :loading, :loaded, :interactive, :complete, :success, :failure]
+ callbacks.each do |callback|
+ assert_dom_equal %(<form action=\"http://www.example.com/fast\" method=\"post\" onsubmit=\"new Ajax.Updater('glass_of_beer', 'http://www.example.com/fast', {asynchronous:true, evalScripts:true, on#{callback.to_s.capitalize}:function(request){monkeys();}, parameters:Form.serialize(this)}); return false;">),
+ form_remote_tag(:update => "glass_of_beer", :url => { :action => :fast }, callback=>"monkeys();")
+ assert_dom_equal %(<form action=\"http://www.example.com/fast\" method=\"post\" onsubmit=\"new Ajax.Updater({success:'glass_of_beer'}, 'http://www.example.com/fast', {asynchronous:true, evalScripts:true, on#{callback.to_s.capitalize}:function(request){monkeys();}, parameters:Form.serialize(this)}); return false;">),
+ form_remote_tag(:update => { :success => "glass_of_beer" }, :url => { :action => :fast }, callback=>"monkeys();")
+ assert_dom_equal %(<form action=\"http://www.example.com/fast\" method=\"post\" onsubmit=\"new Ajax.Updater({failure:'glass_of_beer'}, 'http://www.example.com/fast', {asynchronous:true, evalScripts:true, on#{callback.to_s.capitalize}:function(request){monkeys();}, parameters:Form.serialize(this)}); return false;">),
+ form_remote_tag(:update => { :failure => "glass_of_beer" }, :url => { :action => :fast }, callback=>"monkeys();")
+ assert_dom_equal %(<form action=\"http://www.example.com/fast\" method=\"post\" onsubmit=\"new Ajax.Updater({success:'glass_of_beer',failure:'glass_of_water'}, 'http://www.example.com/fast', {asynchronous:true, evalScripts:true, on#{callback.to_s.capitalize}:function(request){monkeys();}, parameters:Form.serialize(this)}); return false;">),
+ form_remote_tag(:update => { :success => "glass_of_beer", :failure => "glass_of_water" }, :url => { :action => :fast }, callback=>"monkeys();")
+ end
+
+ #HTTP status codes 200 up to 599 have callbacks
+ #these should work
+ 100.upto(599) do |callback|
+ assert_dom_equal %(<form action=\"http://www.example.com/fast\" method=\"post\" onsubmit=\"new Ajax.Updater('glass_of_beer', 'http://www.example.com/fast', {asynchronous:true, evalScripts:true, on#{callback.to_s.capitalize}:function(request){monkeys();}, parameters:Form.serialize(this)}); return false;">),
+ form_remote_tag(:update => "glass_of_beer", :url => { :action => :fast }, callback=>"monkeys();")
+ end
+
+ #test 200 and 404
+ assert_dom_equal %(<form action=\"http://www.example.com/fast\" method=\"post\" onsubmit=\"new Ajax.Updater('glass_of_beer', 'http://www.example.com/fast', {asynchronous:true, evalScripts:true, on200:function(request){monkeys();}, on404:function(request){bananas();}, parameters:Form.serialize(this)}); return false;">),
+ form_remote_tag(:update => "glass_of_beer", :url => { :action => :fast }, 200=>"monkeys();", 404=>"bananas();")
+
+ #these shouldn't
+ 1.upto(99) do |callback|
+ assert_dom_equal %(<form action=\"http://www.example.com/fast\" method=\"post\" onsubmit=\"new Ajax.Updater('glass_of_beer', 'http://www.example.com/fast', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;">),
+ form_remote_tag(:update => "glass_of_beer", :url => { :action => :fast }, callback=>"monkeys();")
+ end
+ 600.upto(999) do |callback|
+ assert_dom_equal %(<form action=\"http://www.example.com/fast\" method=\"post\" onsubmit=\"new Ajax.Updater('glass_of_beer', 'http://www.example.com/fast', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;">),
+ form_remote_tag(:update => "glass_of_beer", :url => { :action => :fast }, callback=>"monkeys();")
+ end
+
+ #test ultimate combo
+ assert_dom_equal %(<form action=\"http://www.example.com/fast\" method=\"post\" onsubmit=\"new Ajax.Updater('glass_of_beer', 'http://www.example.com/fast', {asynchronous:true, evalScripts:true, on200:function(request){monkeys();}, on404:function(request){bananas();}, onComplete:function(request){c();}, onFailure:function(request){f();}, onLoading:function(request){c1()}, onSuccess:function(request){s()}, parameters:Form.serialize(this)}); return false;\">),
+ form_remote_tag(:update => "glass_of_beer", :url => { :action => :fast }, :loading => "c1()", :success => "s()", :failure => "f();", :complete => "c();", 200=>"monkeys();", 404=>"bananas();")
+ end
+
+ def test_remote_form_for_with_record_identification_with_new_record
+ remote_form_for(@record, {:html => { :id => 'create-author' }}) {}
+
+ 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, output_buffer
+ end
+
+ def test_remote_form_for_with_record_identification_without_html_options
+ remote_form_for(@record) {}
+
+ 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, output_buffer
+ end
+
+ def test_remote_form_for_with_record_identification_with_existing_record
+ @record.save
+ remote_form_for(@record) {}
+
+ 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;display:inline'><input name='_method' type='hidden' value='put' /></div></form>)
+ assert_dom_equal expected, output_buffer
+ end
+
+ def test_remote_form_for_with_new_object_in_list
+ 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, output_buffer
+ end
+
+ def test_remote_form_for_with_existing_object_in_list
+ @author.save
+ @article.save
+ 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;display:inline'><input name='_method' type='hidden' value='put' /></div></form>)
+ assert_dom_equal expected, output_buffer
+ end
+
+
def test_button_to_remote
assert_dom_equal %(<input class=\"fine\" type=\"button\" value=\"Remote outpost\" onclick=\"new Ajax.Request('http://www.example.com/whatnot', {asynchronous:true, evalScripts:true});\" />),
button_to_remote("Remote outpost", { :url => { :action => "whatnot" }}, { :class => "fine" })

0 comments on commit 4b71a54

Please sign in to comment.