aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTobias Lütke <tobias.luetke@gmail.com>2007-07-19 13:42:11 +0000
committerTobias Lütke <tobias.luetke@gmail.com>2007-07-19 13:42:11 +0000
commit9014bf3f262a6b55aa4a05e2626aeebf688aa05a (patch)
treed7a0bcd785d1a5984e55946a3abe4977d21e37f9
parent8b5eb2bacc9e61c6a23b85c03933ef6e2071fab8 (diff)
downloadrails-9014bf3f262a6b55aa4a05e2626aeebf688aa05a.tar.gz
rails-9014bf3f262a6b55aa4a05e2626aeebf688aa05a.tar.bz2
rails-9014bf3f262a6b55aa4a05e2626aeebf688aa05a.zip
* url_for now accepts a series of symbols representing the namespace of the record [Josh Knowles]. Closes #8640
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7197 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
-rw-r--r--actionpack/CHANGELOG2
-rw-r--r--actionpack/lib/action_controller/polymorphic_routes.rb22
-rw-r--r--actionpack/test/controller/polymorphic_routes_test.rb27
-rw-r--r--actionpack/test/template/form_helper_test.rb29
4 files changed, 76 insertions, 4 deletions
diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG
index 198e4a2fac..8fc1201d6d 100644
--- a/actionpack/CHANGELOG
+++ b/actionpack/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* url_for now accepts a series of symbols representing the namespace of the record [Josh Knowles]
+
* Make :trailing_slash work with query parameters for url_for. Closes #4004 [nov]
* Make sure missing template exceptions actually say which template they were looking for. Closes #8683 [dasil003]
diff --git a/actionpack/lib/action_controller/polymorphic_routes.rb b/actionpack/lib/action_controller/polymorphic_routes.rb
index 18dc1bd72f..0e826d4ee7 100644
--- a/actionpack/lib/action_controller/polymorphic_routes.rb
+++ b/actionpack/lib/action_controller/polymorphic_routes.rb
@@ -3,6 +3,8 @@ module ActionController
def polymorphic_url(record_or_hash_or_array, options = {})
record = extract_record(record_or_hash_or_array)
+ namespace = extract_namespace(record_or_hash_or_array)
+
args = case record_or_hash_or_array
when Hash: [ record_or_hash_or_array ]
when Array: record_or_hash_or_array.dup
@@ -21,7 +23,7 @@ module ActionController
:singular
end
- named_route = build_named_route_call(record_or_hash_or_array, inflection, options)
+ named_route = build_named_route_call(record_or_hash_or_array, namespace, inflection, options)
send(named_route, *args)
end
@@ -51,8 +53,8 @@ module ActionController
"#{options[:routing_type] || "url"}"
end
- def build_named_route_call(records, inflection, options = {})
- records = Array.new([extract_record(records)]) unless records.is_a?(Array)
+ def build_named_route_call(records, namespace, inflection, options = {})
+ records = Array.new([extract_record(records)]) unless records.is_a?(Array)
base_segment = "#{RecordIdentifier.send("#{inflection}_class_name", records.pop)}_"
method_root = records.reverse.inject(base_segment) do |string, name|
@@ -60,7 +62,7 @@ module ActionController
segment << string
end
- action_prefix(options) + method_root + routing_type(options)
+ action_prefix(options) + namespace + method_root + routing_type(options)
end
def extract_record(record_or_hash_or_array)
@@ -70,5 +72,17 @@ module ActionController
else record_or_hash_or_array
end
end
+
+ def extract_namespace(record_or_hash_or_array)
+ returning "" do |namespace|
+ if record_or_hash_or_array.is_a?(Array)
+ record_or_hash_or_array.delete_if do |record_or_namespace|
+ if record_or_namespace.is_a?(String) || record_or_namespace.is_a?(Symbol)
+ namespace << "#{record_or_namespace.to_s}_"
+ end
+ end
+ end
+ end
+ end
end
end
diff --git a/actionpack/test/controller/polymorphic_routes_test.rb b/actionpack/test/controller/polymorphic_routes_test.rb
index 1dc0502f63..3e72063e9e 100644
--- a/actionpack/test/controller/polymorphic_routes_test.rb
+++ b/actionpack/test/controller/polymorphic_routes_test.rb
@@ -39,6 +39,23 @@ class Test::Unit::TestCase
def article_comment_url(article, comment)
"http://www.example.com/articles/#{article.id}/comments/#{comment.id}"
end
+
+ def admin_articles_url
+ "http://www.example.com/admin/articles"
+ end
+ alias_method :new_admin_article_url, :admin_articles_url
+
+ def admin_article_url(article)
+ "http://www.example.com/admin/articles/#{article.id}"
+ end
+
+ def admin_article_comments_url(article)
+ "http://www.example.com/admin/articles/#{article.id}/comments"
+ end
+
+ def admin_article_comment_url(article, comment)
+ "http://www.example.com/admin/test/articles/#{article.id}/comments/#{comment.id}"
+ end
end
@@ -68,4 +85,14 @@ class PolymorphicRoutesTest < Test::Unit::TestCase
@comment.save
assert_equal(article_comment_url(@article, @comment), polymorphic_url([@article, @comment]))
end
+
+ def test_with_array_and_namespace
+ assert_equal(admin_articles_url, polymorphic_url([:admin, @article], :action => 'new'))
+ assert_equal(admin_articles_url, polymorphic_url([:admin, @article]))
+ @article.save
+ assert_equal(admin_article_url(@article), polymorphic_url([:admin, @article]))
+ assert_equal(admin_article_comments_url(@article), polymorphic_url([:admin, @article, @comment]))
+ @comment.save
+ assert_equal(admin_article_comment_url(@article, @comment), polymorphic_url([:admin, @article, @comment]))
+ end
end
diff --git a/actionpack/test/template/form_helper_test.rb b/actionpack/test/template/form_helper_test.rb
index bec754f844..a852f2af17 100644
--- a/actionpack/test/template/form_helper_test.rb
+++ b/actionpack/test/template/form_helper_test.rb
@@ -629,6 +629,25 @@ class FormHelperTest < Test::Unit::TestCase
assert_dom_equal expected, _erbout
end
+ def test_form_for_with_existing_object_and_namespace_in_list
+ @post.new_record = false
+ @comment.save
+ _erbout = ''
+ form_for([:admin, @post, @comment]) {}
+
+ expected = %(<form action="#{admin_comment_path(@post, @comment)}" class="edit_comment" id="edit_comment_1" method="post"><div style="margin:0;padding:0"><input name="_method" type="hidden" value="put" /></div></form>)
+ assert_dom_equal expected, _erbout
+ end
+
+ def test_form_for_with_new_object_and_namespace_in_list
+ @post.new_record = false
+ _erbout = ''
+ form_for([:admin, @post, @comment]) {}
+
+ expected = %(<form action="#{admin_comments_path(@post)}" class="new_comment" id="new_comment" method="post"></form>)
+ assert_dom_equal expected, _erbout
+ end
+
def test_form_for_with_existing_object_and_custom_url
_erbout = ''
@@ -660,6 +679,16 @@ class FormHelperTest < Test::Unit::TestCase
end
alias_method :post_comment_path, :comment_path
+ def admin_comments_path(post)
+ "/admin/posts/#{post.id}/comments"
+ end
+ alias_method :admin_post_comments_path, :admin_comments_path
+
+ def admin_comment_path(post, comment)
+ "/admin/posts/#{post.id}/comments/#{comment.id}"
+ end
+ alias_method :admin_post_comment_path, :admin_comment_path
+
def posts_path
"/posts"
end