Permalink
Browse files

Merge pull request #14684 from Futurelearn/jc-deep-nested-shallow-url…

…-helper-bug

Add a failing test for a URL helper that was broken by a6b9ea2.

(cherry picked from commit 1424482)

Conflicts:
	actionpack/CHANGELOG.md
  • Loading branch information...
1 parent fca54a1 commit d0e5963a38f931335926fa30b951cff4e0f3aff4 @pixeltrix pixeltrix committed Apr 11, 2014
View
6 actionpack/CHANGELOG.md
@@ -1,3 +1,9 @@
+* Only make deeply nested routes shallow when parent is shallow.
+
+ Fixes #14684.
+
+ *Andrew White*, *James Coglan*
+
* `date_select` helper with option `with_css_classes: true` does not overwrite other classes.
*Izumi Wong-Horiuchi*
View
15 actionpack/lib/action_dispatch/routing/mapper.rb
@@ -981,6 +981,7 @@ def initialize(entities, options = {})
@as = options[:as]
@param = (options[:param] || :id).to_sym
@options = options
+ @shallow = false
end
def default_actions
@@ -1041,6 +1042,13 @@ def nested_scope
"#{path}/:#{nested_param}"
end
+ def shallow=(value)
+ @shallow = value
+ end
+
+ def shallow?
+ @shallow
+ end
end
class SingletonResource < Resource #:nodoc:
@@ -1347,7 +1355,7 @@ def nested
end
with_scope_level(:nested) do
- if 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 }
@@ -1561,6 +1569,7 @@ def with_scope_level(kind)
end
def resource_scope(kind, resource) #:nodoc:
+ resource.shallow = @scope[:shallow]
old_resource, @scope[:scope_level_resource] = @scope[:scope_level_resource], resource
@nesting.push(resource)
@@ -1585,6 +1594,10 @@ def nesting_depth #:nodoc:
@nesting.size
end
+ def shallow_nesting_depth #:nodoc:
+ @nesting.select(&:shallow?).size
+ end
+
def param_constraint? #:nodoc:
@scope[:constraints] && @scope[:constraints][parent_resource.param].is_a?(Regexp)
end
View
18 actionpack/test/dispatch/routing_test.rb
@@ -1981,6 +1981,24 @@ def test_shallow_nested_resources_inside_resource
assert_equal 'cards#destroy', @response.body
end
+ def test_shallow_deeply_nested_resources
+ draw do
+ resources :blogs do
+ resources :posts do
+ resources :comments, shallow: true
+ end
+ end
+ end
+
+ get '/comments/1'
+ assert_equal 'comments#show', @response.body
+
+ assert_equal '/comments/1', comment_path('1')
+ assert_equal '/blogs/new', new_blog_path
+ assert_equal '/blogs/1/posts/new', new_blog_post_path(:blog_id => 1)
+ assert_equal '/blogs/1/posts/2/comments/new', new_blog_post_comment_path(:blog_id => 1, :post_id => 2)
+ end
+
def test_shallow_nested_resources_within_scope
draw do
scope '/hello' do

0 comments on commit d0e5963

Please sign in to comment.