Skip to content

Commit

Permalink
Generate shallow paths for all children of shallow resources.
Browse files Browse the repository at this point in the history
Prior to this commit shallow resources would only generate paths for
non-direct children (with a nested depth greater than 1).

Take the following routes file.

    resources :blogs do
      resources :posts, shallow: true do
        resources :comments do
          resources :tags
        end
      end
    end

This would generate shallow paths for `tags` nested under `posts`,
e.g `/posts/:id/tags/`, however it would not generate shallow paths
for `comments` nested under `posts`, e.g `/posts/:id/comments/new`.

This commit changes the behaviour of the route mapper so that it
generate paths for direct children of shallow resources, for example
if you take the previous routes file, this will now generate
shallow paths for `comments` nested under `posts`, .e.g
`posts/:id/comments/new`.

This was the behaviour in Rails `4.0.4` however this was broken in
@jcoglan's fix for another routes related issue[1].

This also fixes an issue[2] reported by @smdern.

[1] d0e5963
[2] #15783
  • Loading branch information
sebjacobs authored and pixeltrix committed Jul 6, 2014
1 parent b974033 commit e972d34
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
7 changes: 6 additions & 1 deletion actionpack/CHANGELOG.md
@@ -1,3 +1,9 @@
* Generate shallow paths for all children of shallow resources.

Fixes #15783.

*Seb Jacobs*

* JSONP responses are now rendered with the `text/javascript` content type
when rendering through a `respond_to` block.

Expand Down Expand Up @@ -177,5 +183,4 @@

*Tony Wooster*


Please check [4-1-stable](https://github.com/rails/rails/blob/4-1-stable/actionpack/CHANGELOG.md) for previous changes.
2 changes: 1 addition & 1 deletion actionpack/lib/action_dispatch/routing/mapper.rb
Expand Up @@ -1434,7 +1434,7 @@ def nested
end

with_scope_level(:nested) do
if shallow? && shallow_nesting_depth > 1
if shallow? && shallow_nesting_depth >= 1
shallow_scope(parent_resource.nested_scope, nested_options) { yield }
else
scope(parent_resource.nested_scope, nested_options) { yield }
Expand Down
22 changes: 22 additions & 0 deletions actionpack/test/dispatch/routing_test.rb
Expand Up @@ -2022,6 +2022,28 @@ def test_shallow_deeply_nested_resources
assert_equal '/blogs/1/posts/2/comments/new', new_blog_post_comment_path(:blog_id => 1, :post_id => 2)
end

def test_direct_children_of_shallow_resources
draw do
resources :blogs do
resources :posts, shallow: true do
resources :comments
end
end
end

post '/posts/1/comments'
assert_equal 'comments#create', @response.body
assert_equal '/posts/1/comments', post_comments_path('1')

get '/posts/2/comments/new'
assert_equal 'comments#new', @response.body
assert_equal '/posts/2/comments/new', new_post_comment_path('2')

get '/posts/1/comments'
assert_equal 'comments#index', @response.body
assert_equal '/posts/1/comments', post_comments_path('1')
end

def test_shallow_nested_resources_within_scope
draw do
scope '/hello' do
Expand Down

0 comments on commit e972d34

Please sign in to comment.