diff options
Diffstat (limited to 'actionpack')
-rw-r--r-- | actionpack/lib/action_view/helpers/ajax_helper.rb | 142 | ||||
-rw-r--r-- | actionpack/test/template/ajax_helper_test.rb | 268 |
2 files changed, 272 insertions, 138 deletions
diff --git a/actionpack/lib/action_view/helpers/ajax_helper.rb b/actionpack/lib/action_view/helpers/ajax_helper.rb index 445e59d3a5..169a803848 100644 --- a/actionpack/lib/action_view/helpers/ajax_helper.rb +++ b/actionpack/lib/action_view/helpers/ajax_helper.rb @@ -6,12 +6,11 @@ module ActionView include PrototypeHelper # Returns a form that will allow the unobtrusive JavaScript drivers to submit the - # form the dynamic nature of their choice. The default behaviour is an XMLHttpRequest - # in the background instead of the regular 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+. + # form dynamically. The default driver behaviour is an XMLHttpRequest in the background + # instead of the regular 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> anddefining callbacks is the same as +link_to_remote+. # # === Resource # @@ -30,7 +29,10 @@ module ActionView # # 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| %> + # <% remote_form_for :post, @post, :url => post_path(@post), + # :html => { :method => :put, + # :class => "edit_post", + # :id => "edit_post_45" } do |f| %> # ... # <% end %> # @@ -38,22 +40,22 @@ module ActionView # # Example: # # Generates: - # # <form class='edit_post_comment' - # # id='edit_comment_1' - # # action='/posts/1/comments/1/edit' + # # <form action='/authors/1/articles' + # # data-remote="true" + # # class='new_article' # # method='post' - # # data-remote='true'>...</div> + # # id='new_article'></form> # # - # <% remote_form_for([@post, @comment]) do |f| %> + # <% remote_form_for([@author, @article]) do |f| %> # ... # <% end %> # # This will expand to be the same as: # - # <% remote_form_for :comment, @comment, :url => post_comment_path(@post, @comment), + # <% remote_form_for :article, @article, :url => author_article_path(@author, @article), # :html => { :method => :put, - # :class => "edit_comment", - # :id => "edit_comment_45" } do |f| %> + # :class => "new_article", + # :id => "new_comment" } do |f| %> # ... # <% end %> # @@ -76,14 +78,13 @@ module ActionView alias_method :form_remote_for, :remote_form_for # Returns a form tag that will allow the unobtrusive JavaScript drivers to submit the - # form via the dynamic behaviour of choice. The default behaviour is an XMLHttpRequest + # form dynamically. The default JavaScript driver behaviour is an XMLHttpRequest # in the background instead of the regular 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>. # @@ -93,9 +94,9 @@ module ActionView # # <form action="http://www.example.com/fast" # # method="post" # # data-remote="true" - # # data-update-success="glass_of_beer"> + # # data-update-success="glass_of_beer"></form> # # - # form_remote_tag(:update => "glass_of_beer", :url => { :action => :fast }) + # form_remote_tag(:update => "glass_of_beer", :url => { :action => :fast }) {} # # The Hash passed to the <tt>:html</tt> key is equivalent to the options (2nd) # argument in the FormTagHelper.form_tag method. @@ -105,14 +106,14 @@ module ActionView # # form_remote_tag also takes a block, like form_tag: # # Generates: - # # <form action='/' + # # <form action='/posts' # # method='post' # # data-remote='true'> - # # <div><input name="commit" type="submit" value="Save" /></div> + # # <input name="commit" type="submit" value="Save" /> # # </form> # # # <% form_remote_tag :url => '/posts' do -%> - # <div><%= submit_tag 'Save' %></div> + # <%= submit_tag 'Save' %> # <% end -%> # # # Generates: @@ -122,7 +123,7 @@ module ActionView # # data-update-success="glass_of_beer">Hello world!</form> # # # <% form_remote_tag(:update => "glass_of_beer", :url => { :action => :fast }) do -%> - # <% concat "Hello world!" %> + # "Hello world!" # <% end -%> # def form_remote_tag(options = {}, &block) @@ -178,27 +179,38 @@ module ActionView # # Example: # # Generates: - # # <a href='/blog/5' - # # rel='nofollow' - # # data-remote='true' - # # data-method='delete' - # # data-success='posts' - # # data-failure='error' >Delete this post</a> + # # + # # <a href="#" + # # data-url="http://www.example.com/destroy" + # # data-update-success="posts" + # # data-update-failure="error" + # # data-remote="true">Delete this Post</a>' # # # link_to_remote "Delete this post", - # :url => { :action => "destroy", :id => post.id }, + # :url => { :action => "destroy"}, # :update => { :success => "posts", :failure => "error" } # # Optionally, you can use the <tt>options[:position]</tt> parameter to # influence how the target DOM element is updated. It must be one of # <tt>:before</tt>, <tt>:top</tt>, <tt>:bottom</tt>, or <tt>:after</tt>. # + # Example: + # # Generates: + # # <a href="#" + # # data-remote="true" + # # data-url="http://www.example.com/whatnot" + # # data-update-position="bottom">Remove Author</a> + # # + # link_to_remote("Remove Author", :url => { :action => "whatnot" }, :position => :bottom) + # + # # The method used is by default POST. You can also specify GET or you # can simulate PUT or DELETE over POST. All specified with <tt>options[:method]</tt> # # Example: # # Generates: - # # <a href='/person/4' + # # <a href='#' + # # data-url='/person/4' # # rel='nofollow' # # data-remote='true' # # data-method='delete'>Destroy</a> @@ -215,12 +227,14 @@ module ActionView # # Example: # # Generates: - # # <a href='/words/undo?n=33' - # # data-remote='true' >hello</a> # # - # word = 'hello' - # link_to_remote word, - # :url => { :action => "undo", :n => word_counter }, + # # <a href='#' + # # data-url='http://www.example.com/undo?n=5' + # # data-oncomplete='undoRequestCompleted(request)' + # # data-remote='true'>undo</a> + # # + # link_to_remote "undo", + # :url => { :controller => "words", :action => "undo", :n => word_counter }, # :complete => "undoRequestCompleted(request)" # # The callbacks that may be specified are (in order): @@ -290,7 +304,7 @@ module ActionView # # :with => "'name=' + $('name').value" # - # You can generate a link that uses AJAX in the general case, while + # You can generate a link that uses the UJS drivers in the general case, while # degrading gracefully to plain link behavior in the absence of # JavaScript by setting <tt>html_options[:href]</tt> to an alternate URL. # Note the extra curly braces around the <tt>options</tt> hash separate @@ -309,6 +323,7 @@ module ActionView # def link_to_remote(name, options, html_options = {}) attributes = {} + attributes.merge!(:rel => "nofollow") if options[:method] && options[:method].to_s.downcase == "delete" attributes.merge!(extract_remote_attributes!(options)) @@ -369,7 +384,7 @@ module ActionView # # type='button' # # value='Create' # # data-remote='true' - # # data-url='/testing/create' /> + # # data-url='/create' /> # # # <%= submit_to_remote 'create_btn', 'Create', :url => { :action => 'create' } %> # @@ -380,7 +395,7 @@ module ActionView # # <input name='update_btn' # # type='button' # # value='Update' - # # date-remote='true' + # # date-remote-submit='true' # # data-url='/testing/update' # # data-success='succeed' # # data-failure='fail' /> @@ -401,34 +416,49 @@ module ActionView tag(:input, attributes) end - # Periodically calls the specified url (<tt>options[:url]</tt>) every - # <tt>options[:frequency]</tt> seconds (default is 10). Usually used to + # Periodically provides the UJS driver with the information to call the specified + # url (<tt>options[:url]</tt>) every <tt>options[:frequency]</tt> seconds (default is 10). Usually used to # update a specified div (<tt>options[:update]</tt>) with the results # of the remote call. The options for specifying the target with <tt>:url</tt> # and defining callbacks is the same as link_to_remote. # Examples: # # Call get_averages and put its results in 'avg' every 10 seconds # # Generates: - # # new PeriodicalExecuter(function() {new Ajax.Updater('avg', '/grades/get_averages', - # # {asynchronous:true, evalScripts:true})}, 10) + # # <script data-periodical='true' + # # data-url='/get_averages' + # # type='application/json' + # # data-update-success='avg' + # # data-frequency='10'></script> + # # # periodically_call_remote(:url => { :action => 'get_averages' }, :update => 'avg') # # # Call invoice every 10 seconds with the id of the customer # # If it succeeds, update the invoice DIV; if it fails, update the error DIV # # Generates: - # # new PeriodicalExecuter(function() {new Ajax.Updater({success:'invoice',failure:'error'}, - # # '/testing/invoice/16', {asynchronous:true, evalScripts:true})}, 10) - # periodically_call_remote(:url => { :action => 'invoice', :id => customer.id }, + # # <script data-periodical='true' + # # data-url='/invoice/1' + # # type='application/json' + # # data-update-success='invoice' + # # data-update-failure='error' + # # data-frequency='10'></script>" + # # + # periodically_call_remote(:url => { :action => 'invoice', :id => 1 }, # :update => { :success => "invoice", :failure => "error" } # # # Call update every 20 seconds and update the new_block DIV # # Generates: - # # new PeriodicalExecuter(function() {new Ajax.Updater('news_block', 'update', {asynchronous:true, evalScripts:true})}, 20) + # # <script data-periodical='true' + # # data-url='update' + # # type='application/json' + # # data-update-success='news_block' + # # data-frequency='20'></script> + # # # periodically_call_remote(:url => 'update', :frequency => '20', :update => 'news_block') # def periodically_call_remote(options = {}) attributes = extract_observer_attributes!(options) attributes["data-periodical"] = true + attributes["data-frequency"] ||= 10 # periodically_call_remote does not need data-observe=true attributes.delete('data-observe') @@ -442,8 +472,16 @@ module ActionView # parameter with the Ajax call. # # Example: - # # Generates: new Form.Element.Observer('suggest', 0.25, function(element, value) {new Ajax.Updater('suggest', - # # '/testing/find_suggestion', {asynchronous:true, evalScripts:true, parameters:'q=' + value})}) + # # Generates: + # # "<script type='text/javascript' + # # data-observe='true' + # # data-observed='suggest' + # # data-frequency='0.25' + # # type='application/json' + # # data-url='/find_suggestion' + # # data-update-success='suggest' + # # data-with='q'></script>" + # # # <%= observe_field :suggest, :url => { :action => :find_suggestion }, # :frequency => 0.25, # :update => :suggest, @@ -567,7 +605,7 @@ module ActionView url_options = options.delete(:url) url_options = url_options.merge(:escape => false) if url_options.is_a?(Hash) - attributes["data-url"] = escape_javascript(url_for(url_options)) + attributes["data-url"] = escape_javascript(url_for(url_options)) if url_options purge_unused_attributes!(attributes) end @@ -591,14 +629,14 @@ module ActionView def extract_observer_attributes!(options) callback = options.delete(:function) - frequency = options.delete(:frequency) + frequency = options.delete(:frequency) || 10 attributes = extract_remote_attributes!(options) attributes["data-observe"] = true attributes["data-observed"] = options.delete(:observed) attributes["data-onobserve"] = callback if callback - attributes["data-frequency"] = frequency.to_i if frequency && frequency != 0 + attributes["data-frequency"] = frequency if frequency && frequency.to_f != 0 attributes.delete("data-remote") purge_unused_attributes!(attributes) diff --git a/actionpack/test/template/ajax_helper_test.rb b/actionpack/test/template/ajax_helper_test.rb index ed020d74f8..c925dbb8f6 100644 --- a/actionpack/test/template/ajax_helper_test.rb +++ b/actionpack/test/template/ajax_helper_test.rb @@ -6,8 +6,14 @@ class Author include ActiveModel::Conversion attr_reader :id - def save; @id = 1 end - def new_record?; @id.nil? end + def save + @id = 1 + end + + def new_record? + @id.nil? + end + def name @id.nil? ? 'new author' : "author ##{@id}" end @@ -18,8 +24,16 @@ class Article include ActiveModel::Conversion attr_reader :id attr_reader :author_id - def save; @id = 1; @author_id = 1 end - def new_record?; @id.nil? end + + def save + @id = 1 + @author_id = 1 + end + + def new_record? + @id.nil? + end + def name @id.nil? ? 'new article' : "article ##{@id}" end @@ -36,17 +50,34 @@ class AjaxHelperBaseTest < ActionView::TestCase super @template = self @controller = Class.new do + def url_for(options) - if options.is_a?(String) - options - else - url = "http://www.example.com/" - url << options[:action].to_s if options and options[:action] - url << "?a=#{options[:a]}" if options && options[:a] - url << "&b=#{options[:b]}" if options && options[:a] && options[:b] - url + return optons unless options.is_a?(Hash) + + url = options.delete(:only_path) ? '/' : 'http://www.example.com' + + if controller = options.delete(:controller) + url << '/' << controller.to_s + end + if action = options.delete(:action) + url << '/' << action.to_s end + + if id = options.delete(:id) + url << '/' << id.to_s + end + + url << hash_to_param(options) if options.any? + + url.gsub!(/\/\/+/,'/') + + url end + + private + def hash_to_param(hash) + hash.map { |k,v| "#{k}=#{v}" }.join('&').insert(0,'?') + end end.new end @@ -70,24 +101,36 @@ class AjaxHelperTest < AjaxHelperBaseTest end test "link_to_remote" do - assert_dom_equal %(<a class=\"fine\" href=\"#\" data-remote=\"true\" data-url=\"http://www.example.com/whatnot\">Remote outauthor</a>), - link_to_remote("Remote outauthor", { :url => { :action => "whatnot" }}, { :class => "fine" }) - assert_dom_equal %(<a href=\"#\" data-remote=\"true\" data-url=\"http://www.example.com/whatnot\" data-oncomplete=\"alert(request.responseText)\">Remote outauthor</a>), - link_to_remote("Remote outauthor", :complete => "alert(request.responseText)", :url => { :action => "whatnot" }) - assert_dom_equal %(<a href=\"#\" data-remote=\"true\" data-url=\"http://www.example.com/whatnot\" data-onsuccess=\"alert(request.responseText)\">Remote outauthor</a>), - link_to_remote("Remote outauthor", :success => "alert(request.responseText)", :url => { :action => "whatnot" }) - assert_dom_equal %(<a href=\"#\" data-remote=\"true\" data-url=\"http://www.example.com/whatnot\" data-onfailure=\"alert(request.responseText)\">Remote outauthor</a>), - link_to_remote("Remote outauthor", :failure => "alert(request.responseText)", :url => { :action => "whatnot" }) - assert_dom_equal %(<a href=\"#\" data-remote=\"true\" data-url=\"http://www.example.com/whatnot?a=10&b=20\" data-onfailure=\"alert(request.responseText)\">Remote outauthor</a>), - link_to_remote("Remote outauthor", :failure => "alert(request.responseText)", :url => { :action => "whatnot", :a => '10', :b => '20' }) - assert_dom_equal %(<a href=\"#\" data-remote=\"true\" data-url=\"http://www.example.com/whatnot\" data-remote-type=\"synchronous\">Remote outauthor</a>), - link_to_remote("Remote outauthor", :url => { :action => "whatnot" }, :type => :synchronous) - assert_dom_equal %(<a href=\"#\" data-remote=\"true\" data-url=\"http://www.example.com/whatnot\" data-update-position=\"bottom\">Remote outauthor</a>), - link_to_remote("Remote outauthor", :url => { :action => "whatnot" }, :position => :bottom) + assert_dom_equal %(<a class=\"fine\" href=\"#\" data-remote=\"true\" data-url=\"/whatnot\">Remove Author</a>), + link_to_remote("Remove Author", { :url => { :action => "whatnot" }}, { :class => "fine" }) + assert_dom_equal %(<a href=\"#\" data-remote=\"true\" data-url=\"/whatnot\" data-oncomplete=\"alert(request.responseText)\">Remove Author</a>), + link_to_remote("Remove Author", :complete => "alert(request.responseText)", :url => { :action => "whatnot" }) + assert_dom_equal %(<a href=\"#\" data-remote=\"true\" data-url=\"/whatnot\" data-onsuccess=\"alert(request.responseText)\">Remove Author</a>), + link_to_remote("Remove Author", :success => "alert(request.responseText)", :url => { :action => "whatnot" }) + assert_dom_equal %(<a href=\"#\" data-remote=\"true\" data-url=\"/whatnot\" data-onfailure=\"alert(request.responseText)\">Remove Author</a>), + link_to_remote("Remove Author", :failure => "alert(request.responseText)", :url => { :action => "whatnot" }) + assert_dom_equal %(<a href=\"#\" data-remote=\"true\" data-url=\"/whatnot?a=10&b=20\" data-onfailure=\"alert(request.responseText)\">Remove Author</a>), + link_to_remote("Remove Author", :failure => "alert(request.responseText)", :url => { :action => "whatnot", :a => '10', :b => '20' }) + assert_dom_equal %(<a href=\"#\" data-remote=\"true\" data-url=\"/whatnot\" data-remote-type=\"synchronous\">Remove Author</a>), + link_to_remote("Remove Author", :url => { :action => "whatnot" }, :type => :synchronous) + assert_dom_equal %(<a href=\"#\" data-remote=\"true\" data-url=\"/whatnot\" data-update-position=\"bottom\">Remove Author</a>), + link_to_remote("Remove Author", :url => { :action => "whatnot" }, :position => :bottom) + end + + test "link_to_remote with url and oncomplete" do + actual = link_to_remote "undo", :url => { :controller => "words", :action => "undo", :n => 5 }, :complete => "undoRequestCompleted(request)" + expected = '<a href="#" data-url="/words/undo?n=5" data-remote="true" data-oncomplete="undoRequestCompleted(request)">undo</a>' + assert_dom_equal expected, actual + end + + test "link_to_remote with delete" do + actual = link_to_remote("Remove Author", { :url => { :action => "whatnot" }, :method => 'delete'}, { :class => "fine" }) + expected = '<a class="fine" rel="nofollow" href="#" data-remote="true" data-method="delete" data-url="/whatnot">Remove Author</a>' + assert_dom_equal expected, actual end - + test "link_to_remote using both url and href" do - expected = '<a href="http://www.example.com/destroy" data-url="http://www.example.com/destroy" data-update-success="posts" data-remote="true">Delete this Post</a>' + expected = '<a href="/destroy" data-url="/destroy" data-update-success="posts" data-remote="true">Delete this Post</a>' assert_dom_equal expected, link_to_remote( "Delete this Post", { :update => "posts", :url => { :action => "destroy" } }, @@ -95,135 +138,162 @@ class AjaxHelperTest < AjaxHelperBaseTest end test "link_to_remote with update-success and url" do - expected = '<a href="#" data-url="http://www.example.com/destroy" data-update-success="posts" data-update-failure="error" data-remote="true">Delete this Post</a>' - assert_dom_equal expected, link_to_remote( "Delete this Post", :url => { :action => "destroy", :id => 5 }, + expected = '<a href="#" data-url="/destroy" data-update-success="posts" data-update-failure="error" data-remote="true">Delete this Post</a>' + assert_dom_equal expected, link_to_remote( "Delete this Post", :url => { :action => "destroy"}, :update => { :success => "posts", :failure => "error" }) end test "link_to_remote with before/after callbacks" do - assert_dom_equal %(<a href=\"#\" data-remote=\"true\" data-url=\"http://www.example.com/whatnot\" data-onbefore=\"before();\" data-onafter=\"after();\">Remote outauthor</a>), + assert_dom_equal %(<a href=\"#\" data-remote=\"true\" data-url=\"/whatnot\" data-onbefore=\"before();\" data-onafter=\"after();\">Remote outauthor</a>), link_to_remote("Remote outauthor", :url => { :action => "whatnot" }, :before => "before();", :after => "after();") end test "link_to_remote using :with expression" do - expected = %(<a href=\"#\" data-remote=\"true\" data-url=\"http://www.example.com/whatnot\" data-with=\"id=123\">Remote outauthor</a>) + expected = %(<a href=\"#\" data-remote=\"true\" data-url=\"/whatnot\" data-with=\"id=123\">Remote outauthor</a>) assert_dom_equal expected, link_to_remote("Remote outauthor", :url => { :action => "whatnot" }, :with => "id=123") end test "link_to_remote using :condition expression" do - expected = %(<a href=\"#\" data-remote=\"true\" data-url=\"http://www.example.com/whatnot\" data-condition=\"$('foo').val() == true\">Remote outauthor</a>) + expected = %(<a href=\"#\" data-remote=\"true\" data-url=\"/whatnot\" data-condition=\"$('foo').val() == true\">Remote outauthor</a>) assert_dom_equal expected, link_to_remote("Remote outauthor", :url => { :action => "whatnot" }, :condition => '$(\'foo\').val() == true') end test "link_to_remote using explicit :href" do - expected = %(<a href=\"http://www.example.com/testhref\" data-remote=\"true\" data-url=\"http://www.example.com/whatnot\" data-condition=\"$('foo').val() == true\">Remote outauthor</a>) + expected = %(<a href=\"http://www.example.com/testhref\" data-remote=\"true\" data-url=\"/whatnot\" data-condition=\"$('foo').val() == true\">Remote outauthor</a>) assert_dom_equal expected, link_to_remote("Remote outauthor", {:url => { :action => "whatnot" }, :condition => '$(\'foo\').val() == true'}, :href => 'http://www.example.com/testhref') end test "link_to_remote using :submit" do - expected = %(<a href=\"#\" data-remote=\"true\" data-url=\"http://www.example.com/whatnot\" data-submit=\"myForm\">Remote outauthor</a>) + expected = %(<a href=\"#\" data-remote=\"true\" data-url=\"/whatnot\" data-submit=\"myForm\">Remote outauthor</a>) assert_dom_equal expected, link_to_remote("Remote outauthor", :url => { :action => "whatnot" }, :submit => 'myForm') end test "link_to_remote with method delete" do - assert_dom_equal %(<a class=\"fine\" href=\"#\" data-remote=\"true\" data-url=\"http://www.example.com/whatnot\" data-method=\"delete\" rel=\"nofollow\">Remote outauthor</a>), + assert_dom_equal %(<a class=\"fine\" href=\"#\" data-remote=\"true\" data-url=\"/whatnot\" data-method=\"delete\" rel=\"nofollow\">Remote outauthor</a>), link_to_remote("Remote outauthor", { :url => { :action => "whatnot" }, :method => "delete"}, { :class => "fine" }) end test "link_to_remote with method delete as symbol" do - assert_dom_equal %(<a class=\"fine\" href=\"#\" data-remote=\"true\" data-url=\"http://www.example.com/whatnot\" data-method=\"delete\" rel=\"nofollow\">Remote outauthor</a>), + assert_dom_equal %(<a class=\"fine\" href=\"#\" data-remote=\"true\" data-url=\"/whatnot\" data-method=\"delete\" rel=\"nofollow\">Remote outauthor</a>), link_to_remote("Remote outauthor", { :url => { :action => "whatnot" }, :method => :delete}, { :class => "fine" }) end test "link_to_remote html options" do - assert_dom_equal %(<a class=\"fine\" href=\"#\" data-remote=\"true\" data-url=\"http://www.example.com/whatnot\">Remote outauthor</a>), + assert_dom_equal %(<a class=\"fine\" href=\"#\" data-remote=\"true\" data-url=\"/whatnot\">Remote outauthor</a>), link_to_remote("Remote outauthor", { :url => { :action => "whatnot" }, :html => { :class => "fine" } }) end test "link_to_remote url quote escaping" do - assert_dom_equal %(<a href="#" data-remote=\"true\" data-url=\"http://www.example.com/whatnot\\\'s\">Remote</a>), + assert_dom_equal %(<a href="#" data-remote=\"true\" data-url=\"/whatnot\\\'s\">Remote</a>), link_to_remote("Remote", { :url => { :action => "whatnot's" } }) end test "link_to_remote with confirm" do - assert_dom_equal %(<a class=\"fine\" href=\"#\" data-remote=\"true\" data-url=\"http://www.example.com/whatnot\" data-method=\"delete\" rel=\"nofollow\" data-confirm="Are you sure?">Remote confirm</a>), + assert_dom_equal %(<a class=\"fine\" href=\"#\" data-remote=\"true\" data-url=\"/whatnot\" data-method=\"delete\" rel=\"nofollow\" data-confirm="Are you sure?">Remote confirm</a>), link_to_remote("Remote confirm", { :url => { :action => "whatnot" }, :method => "delete", :confirm => "Are you sure?"}, { :class => "fine" }) end test "button_to_remote" do - assert_dom_equal %(<input class=\"fine\" type=\"button\" value=\"Remote outpost\" data-remote=\"true\" data-url=\"http://www.example.com/whatnot\" />), + assert_dom_equal %(<input class=\"fine\" type=\"button\" value=\"Remote outpost\" data-remote=\"true\" data-url=\"/whatnot\" />), button_to_remote("Remote outpost", { :url => { :action => "whatnot" }}, { :class => "fine" }) - assert_dom_equal %(<input type=\"button\" value=\"Remote outpost\" data-remote=\"true\" data-url=\"http://www.example.com/whatnot\" data-oncomplete=\"alert(request.reponseText)\" />), + assert_dom_equal %(<input type=\"button\" value=\"Remote outpost\" data-remote=\"true\" data-url=\"/whatnot\" data-oncomplete=\"alert(request.reponseText)\" />), button_to_remote("Remote outpost", :complete => "alert(request.reponseText)", :url => { :action => "whatnot" }) - assert_dom_equal %(<input type=\"button\" value=\"Remote outpost\" data-remote=\"true\" data-url=\"http://www.example.com/whatnot\" data-onsuccess=\"alert(request.reponseText)\" />), + assert_dom_equal %(<input type=\"button\" value=\"Remote outpost\" data-remote=\"true\" data-url=\"/whatnot\" data-onsuccess=\"alert(request.reponseText)\" />), button_to_remote("Remote outpost", :success => "alert(request.reponseText)", :url => { :action => "whatnot" }) - assert_dom_equal %(<input type=\"button\" value=\"Remote outpost\" data-remote=\"true\" data-url=\"http://www.example.com/whatnot\" data-onfailure=\"alert(request.reponseText)\" />), + assert_dom_equal %(<input type=\"button\" value=\"Remote outpost\" data-remote=\"true\" data-url=\"/whatnot\" data-onfailure=\"alert(request.reponseText)\" />), button_to_remote("Remote outpost", :failure => "alert(request.reponseText)", :url => { :action => "whatnot" }) - assert_dom_equal %(<input type=\"button\" value=\"Remote outpost\" data-remote=\"true\" data-url=\"http://www.example.com/whatnot?a=10&b=20\" data-onfailure=\"alert(request.reponseText)\" />), + assert_dom_equal %(<input type=\"button\" value=\"Remote outpost\" data-remote=\"true\" data-url=\"/whatnot?a=10&b=20\" data-onfailure=\"alert(request.reponseText)\" />), button_to_remote("Remote outpost", :failure => "alert(request.reponseText)", :url => { :action => "whatnot", :a => '10', :b => '20' }) end test "button_to_remote with confirm" do - assert_dom_equal %(<input class=\"fine\" type=\"button\" value=\"Remote outpost\" data-remote=\"true\" data-url=\"http://www.example.com/whatnot\" data-confirm="Are you sure?" />), + assert_dom_equal %(<input class=\"fine\" type=\"button\" value=\"Remote outpost\" data-remote=\"true\" data-url=\"/whatnot\" data-confirm="Are you sure?" />), button_to_remote("Remote outpost", { :url => { :action => "whatnot" }, :confirm => "Are you sure?"}, { :class => "fine" }) end test "button_to_remote with :submit" do - assert_dom_equal %(<input class=\"fine\" type=\"button\" value=\"Remote outpost\" data-remote=\"true\" data-url=\"http://www.example.com/whatnot\" data-submit="myForm" />), + assert_dom_equal %(<input class=\"fine\" type=\"button\" value=\"Remote outpost\" data-remote=\"true\" data-url=\"/whatnot\" data-submit="myForm" />), button_to_remote("Remote outpost", { :url => { :action => "whatnot" }, :submit => "myForm"}, { :class => "fine" }) end test "periodically_call_remote" do - assert_dom_equal %(<script data-url=\"http://www.example.com/mehr_bier\" data-update-success=\"schremser_bier\" type=\"application/json\" data-periodical=\"true\"></script>), - periodically_call_remote(:update => "schremser_bier", :url => { :action => "mehr_bier" }) + expected = "<script data-url='/mehr_bier' data-update-success='schremser_bier' type='application/json' data-frequency='10' data-periodical='true'></script>" + actual = periodically_call_remote(:update => "schremser_bier", :url => { :action => "mehr_bier" }) + assert_dom_equal expected, actual end test "periodically_call_remote_with_frequency" do - assert_dom_equal( - "<script data-periodical=\"true\" data-url=\"http://www.example.com/\" type=\"application/json\" data-frequency=\"2\"></script>", - periodically_call_remote(:frequency => 2) - ) + expected = "<script data-periodical='true' type='application/json' data-frequency='2'></script>" + actual = periodically_call_remote(:frequency => 2) + assert_dom_equal expected, actual end test "periodically_call_remote_with_function" do - assert_dom_equal( - "<script data-periodical=\"true\" data-url=\"http://www.example.com/\" type=\"application/json\" data-onobserve=\"alert('test')\" data-frequency=\"2\"></script>", - periodically_call_remote(:frequency => 2, :function => "alert('test')") - ) + expected = "<script data-periodical=\"true\" type=\"application/json\" data-onobserve=\"alert('test')\" data-frequency='2'></script>" + actual = periodically_call_remote(:frequency => 2, :function => "alert('test')") + assert_dom_equal expected, actual + end + + test "periodically_call_remote_with_update" do + actual = periodically_call_remote(:url => { :action => 'get_averages' }, :update => 'avg') + expected = "<script data-periodical='true' data-url='/get_averages' type='application/json' data-update-success='avg' data-frequency='10'></script>" + assert_dom_equal expected, actual + end + + test "periodically_call_remote with update success and failure" do + actual = periodically_call_remote(:url => { :action => 'invoice', :id => 1 },:update => { :success => "invoice", :failure => "error" }) + expected = "<script data-periodical='true' data-url='/invoice/1' type='application/json' data-update-success='invoice' data-frequency='10' data-update-failure='error'></script>" + assert_dom_equal expected, actual + end + + test "periodically_call_remote with frequency and update" do + actual = periodically_call_remote(:url => 'update', :frequency => '20', :update => 'news_block') + expected = "<script data-periodical='true' data-url='update' type='application/json' data-update-success='news_block' data-frequency='20'></script>" + assert_dom_equal expected, actual end test "form_remote_tag" do - assert_dom_equal %(<form action=\"http://www.example.com/fast\" method=\"post\" data-remote=\"true\" data-update-success=\"glass_of_beer\">), - form_remote_tag(:update => "glass_of_beer", :url => { :action => :fast }) - assert_dom_equal %(<form action=\"http://www.example.com/fast\" method=\"post\" data-remote=\"true\" data-update-success=\"glass_of_beer\">), + assert_dom_equal %(<form action=\"/fast\" method=\"post\" data-remote=\"true\" data-update-success=\"glass_of_beer\">), + form_remote_tag(:update => "glass_of_beer", :url => { :action => :fast } ) + assert_dom_equal %(<form action=\"/fast\" method=\"post\" data-remote=\"true\" data-update-success=\"glass_of_beer\">), form_remote_tag(:update => { :success => "glass_of_beer" }, :url => { :action => :fast }) - assert_dom_equal %(<form action=\"http://www.example.com/fast\" method=\"post\" data-remote=\"true\" data-update-failure=\"glass_of_water\">), + assert_dom_equal %(<form action=\"/fast\" method=\"post\" data-remote=\"true\" data-update-failure=\"glass_of_water\">), form_remote_tag(:update => { :failure => "glass_of_water" }, :url => { :action => :fast }) - assert_dom_equal %(<form action=\"http://www.example.com/fast\" method=\"post\" data-remote=\"true\" data-update-success=\"glass_of_beer\" data-update-failure=\"glass_of_water\">), + assert_dom_equal %(<form action=\"/fast\" method=\"post\" data-remote=\"true\" data-update-success=\"glass_of_beer\" data-update-failure=\"glass_of_water\">), form_remote_tag(:update => { :success => 'glass_of_beer', :failure => "glass_of_water" }, :url => { :action => :fast }) end test "form_remote_tag with method" do - assert_dom_equal %(<form action=\"http://www.example.com/fast\" method=\"post\" data-remote=\"true\" data-update-success=\"glass_of_beer\"><div style='margin:0;padding:0;display:inline'><input name='_method' type='hidden' value='put' /></div>), + assert_dom_equal %(<form action=\"/fast\" method=\"post\" data-remote=\"true\" data-update-success=\"glass_of_beer\"><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 + test "form_remote_tag with url" do + form_remote_tag(:url => '/posts' ){} + expected = "<form action='/posts' method='post' data-remote='true'></form>" + assert_dom_equal expected, output_buffer + end + test "form_remote_tag with block in erb" do __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\" data-remote=\"true\" data-update-success=\"glass_of_beer\">Hello world!</form>), output_buffer + assert_dom_equal %(<form action=\"/fast\" method=\"post\" data-remote=\"true\" data-update-success=\"glass_of_beer\">Hello world!</form>), output_buffer end test "remote_form_for with record identification with new record" do remote_form_for(@record, {:html => { :id => 'create-author' }}) {} - expected = %(<form action='#{authors_path}' data-remote=\"true\" class='new_author' id='create-author' method='post'></form>) assert_dom_equal expected, output_buffer end + test "remote_form_for with url" do + remote_form_for(@record, {:html => { :id => 'create-author' }}) {} + expected = "<form action='/authors' data-remote='true' class='new_author' id='create-author' method='post'></form>" + assert_dom_equal expected, output_buffer + end + test "remote_form_for with record identification without html options" do remote_form_for(@record) {} - expected = %(<form action='#{authors_path}' data-remote=\"true\" class='new_author' method='post' id='new_author'></form>) assert_dom_equal expected, output_buffer end @@ -236,7 +306,8 @@ class AjaxHelperTest < AjaxHelperBaseTest assert_dom_equal expected, output_buffer end - test "remote_form_for with new object in list" do + test "remote_form_for with new nested object and an excisting parent" do + @author.save remote_form_for([@author, @article]) {} expected = %(<form action='#{author_articles_path(@author)}' data-remote=\"true\" class='new_article' method='post' id='new_article'></form>) @@ -246,94 +317,119 @@ class AjaxHelperTest < AjaxHelperBaseTest test "remote_form_for with existing object in list" do @author.save @article.save + remote_form_for([@author, @article]) {} - expected = %(<form action='#{author_article_path(@author, @article)}' id='edit_article_1' method='post' data-remote=\"true\" class='edit_article'><div style='margin:0;padding:0;display:inline'><input name='_method' type='hidden' value='put' /></div></form>) + expected = %(<form action='#{author_article_path(@author, @article)}' id='edit_article_#{@article.id}' method='post' data-remote=\"true\" 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 test "on callbacks" do callbacks = [:uninitialized, :loading, :loaded, :interactive, :complete, :success, :failure] callbacks.each do |callback| - assert_dom_equal %(<form action=\"http://www.example.com/fast\" method=\"post\" data-remote=\"true\" data-update-success=\"glass_of_beer\" data-on#{callback}=\"monkeys();\">), + assert_dom_equal %(<form action=\"/fast\" method=\"post\" data-remote=\"true\" data-update-success=\"glass_of_beer\" data-on#{callback}=\"monkeys();\">), form_remote_tag(:update => "glass_of_beer", :url => { :action => :fast }, callback=>"monkeys();") - assert_dom_equal %(<form action=\"http://www.example.com/fast\" method=\"post\" data-remote=\"true\" data-update-success=\"glass_of_beer\" data-on#{callback}=\"monkeys();\">), + assert_dom_equal %(<form action=\"/fast\" method=\"post\" data-remote=\"true\" data-update-success=\"glass_of_beer\" data-on#{callback}=\"monkeys();\">), 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\" data-remote=\"true\" data-update-failure=\"glass_of_beer\" data-on#{callback}=\"monkeys();\">), + assert_dom_equal %(<form action=\"/fast\" method=\"post\" data-remote=\"true\" data-update-failure=\"glass_of_beer\" data-on#{callback}=\"monkeys();\">), 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\" data-remote=\"true\" data-update-failure=\"glass_of_water\" data-update-success=\"glass_of_beer\" data-on#{callback}=\"monkeys();\">), + assert_dom_equal %(<form action=\"/fast\" method=\"post\" data-remote=\"true\" data-update-failure=\"glass_of_water\" data-update-success=\"glass_of_beer\" data-on#{callback}=\"monkeys();\">), 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\" data-remote=\"true\" data-update-success=\"glass_of_beer\" data-on#{callback}=\"monkeys();\">), + assert_dom_equal %(<form action=\"/fast\" method=\"post\" data-remote=\"true\" data-update-success=\"glass_of_beer\" data-on#{callback}=\"monkeys();\">), 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\" data-remote=\"true\" data-update-success=\"glass_of_beer\" data-on200=\"monkeys();\" data-on404=\"bananas();\">), + assert_dom_equal %(<form action=\"/fast\" method=\"post\" data-remote=\"true\" data-update-success=\"glass_of_beer\" data-on200=\"monkeys();\" data-on404=\"bananas();\">), 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\" data-remote=\"true\" data-update-success=\"glass_of_beer\" data-on#{callback}=\"monkeys();\">), + assert_dom_equal %(<form action=\"/fast\" method=\"post\" data-remote=\"true\" data-update-success=\"glass_of_beer\" data-on#{callback}=\"monkeys();\">), 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\" data-remote=\"true\" data-update-success=\"glass_of_beer\" data-on#{callback}=\"monkeys();\">), + assert_dom_equal %(<form action=\"/fast\" method=\"post\" data-remote=\"true\" data-update-success=\"glass_of_beer\" data-on#{callback}=\"monkeys();\">), form_remote_tag(:update => "glass_of_beer", :url => { :action => :fast }, callback=>"monkeys();") end #test ultimate combo - assert_dom_equal %(<form data-on404=\"bananas();\" method=\"post\" data-onsuccess=\"s()\" action=\"http://www.example.com/fast\" data-oncomplete=\"c();\" data-update-success=\"glass_of_beer\" data-on200=\"monkeys();\" data-onloading=\"c1()\" data-remote=\"true\" data-onfailure=\"f();\">), + assert_dom_equal %(<form data-on404=\"bananas();\" method=\"post\" data-onsuccess=\"s()\" action=\"/fast\" data-oncomplete=\"c();\" data-update-success=\"glass_of_beer\" data-on200=\"monkeys();\" data-onloading=\"c1()\" data-remote=\"true\" data-onfailure=\"f();\">), form_remote_tag(:update => "glass_of_beer", :url => { :action => :fast }, :loading => "c1()", :success => "s()", :failure => "f();", :complete => "c();", 200=>"monkeys();", 404=>"bananas();") end test "submit_to_remote" do - assert_dom_equal %(<input name=\"More beer!\" type=\"button\" value=\"1000000\" data-url=\"http://www.example.com/empty_bottle\" data-remote-submit=\"true\" data-update-success=\"empty_bottle\" />), + assert_dom_equal %(<input name=\"More beer!\" type=\"button\" value=\"1000000\" data-url=\"/empty_bottle\" data-remote-submit=\"true\" data-update-success=\"empty_bottle\" />), submit_to_remote("More beer!", 1_000_000, :url => { :action => 'empty_bottle' }, :update => "empty_bottle") end + test "submit_to_remote simple" do + expected = "<input name='create_btn' type='button' value='Create' data-remote-submit='true' data-url='/create' />" + actual = submit_to_remote 'create_btn', 'Create', :url => { :action => 'create' } + assert_dom_equal expected, actual + end + + test "submit_to_remote with success and failure" do + expected = "<input name='update_btn' data-url='/update' data-remote-submit='true' data-update-failure='fail' data-update-success='succeed' value='Update' type='button' />" + actual = submit_to_remote 'update_btn', 'Update', :url => { :action => 'update' }, :update => { :success => "succeed", :failure => "fail" } + assert_dom_equal expected, actual + end + test "observe_field" do - assert_dom_equal %(<script type=\"text/javascript\" data-observe=\"true\" data-observed=\"glass\" data-frequency=\"300\" type=\"application/json\" data-url=\"http://www.example.com/reorder_if_empty\"></script>), + assert_dom_equal %(<script type=\"text/javascript\" data-observe=\"true\" data-observed=\"glass\" data-frequency=\"300\" type=\"application/json\" data-url=\"/reorder_if_empty\"></script>), observe_field("glass", :frequency => 5.minutes, :url => { :action => "reorder_if_empty" }) end + test "observe_field with url, frequency, update and with" do + actual = observe_field :suggest, :url => { :action => :find_suggestion }, :frequency => 0.25, :update => :suggest, :with => 'q' + expected = "<script type='text/javascript' data-observe='true' data-observed='suggest' data-frequency='0.25' type='application/json' data-url='/find_suggestion' data-update-success='suggest' data-with='q'></script>" + assert_dom_equal actual, expected + end + + test "observe_field default frequency" do + actual = observe_field :suggest + expected = "<script type='text/javascript' data-observe='true' data-observed='suggest' data-frequency='10' type='application/json'></script>" + assert_dom_equal actual, expected + end + test "observe_field using with option" do - expected = %(<script type=\"text/javascript\" data-observe=\"true\" data-observed=\"glass\" data-frequency=\"300\" type=\"application/json\" data-url=\"http://www.example.com/check_value\" data-with=\"id=123\"></script>) + expected = %(<script type=\"text/javascript\" data-observe=\"true\" data-observed=\"glass\" data-frequency=\"300\" type=\"application/json\" data-url=\"/check_value\" data-with=\"id=123\"></script>) assert_dom_equal expected, observe_field("glass", :frequency => 5.minutes, :url => { :action => "check_value" }, :with => 'id=123') end test "observe_field using condition option" do - expected = %(<script type=\"text/javascript\" data-observe=\"true\" data-observed=\"glass\" data-frequency=\"300\" type=\"application/json\" data-url=\"http://www.example.com/check_value\" data-condition=\"$('foo').val() == true\"></script>) + expected = %(<script type=\"text/javascript\" data-observe=\"true\" data-observed=\"glass\" data-frequency=\"300\" type=\"application/json\" data-url=\"/check_value\" data-condition=\"$('foo').val() == true\"></script>) assert_dom_equal expected, observe_field("glass", :frequency => 5.minutes, :url => { :action => "check_value" }, :condition => '$(\'foo\').val() == true') end test "observe_field using json in with option" do - expected = %(<script data-with=\"{'id':value}\" data-observed=\"glass\" data-url=\"http://www.example.com/check_value\" data-observe=\"true\" type=\"application/json\" data-frequency=\"300\"></script>) + expected = %(<script data-with=\"{'id':value}\" data-observed=\"glass\" data-url=\"/check_value\" data-observe=\"true\" type=\"application/json\" data-frequency=\"300\"></script>) assert_dom_equal expected, observe_field("glass", :frequency => 5.minutes, :url => { :action => "check_value" }, :with => "{'id':value}") end test "observe_field using function for callback" do - assert_dom_equal %(<script data-observed=\"glass\" data-url=\"http://www.example.com/\" data-observe=\"true\" type=\"application/json\" data-onobserve=\"alert('Element changed')\" data-frequency=\"300\"></script>), + assert_dom_equal %(<script data-observed=\"glass\" data-observe=\"true\" type=\"application/json\" data-onobserve=\"alert('Element changed')\" data-frequency=\"300\"></script>), observe_field("glass", :frequency => 5.minutes, :function => "alert('Element changed')") end test "observe_form" do - assert_dom_equal %(<script data-observed=\"cart\" data-url=\"http://www.example.com/cart_changed\" data-observe=\"true\" type=\"application/json\" data-frequency=\"2\"></script>), + assert_dom_equal %(<script data-observed=\"cart\" data-url=\"/cart_changed\" data-observe=\"true\" type=\"application/json\" data-frequency=\"2\"></script>), observe_form("cart", :frequency => 2, :url => { :action => "cart_changed" }) end test "observe_form using function for callback" do - assert_dom_equal %(<script data-observed=\"cart\" data-url=\"http://www.example.com/\" data-observe=\"true\" type=\"application/json\" data-onobserve=\"alert('Form changed')\" data-frequency=\"2\"></script>), + assert_dom_equal %(<script data-observed=\"cart\" data-observe=\"true\" type=\"application/json\" data-onobserve=\"alert('Form changed')\" data-frequency=\"2\"></script>), observe_form("cart", :frequency => 2, :function => "alert('Form changed')") end test "observe_field without frequency" do - assert_dom_equal %(<script data-observed=\"glass\" data-url=\"http://www.example.com/\" data-observe=\"true\" type=\"application/json\"></script>), + assert_dom_equal %(<script data-observed=\"glass\" data-observe=\"true\" type=\"application/json\" data-frequency='10'></script>), observe_field("glass") end |