Skip to content
This repository
Browse code

* 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
  • Loading branch information...
commit 9014bf3f262a6b55aa4a05e2626aeebf688aa05a 1 parent 8b5eb2b
authored
2  actionpack/CHANGELOG
... ...
@@ -1,5 +1,7 @@
1 1
 *SVN*
2 2
 
  3
+* url_for now accepts a series of symbols representing the namespace of the record [Josh Knowles]
  4
+
3 5
 * Make :trailing_slash work with query parameters for url_for. Closes #4004 [nov]
4 6
 
5 7
 * Make sure missing template exceptions actually say which template they were looking for.  Closes #8683 [dasil003]
22  actionpack/lib/action_controller/polymorphic_routes.rb
@@ -3,6 +3,8 @@ module PolymorphicRoutes
3 3
     def polymorphic_url(record_or_hash_or_array, options = {})
4 4
       record = extract_record(record_or_hash_or_array)
5 5
 
  6
+      namespace = extract_namespace(record_or_hash_or_array)
  7
+      
6 8
       args = case record_or_hash_or_array
7 9
         when Hash:  [ record_or_hash_or_array ]
8 10
         when Array: record_or_hash_or_array.dup
@@ -21,7 +23,7 @@ def polymorphic_url(record_or_hash_or_array, options = {})
21 23
           :singular
22 24
         end
23 25
       
24  
-      named_route = build_named_route_call(record_or_hash_or_array, inflection, options)
  26
+      named_route = build_named_route_call(record_or_hash_or_array, namespace, inflection, options)
25 27
       send(named_route, *args)
26 28
     end
27 29
 
@@ -51,8 +53,8 @@ def routing_type(options)
51 53
         "#{options[:routing_type] || "url"}"
52 54
       end
53 55
 
54  
-      def build_named_route_call(records, inflection, options = {})
55  
-        records = Array.new([extract_record(records)]) unless records.is_a?(Array)
  56
+      def build_named_route_call(records, namespace, inflection, options = {})
  57
+        records = Array.new([extract_record(records)]) unless records.is_a?(Array)        
56 58
         base_segment = "#{RecordIdentifier.send("#{inflection}_class_name", records.pop)}_"
57 59
 
58 60
         method_root = records.reverse.inject(base_segment) do |string, name|
@@ -60,7 +62,7 @@ def build_named_route_call(records, inflection, options = {})
60 62
           segment << string
61 63
         end
62 64
 
63  
-        action_prefix(options) + method_root + routing_type(options)
  65
+        action_prefix(options) + namespace + method_root + routing_type(options)
64 66
       end
65 67
 
66 68
       def extract_record(record_or_hash_or_array)
@@ -70,5 +72,17 @@ def extract_record(record_or_hash_or_array)
70 72
           else        record_or_hash_or_array
71 73
         end
72 74
       end
  75
+      
  76
+      def extract_namespace(record_or_hash_or_array)
  77
+        returning "" do |namespace|
  78
+          if record_or_hash_or_array.is_a?(Array)
  79
+            record_or_hash_or_array.delete_if do |record_or_namespace|
  80
+              if record_or_namespace.is_a?(String) || record_or_namespace.is_a?(Symbol)
  81
+                namespace << "#{record_or_namespace.to_s}_"
  82
+              end
  83
+            end
  84
+          end  
  85
+        end
  86
+      end
73 87
   end
74 88
 end
27  actionpack/test/controller/polymorphic_routes_test.rb
@@ -39,6 +39,23 @@ def article_comments_url(article)
39 39
   def article_comment_url(article, comment)
40 40
     "http://www.example.com/articles/#{article.id}/comments/#{comment.id}"
41 41
   end
  42
+  
  43
+  def admin_articles_url
  44
+    "http://www.example.com/admin/articles"
  45
+  end
  46
+  alias_method :new_admin_article_url, :admin_articles_url
  47
+  
  48
+  def admin_article_url(article)
  49
+    "http://www.example.com/admin/articles/#{article.id}"
  50
+  end
  51
+  
  52
+  def admin_article_comments_url(article)
  53
+    "http://www.example.com/admin/articles/#{article.id}/comments"
  54
+  end
  55
+  
  56
+  def admin_article_comment_url(article, comment)
  57
+    "http://www.example.com/admin/test/articles/#{article.id}/comments/#{comment.id}"
  58
+  end
42 59
 end
43 60
 
44 61
 
@@ -68,4 +85,14 @@ def test_with_array
68 85
     @comment.save
69 86
     assert_equal(article_comment_url(@article, @comment), polymorphic_url([@article, @comment]))
70 87
   end  
  88
+  
  89
+  def test_with_array_and_namespace
  90
+    assert_equal(admin_articles_url, polymorphic_url([:admin, @article], :action => 'new'))
  91
+    assert_equal(admin_articles_url, polymorphic_url([:admin, @article]))
  92
+    @article.save
  93
+    assert_equal(admin_article_url(@article), polymorphic_url([:admin, @article]))
  94
+    assert_equal(admin_article_comments_url(@article), polymorphic_url([:admin, @article, @comment]))
  95
+    @comment.save
  96
+    assert_equal(admin_article_comment_url(@article, @comment), polymorphic_url([:admin, @article, @comment]))
  97
+  end
71 98
 end
29  actionpack/test/template/form_helper_test.rb
@@ -629,6 +629,25 @@ def test_form_for_with_new_object_in_list
629 629
     assert_dom_equal expected, _erbout
630 630
   end
631 631
 
  632
+  def test_form_for_with_existing_object_and_namespace_in_list
  633
+    @post.new_record = false
  634
+    @comment.save
  635
+    _erbout = ''
  636
+    form_for([:admin, @post, @comment]) {}
  637
+  
  638
+    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>)
  639
+    assert_dom_equal expected, _erbout
  640
+  end
  641
+  
  642
+  def test_form_for_with_new_object_and_namespace_in_list
  643
+    @post.new_record = false
  644
+    _erbout = ''
  645
+    form_for([:admin, @post, @comment]) {}
  646
+  
  647
+    expected = %(<form action="#{admin_comments_path(@post)}" class="new_comment" id="new_comment" method="post"></form>)
  648
+    assert_dom_equal expected, _erbout
  649
+  end
  650
+
632 651
   def test_form_for_with_existing_object_and_custom_url
633 652
     _erbout = ''
634 653
 
@@ -660,6 +679,16 @@ def comment_path(post, comment)
660 679
     end
661 680
     alias_method :post_comment_path, :comment_path
662 681
     
  682
+    def admin_comments_path(post)
  683
+      "/admin/posts/#{post.id}/comments"
  684
+    end
  685
+    alias_method :admin_post_comments_path, :admin_comments_path
  686
+    
  687
+    def admin_comment_path(post, comment)
  688
+      "/admin/posts/#{post.id}/comments/#{comment.id}"
  689
+    end
  690
+    alias_method :admin_post_comment_path, :admin_comment_path
  691
+    
663 692
     def posts_path
664 693
       "/posts"
665 694
     end 

0 notes on commit 9014bf3

Please sign in to comment.
Something went wrong with that request. Please try again.