Skip to content

Commit

Permalink
Add support for shallow nesting of routes. [#838 state:resolved]
Browse files Browse the repository at this point in the history
Adds :shallow option to resource route definition. If true, paths for nested
resources which reference a specific member (ie. those with an :id parameter)
will not use the parent path prefix or name prefix.

Example :

map.resources :users, :shallow => true do |user|
  user.resources :posts
end

* GET /users/1/posts (maps to PostsController#index action as usual)
  named route "user_posts" is added as usual.

* GET /posts/2 (maps to PostsController#show action as if it were not nested)
  Additionally, named route "post" is added too.
  • Loading branch information
lifo committed Aug 30, 2008
1 parent be4ae1f commit 83c6ba1
Show file tree
Hide file tree
Showing 3 changed files with 184 additions and 37 deletions.
14 changes: 14 additions & 0 deletions actionpack/CHANGELOG
@@ -1,5 +1,19 @@
*Edge*

* Add support for shallow nesting of routes. #838 [S. Brent Faulkner]

Example :

map.resources :users, :shallow => true do |user|
user.resources :posts
end

- GET /users/1/posts (maps to PostsController#index action as usual)
named route "user_posts" is added as usual.

- GET /posts/2 (maps to PostsController#show action as if it were not nested)
Additionally, named route "post" is added too.

* Added button_to_remote helper. #3641 [Donald Piret, Tarmo Tänav]

* Deprecate render_component. Please use render_component plugin from http://github.com/rails/render_component/tree/master [Pratik]
Expand Down
73 changes: 61 additions & 12 deletions actionpack/lib/action_controller/resources.rb
Expand Up @@ -85,16 +85,24 @@ def new_path
@new_path ||= "#{path}/#{new_action}"
end

def shallow_path_prefix
@shallow_path_prefix ||= "#{path_prefix unless @options[:shallow]}"
end

def member_path
@member_path ||= "#{path}/:id"
@member_path ||= "#{shallow_path_prefix}/#{path_segment}/:id"
end

def nesting_path_prefix
@nesting_path_prefix ||= "#{path}/:#{singular}_id"
@nesting_path_prefix ||= "#{shallow_path_prefix}/#{path_segment}/:#{singular}_id"
end

def shallow_name_prefix
@shallow_name_prefix ||= "#{name_prefix unless @options[:shallow]}"
end

def nesting_name_prefix
"#{name_prefix}#{singular}_"
"#{shallow_name_prefix}#{singular}_"
end

def action_separator
Expand Down Expand Up @@ -141,6 +149,8 @@ def initialize(entity, options)
super
end

alias_method :shallow_path_prefix, :path_prefix
alias_method :shallow_name_prefix, :name_prefix
alias_method :member_path, :path
alias_method :nesting_path_prefix, :path
end
Expand Down Expand Up @@ -318,6 +328,31 @@ def initialize(entity, options)
# comments_url(@article)
# comment_url(@article, @comment)
#
# * <tt>:shallow</tt> - If true, paths for nested resources which reference a specific member
# (ie. those with an :id parameter) will not use the parent path prefix or name prefix.
#
# The <tt>:shallow</tt> option is inherited by any nested resource(s).
#
# For example, 'users', 'posts' and 'comments' all use shallow paths with the following nested resources:
#
# map.resources :users, :shallow => true do |user|
# user.resources :posts do |post|
# post.resources :comments
# end
# end
# # --> GET /users/1/posts (maps to the PostsController#index action as usual)
# # also adds the usual named route called "user_posts"
# # --> GET /posts/2 (maps to the PostsController#show action as if it were not nested)
# # also adds the named route called "post"
# # --> GET /posts/2/comments (maps to the CommentsController#index action)
# # also adds the named route called "post_comments"
# # --> GET /comments/2 (maps to the CommentsController#show action as if it were not nested)
# # also adds the named route called "comment"
#
# You may also use <tt>:shallow</tt> in combination with the +has_one+ and +has_many+ shorthand notations like:
#
# map.resources :users, :has_many => { :posts => :comments }, :shallow => true
#
# If <tt>map.resources</tt> is called with multiple resources, they all get the same options applied.
#
# Examples:
Expand Down Expand Up @@ -443,7 +478,7 @@ def map_resource(entities, options = {}, &block)
map_associations(resource, options)

if block_given?
with_options(:path_prefix => resource.nesting_path_prefix, :name_prefix => resource.nesting_name_prefix, :namespace => options[:namespace], &block)
with_options(:path_prefix => resource.nesting_path_prefix, :name_prefix => resource.nesting_name_prefix, :namespace => options[:namespace], :shallow => options[:shallow], &block)
end
end
end
Expand All @@ -460,21 +495,35 @@ def map_singleton_resource(entities, options = {}, &block)
map_associations(resource, options)

if block_given?
with_options(:path_prefix => resource.nesting_path_prefix, :name_prefix => resource.nesting_name_prefix, :namespace => options[:namespace], &block)
with_options(:path_prefix => resource.nesting_path_prefix, :name_prefix => resource.nesting_name_prefix, :namespace => options[:namespace], :shallow => options[:shallow], &block)
end
end
end

def map_associations(resource, options)
map_has_many_associations(resource, options.delete(:has_many), options) if options[:has_many]

path_prefix = "#{options.delete(:path_prefix)}#{resource.nesting_path_prefix}"
name_prefix = "#{options.delete(:name_prefix)}#{resource.nesting_name_prefix}"

Array(options[:has_many]).each do |association|
resources(association, :path_prefix => path_prefix, :name_prefix => name_prefix, :namespace => options[:namespace])
Array(options[:has_one]).each do |association|
resource(association, :path_prefix => path_prefix, :name_prefix => name_prefix, :namespace => options[:namespace], :shallow => options[:shallow])
end
end

Array(options[:has_one]).each do |association|
resource(association, :path_prefix => path_prefix, :name_prefix => name_prefix, :namespace => options[:namespace])
def map_has_many_associations(resource, associations, options)
case associations
when Hash
associations.each do |association,has_many|
map_has_many_associations(resource, association, options.merge(:has_many => has_many))
end
when Array
associations.each do |association|
map_has_many_associations(resource, association, options)
end
when Symbol, String
resources(associations, :path_prefix => resource.nesting_path_prefix, :name_prefix => resource.nesting_name_prefix, :namespace => options[:namespace], :shallow => options[:shallow], :has_many => options[:has_many])
else
end
end

Expand Down Expand Up @@ -530,13 +579,13 @@ def map_member_actions(map, resource)
action_path = resource.options[:path_names][action] if resource.options[:path_names].is_a?(Hash)
action_path ||= Base.resources_path_names[action] || action

map_named_routes(map, "#{action}_#{resource.name_prefix}#{resource.singular}", "#{resource.member_path}#{resource.action_separator}#{action_path}", action_options)
map_named_routes(map, "#{action}_#{resource.shallow_name_prefix}#{resource.singular}", "#{resource.member_path}#{resource.action_separator}#{action_path}", action_options)
end
end
end

show_action_options = action_options_for("show", resource)
map_named_routes(map, "#{resource.name_prefix}#{resource.singular}", resource.member_path, show_action_options)
map_named_routes(map, "#{resource.shallow_name_prefix}#{resource.singular}", resource.member_path, show_action_options)

update_action_options = action_options_for("update", resource)
map_unnamed_routes(map, resource.member_path, update_action_options)
Expand Down Expand Up @@ -579,4 +628,4 @@ def action_options_for(action, resource, method = nil)

class ActionController::Routing::RouteSet::Mapper
include ActionController::Resources
end
end

4 comments on commit 83c6ba1

@acf
Copy link

@acf acf commented on 83c6ba1 Sep 8, 2008

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AWESOME. Does this already exist as a plugin? If not, I might have a crack at it.

@acf
Copy link

@acf acf commented on 83c6ba1 Sep 8, 2008

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AWESOME. Does this already exist as a plugin? If not, I might have a crack at it.

@acf
Copy link

@acf acf commented on 83c6ba1 Sep 8, 2008

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

argh. sorry for the double post, timed out on the first.

@shaokun
Copy link

@shaokun shaokun commented on 83c6ba1 Mar 5, 2009

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is cool!
But new_{resource}_path is not supported for the nested resource, can we add that too?

Please sign in to comment.