0
@@ -14,6 +14,9 @@ class Response < Article
0
+class Editorial < Article
0
@@ -29,6 +32,7 @@ uses_mocha 'polymorphic URL helpers' do
0
@response = Response.new
0
+ @editorial = Editorial.new
0
@@ -37,11 +41,35 @@ uses_mocha 'polymorphic URL helpers' do
0
polymorphic_url(@article)
0
+ def test_with_fallthrough_to_route_for_base_class
0
+ stubs(:respond_to?).with('editorial_url').returns(false)
0
+ stubs(:respond_to?).with('article_url').returns(true)
0
+ expects(:article_url).with(@editorial)
0
+ polymorphic_url(@editorial)
0
+ def test_with_no_matching_routes
0
+ stubs(:respond_to?).with('article_url').returns(false)
0
+ stubs(:respond_to?).with('editorial_url').returns(false)
0
+ expects(:editorial_url).with(@editorial)
0
+ polymorphic_url(@editorial)
0
def test_with_new_record
0
expects(:articles_url).with()
0
@article.expects(:new_record?).returns(true)
0
polymorphic_url(@article)
0
+ def test_fallthrough_with_new_record
0
+ @editorial.stubs(:new_record?).returns(true)
0
+ stubs(:respond_to?).with('editorials_url').returns(false)
0
+ stubs(:respond_to?).with('articles_url').returns(true)
0
+ expects(:articles_url).with()
0
+ polymorphic_url(@editorial)
0
def test_with_record_and_action
0
expects(:new_article_url).with()
0
@@ -49,54 +77,137 @@ uses_mocha 'polymorphic URL helpers' do
0
polymorphic_url(@article, :action => 'new')
0
+ def test_fallthrough_with_record_and_action
0
+ stubs(:respond_to?).with('new_editorial_url').returns(false)
0
+ stubs(:respond_to?).with('new_article_url').returns(true)
0
+ expects(:new_article_url).with()
0
+ @editorial.expects(:new_record?).never
0
+ polymorphic_url(@editorial, :action => 'new')
0
def test_url_helper_prefixed_with_new
0
expects(:new_article_url).with()
0
new_polymorphic_url(@article)
0
+ def test_fallthrough_with_url_helper_prefixed_with_new
0
+ stubs(:respond_to?).with('new_editorial_url').returns(false)
0
+ stubs(:respond_to?).with('new_article_url').returns(true)
0
+ expects(:new_article_url).with()
0
+ new_polymorphic_url(@editorial)
0
def test_url_helper_prefixed_with_edit
0
expects(:edit_article_url).with(@article)
0
edit_polymorphic_url(@article)
0
+ def test_fallthrough_with_url_helper_prefixed_with_edit
0
+ stubs(:respond_to?).with('edit_editorial_url').returns(false)
0
+ stubs(:respond_to?).with('edit_article_url').returns(true)
0
+ expects(:edit_article_url).with(@editorial)
0
+ edit_polymorphic_url(@editorial)
0
def test_formatted_url_helper
0
expects(:formatted_article_url).with(@article, :pdf)
0
formatted_polymorphic_url([@article, :pdf])
0
+ def test_fallthrough_with_formatted_url_helper
0
+ stubs(:respond_to?).with('formatted_editorial_url').returns(false)
0
+ stubs(:respond_to?).with('formatted_article_url').returns(true)
0
+ expects(:formatted_article_url).with(@editorial, :pdf)
0
+ formatted_polymorphic_url([@editorial, :pdf])
0
expects(:article_url).with(@article, :pdf)
0
polymorphic_url(@article, :format => :pdf)
0
+ def test_fallthrough_with_format_option
0
+ stubs(:respond_to?).with('editorial_url').returns(false)
0
+ stubs(:respond_to?).with('article_url').returns(true)
0
+ expects(:article_url).with(@editorial, :pdf)
0
+ polymorphic_url(@editorial, :format => :pdf)
0
def test_id_and_format_option
0
expects(:article_url).with(:id => @article, :format => :pdf)
0
polymorphic_url(:id => @article, :format => :pdf)
0
+ def test_fallthrough_with_id_and_format_option
0
+ stubs(:respond_to?).with('editorial_url').returns(false)
0
+ stubs(:respond_to?).with('article_url').returns(true)
0
+ expects(:article_url).with(:id => @editorial, :format => :pdf)
0
+ polymorphic_url(:id => @editorial, :format => :pdf)
0
expects(:article_response_url).with(@article, @response)
0
polymorphic_url([@article, @response])
0
+ def test_fallthrough_for_child_with_nested
0
+ stubs(:respond_to?).with('response_editorial_url').returns(false)
0
+ stubs(:respond_to?).with('response_article_url').returns(true)
0
+ expects(:response_article_url).with(@response, @editorial)
0
+ polymorphic_url([@response, @editorial])
0
+ def test_fallthrough_for_parent_with_nested #fallthrough only currently works for the targeted resource, not for parents it is nested inside
0
+ stubs(:respond_to?).with('editorial_article_url').returns(false)
0
+ stubs(:respond_to?).with('editorial_response_url').returns(false)
0
+ expects(:editorial_response_url).with(@editorial, @response) #TODO: this should be expects(:article_response_url).with(@editorial, @response)
0
+ polymorphic_url([@editorial, @response])
0
def test_with_nested_unsaved
0
expects(:article_responses_url).with(@article)
0
polymorphic_url([@article, @response])
0
+ def test_fallthrough_for_child_with_nested_unsaved
0
+ stubs(:respond_to?).with('response_editorials_url').returns(false)
0
+ stubs(:respond_to?).with('response_articles_url').returns(true)
0
+ expects(:response_articles_url).with(@response)
0
+ polymorphic_url([@response, @editorial])
0
def test_new_with_array_and_namespace
0
expects(:new_admin_article_url).with()
0
polymorphic_url([:admin, @article], :action => 'new')
0
+ def test_fallthrough_for_new_with_array_and_namespace
0
+ stubs(:respond_to?).with('new_admin_editorial_url').returns(false)
0
+ stubs(:respond_to?).with('new_admin_article_url').returns(true)
0
+ expects(:new_admin_article_url).with()
0
+ polymorphic_url([:admin, @editorial], :action => 'new')
0
def test_unsaved_with_array_and_namespace
0
expects(:admin_articles_url).with()
0
polymorphic_url([:admin, @article])
0
+ def test_fallthrough_for_unsaved_with_array_and_namespace
0
+ stubs(:respond_to?).with('admin_editorials_url').returns(false)
0
+ stubs(:respond_to?).with('admin_articles_url').returns(true)
0
+ expects(:admin_articles_url).with()
0
+ polymorphic_url([:admin, @editorial])
0
def test_nested_unsaved_with_array_and_namespace
0
@@ -106,6 +217,21 @@ uses_mocha 'polymorphic URL helpers' do
0
polymorphic_url([:admin, @article, @response])
0
+ def test_fallthrough_for_nested_unsaved_with_array_and_namespace
0
+ stubs(:respond_to?).with('admin_editorial_url').returns(false)
0
+ stubs(:respond_to?).with('admin_article_url').returns(true)
0
+ expects(:admin_article_url).with(@editorial)
0
+ polymorphic_url([:admin, @editorial])
0
+ #fallthrough only currently works for the targeted resource, not for parents it is nested inside
0
+ stubs(:respond_to?).with('admin_editorial_responses_url').returns(false)
0
+ stubs(:respond_to?).with('admin_editorial_articles_url').returns(false)
0
+ stubs(:respond_to?).with('admin_article_responses_url').returns(true)
0
+ expects(:admin_editorial_responses_url).with(@editorial) #TODO: this should be expects(:admin_article_responses_url).with(@editorial)
0
+ polymorphic_url([:admin, @editorial, @response])
0
def test_nested_with_array_and_namespace
0
expects(:admin_article_response_url).with(@article, @response)
0
@@ -117,6 +243,24 @@ uses_mocha 'polymorphic URL helpers' do
0
expects(:site_admin_article_response_tag_url).with(@article, @response, @tag)
0
polymorphic_url([:site, :admin, @article, @response, @tag])
0
+ def test_fallthrough_for_nested_with_array_and_namespace
0
+ stubs(:respond_to?).with('admin_editorial_response_url').returns(false)
0
+ stubs(:respond_to?).with('admin_editorial_article_url').returns(false)
0
+ stubs(:respond_to?).with('admin_article_response_url').returns(true)
0
+ expects(:admin_editorial_response_url).with(@editorial, @response) #TODO: this should be expects(:admin_article_response_url).with(@editorial, @response)
0
+ polymorphic_url([:admin, @editorial, @response])
0
+ # a ridiculously long named route tests correct ordering of namespaces and nesting:
0
+ stubs(:respond_to?).with('site_admin_editorial_response_tag_url').returns(false)
0
+ stubs(:respond_to?).with('site_admin_editorial_response_article_url').returns(false)
0
+ stubs(:respond_to?).with('site_admin_article_response_tag_url').returns(true)
0
+ expects(:site_admin_editorial_response_tag_url).with(@editorial, @response, @tag) #TODO: this should be expects(:site_admin_article_response_tag_url).with(@editorial, @response, @tag)
0
+ polymorphic_url([:site, :admin, @editorial, @response, @tag])
0
# TODO: Needs to be updated to correctly know about whether the object is in a hash or not
0
@@ -129,6 +273,13 @@ uses_mocha 'polymorphic URL helpers' do
0
expects(:new_article_path).with()
0
polymorphic_path(@article, :action => :new)
0
+ def test_fallthrough_with_polymorphic_path_accepts_options
0
+ stubs(:respond_to?).with('new_editorial_path').returns(false)
0
+ stubs(:respond_to?).with('new_article_path').returns(true)
0
+ expects(:new_article_path).with()
0
+ polymorphic_path(@editorial, :action => :new)
0
def test_polymorphic_path_does_not_modify_arguments
0
expects(:admin_article_responses_url).with(@article)
+1 for something like this to appear in rails/rails/master.. needed.
I agree with eadz: +1
+1 from me
+1
+1
Small comment: to make this work with Rails 2.2, replace send! with send in polymorphic_routes.rb
What happened to this patch?
Thanks for documenting that, costan (looks like github ate the double underscores that go around your second send reference). I found the same fix but was having issues with the resulting URLs referenced—the app was breaking looking for parent_child_child_path, instead of parent_child_path. I had to roll back and haven’t yet gotten a chance to rule out my own routes.rb being the culprit, though.
What did happen to this patch?
Never got picked up off the old trac. It’s been on my todo list to freshen up for edge and open a lighthouse ticket. If anyone wants to take the ball and run, go for it.
it seems it doesn’t support resources under a namespace, does it?
oh, I was mistaken. Just had to pass to namespace and objects inside an array
+1
+1 on this, ran into the same problem.
Yeah, me too. The old ticket is here, for reference: http://dev.rubyonrails.org/ticket/10454
+1
Is a lighthouse ticket filed for this yet?
+1 Wow, Rails 2.3.0 and I’m still fighting with this.
bump
bump
This actually seems to achieve what I most needed from this which is to let my STI types pass through the same controller and views, maybe some other people who need the same thing found their way here and can be helped by this suggestion.
map.resources :items map.resources :pages, :controller => 'items' map.resources :buildings, :controller => 'items'