diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2007-05-14 17:30:35 +0000 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2007-05-14 17:30:35 +0000 |
commit | 6e7b593992f8f6f8e783b87e7ab9d1d5a97063f7 (patch) | |
tree | 8de722a5e02bd4e0e7bcfc10bc3a677d1744b9f4 /actionpack/test/template | |
parent | 50253edec97429ea92621bce6578cdedfcc19b62 (diff) | |
download | rails-6e7b593992f8f6f8e783b87e7ab9d1d5a97063f7.tar.gz rails-6e7b593992f8f6f8e783b87e7ab9d1d5a97063f7.tar.bz2 rails-6e7b593992f8f6f8e783b87e7ab9d1d5a97063f7.zip |
Added record identifications to FormHelper#form_for and PrototypeHelper#remote_form_for [DHH]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6731 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'actionpack/test/template')
-rw-r--r-- | actionpack/test/template/form_helper_test.rb | 65 | ||||
-rw-r--r-- | actionpack/test/template/prototype_helper_test.rb | 82 |
2 files changed, 126 insertions, 21 deletions
diff --git a/actionpack/test/template/form_helper_test.rb b/actionpack/test/template/form_helper_test.rb index a7336c5b08..0d054e2b63 100644 --- a/actionpack/test/template/form_helper_test.rb +++ b/actionpack/test/template/form_helper_test.rb @@ -1,5 +1,22 @@ require "#{File.dirname(__FILE__)}/../abstract_unit" +silence_warnings do + Post = Struct.new(:title, :author_name, :body, :secret, :written_on, :cost) + Post.class_eval do + alias_method :title_before_type_cast, :title unless respond_to?(:title_before_type_cast) + alias_method :body_before_type_cast, :body unless respond_to?(:body_before_type_cast) + alias_method :author_name_before_type_cast, :author_name unless respond_to?(:author_name_before_type_cast) + + def new_record=(boolean) + @new_record = boolean + end + + def new_record? + @new_record + end + end +end + class FormHelperTest < Test::Unit::TestCase include ActionView::Helpers::FormHelper include ActionView::Helpers::FormTagHelper @@ -7,15 +24,7 @@ class FormHelperTest < Test::Unit::TestCase include ActionView::Helpers::TagHelper include ActionView::Helpers::TextHelper include ActionView::Helpers::ActiveRecordHelper - - silence_warnings do - Post = Struct.new("Post", :title, :author_name, :body, :secret, :written_on, :cost) - Post.class_eval do - alias_method :title_before_type_cast, :title unless respond_to?(:title_before_type_cast) - alias_method :body_before_type_cast, :body unless respond_to?(:body_before_type_cast) - alias_method :author_name_before_type_cast, :author_name unless respond_to?(:author_name_before_type_cast) - end - end + include ActionView::Helpers::RecordIdentificationHelper def setup @post = Post.new @@ -546,6 +555,38 @@ class FormHelperTest < Test::Unit::TestCase form_for(:post, @post, :url => @post) do |f| end expected = "<form action=\"/posts/123\" method=\"post\"></form>" + assert_equal expected, _erbout + end + + def test_form_for_with_existing_object + _erbout = '' + + form_for(@post) do |f| end + + expected = "<form action=\"/posts/123\" class=\"edit_post\" id=\"edit_post_123\" method=\"post\"><div style=\"margin:0;padding:0\"><input name=\"_method\" type=\"hidden\" value=\"put\" /></div></form>" + assert_equal expected, _erbout + end + + def test_form_for_with_new_object + _erbout = '' + + post = Post.new + post.new_record = true + def post.id() nil end + + form_for(post) do |f| end + + expected = "<form action=\"/posts\" class=\"new_post\" id=\"new_post\" method=\"post\"></form>" + assert_equal expected, _erbout + end + + def test_form_for_with_existing_object_and_custom_url + _erbout = '' + + form_for(@post, :url => "/super_posts") do |f| end + + expected = "<form action=\"/super_posts\" class=\"edit_post\" id=\"edit_post_123\" method=\"post\"><div style=\"margin:0;padding:0\"><input name=\"_method\" type=\"hidden\" value=\"put\" /></div></form>" + assert_equal expected, _erbout end def test_remote_form_for_with_html_options_adds_options_to_form_tag @@ -561,6 +602,10 @@ class FormHelperTest < Test::Unit::TestCase protected def polymorphic_path(record, url_writer) - "/posts/#{record.id}" + if record.new_record? + "/posts" + else + "/posts/#{record.id}" + end end end
\ No newline at end of file diff --git a/actionpack/test/template/prototype_helper_test.rb b/actionpack/test/template/prototype_helper_test.rb index cd098f2a47..2be2c6f17e 100644 --- a/actionpack/test/template/prototype_helper_test.rb +++ b/actionpack/test/template/prototype_helper_test.rb @@ -2,6 +2,18 @@ require "#{File.dirname(__FILE__)}/../abstract_unit" Bunny = Struct.new(:Bunny, :id) +class Author + attr_reader :id + def save; @id = 1 end + def new_record?; @id.nil? end + def name + @id.nil? ? 'new author' : "author ##{@id}" + end +end + +class Author::Nested < Author; end + + module BaseTest include ActionView::Helpers::JavaScriptHelper include ActionView::Helpers::PrototypeHelper @@ -13,6 +25,7 @@ module BaseTest include ActionView::Helpers::FormTagHelper include ActionView::Helpers::FormHelper include ActionView::Helpers::CaptureHelper + include ActionView::Helpers::RecordIdentificationHelper def setup @template = nil @@ -41,17 +54,22 @@ end class PrototypeHelperTest < Test::Unit::TestCase include BaseTest + def setup + @record = Author.new + super + end + def test_link_to_remote - assert_dom_equal %(<a class=\"fine\" href=\"#\" onclick=\"new Ajax.Request('http://www.example.com/whatnot', {asynchronous:true, evalScripts:true}); return false;\">Remote outpost</a>), - link_to_remote("Remote outpost", { :url => { :action => "whatnot" }}, { :class => "fine" }) - assert_dom_equal %(<a href=\"#\" onclick=\"new Ajax.Request('http://www.example.com/whatnot', {asynchronous:true, evalScripts:true, onComplete:function(request){alert(request.reponseText)}}); return false;\">Remote outpost</a>), - link_to_remote("Remote outpost", :complete => "alert(request.reponseText)", :url => { :action => "whatnot" }) - assert_dom_equal %(<a href=\"#\" onclick=\"new Ajax.Request('http://www.example.com/whatnot', {asynchronous:true, evalScripts:true, onSuccess:function(request){alert(request.reponseText)}}); return false;\">Remote outpost</a>), - link_to_remote("Remote outpost", :success => "alert(request.reponseText)", :url => { :action => "whatnot" }) - assert_dom_equal %(<a href=\"#\" onclick=\"new Ajax.Request('http://www.example.com/whatnot', {asynchronous:true, evalScripts:true, onFailure:function(request){alert(request.reponseText)}}); return false;\">Remote outpost</a>), - link_to_remote("Remote outpost", :failure => "alert(request.reponseText)", :url => { :action => "whatnot" }) - assert_dom_equal %(<a href=\"#\" onclick=\"new Ajax.Request('http://www.example.com/whatnot?a=10&b=20', {asynchronous:true, evalScripts:true, onFailure:function(request){alert(request.reponseText)}}); return false;\">Remote outpost</a>), - link_to_remote("Remote outpost", :failure => "alert(request.reponseText)", :url => { :action => "whatnot", :a => '10', :b => '20' }) + assert_dom_equal %(<a class=\"fine\" href=\"#\" onclick=\"new Ajax.Request('http://www.example.com/whatnot', {asynchronous:true, evalScripts:true}); return false;\">Remote outauthor</a>), + link_to_remote("Remote outauthor", { :url => { :action => "whatnot" }}, { :class => "fine" }) + assert_dom_equal %(<a href=\"#\" onclick=\"new Ajax.Request('http://www.example.com/whatnot', {asynchronous:true, evalScripts:true, onComplete:function(request){alert(request.reponseText)}}); return false;\">Remote outauthor</a>), + link_to_remote("Remote outauthor", :complete => "alert(request.reponseText)", :url => { :action => "whatnot" }) + assert_dom_equal %(<a href=\"#\" onclick=\"new Ajax.Request('http://www.example.com/whatnot', {asynchronous:true, evalScripts:true, onSuccess:function(request){alert(request.reponseText)}}); return false;\">Remote outauthor</a>), + link_to_remote("Remote outauthor", :success => "alert(request.reponseText)", :url => { :action => "whatnot" }) + assert_dom_equal %(<a href=\"#\" onclick=\"new Ajax.Request('http://www.example.com/whatnot', {asynchronous:true, evalScripts:true, onFailure:function(request){alert(request.reponseText)}}); return false;\">Remote outauthor</a>), + link_to_remote("Remote outauthor", :failure => "alert(request.reponseText)", :url => { :action => "whatnot" }) + assert_dom_equal %(<a href=\"#\" onclick=\"new Ajax.Request('http://www.example.com/whatnot?a=10&b=20', {asynchronous:true, evalScripts:true, onFailure:function(request){alert(request.reponseText)}}); return false;\">Remote outauthor</a>), + link_to_remote("Remote outauthor", :failure => "alert(request.reponseText)", :url => { :action => "whatnot", :a => '10', :b => '20' }) end def test_periodically_call_remote @@ -80,7 +98,32 @@ class PrototypeHelperTest < Test::Unit::TestCase form_remote_tag(:update => "glass_of_beer", :url => { :action => :fast }) { _erbout.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>), _erbout end - + + 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>) + 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>) + assert_dom_equal expected, _erbout + end + + def test_remote_form_for_with_record_identification_with_existing_record + @record.save + _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>) + assert_dom_equal expected, _erbout + end + def test_on_callbacks callbacks = [:uninitialized, :loading, :loaded, :interactive, :complete, :success, :failure] callbacks.each do |callback| @@ -161,6 +204,23 @@ class PrototypeHelperTest < Test::Unit::TestCase assert_equal javascript_tag(create_generator(&block).to_s, {:defer => 'true'}), update_page_tag({:defer => 'true'}, &block) end + + protected + def author_url(record) + "/authors/#{record.id}" + end + + def authors_url + "/authors" + end + + def polymorphic_path(record, url_writer) + if record.new_record? + "/authors" + else + "/authors/#{record.id}" + end + end end class JavaScriptGeneratorTest < Test::Unit::TestCase |