Skip to content

Commit

Permalink
Prefix nested resource named routes with their action name, e.g. new_…
Browse files Browse the repository at this point in the history
…group_user_path(@group) instead of group_new_user_path(@group). The old nested action named route is deprecated in Rails 1.2.4. Closes #8558.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7138 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information
jeremy committed Jun 27, 2007
1 parent b5b16a8 commit 557e193
Show file tree
Hide file tree
Showing 5 changed files with 204 additions and 44 deletions.
2 changes: 2 additions & 0 deletions actionpack/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*

* Prefix nested resource named routes with their action name, e.g. new_group_user_path(@group) instead of group_new_user_path(@group). The old nested action named route is deprecated in Rails 1.2.4. #8558 [David Chelimsky]

* Allow sweepers to be created solely for expiring after controller actions, not model changes [DHH]

* Added assigns method to ActionController::Caching::Sweeper to easily access instance variables on the controller [DHH]
Expand Down
21 changes: 13 additions & 8 deletions actionpack/lib/action_controller/resources.rb
Expand Up @@ -426,8 +426,10 @@ def map_collection_actions(map, resource)
resource.collection_methods.each do |method, actions|
actions.each do |action|
action_options = action_options_for(action, resource, method)
map.named_route("#{resource.name_prefix}#{action}_#{resource.plural}", "#{resource.path}/#{action}", action_options)
map.named_route("formatted_#{resource.name_prefix}#{action}_#{resource.plural}", "#{resource.path}/#{action}.:format", action_options)

# See http://dev.rubyonrails.org/ticket/8251
map.deprecated_named_route("#{action}_#{resource.name_prefix}#{resource.plural}", "#{resource.name_prefix}#{action}_#{resource.plural}", "#{resource.path}/#{action}", action_options)
map.deprecated_named_route("formatted_#{action}_#{resource.name_prefix}#{resource.plural}", "formatted_#{resource.name_prefix}#{action}_#{resource.plural}", "#{resource.path}/#{action}.:format", action_options)
end
end
end
Expand All @@ -453,11 +455,13 @@ def map_new_actions(map, resource)
actions.each do |action|
action_options = action_options_for(action, resource, method)
if action == :new
map.named_route("#{resource.name_prefix}new_#{resource.singular}", resource.new_path, action_options)
map.named_route("formatted_#{resource.name_prefix}new_#{resource.singular}", "#{resource.new_path}.:format", action_options)
# See http://dev.rubyonrails.org/ticket/8251
map.deprecated_named_route("new_#{resource.name_prefix}#{resource.singular}", "#{resource.name_prefix}new_#{resource.singular}", resource.new_path, action_options)
map.deprecated_named_route("formatted_new_#{resource.name_prefix}#{resource.singular}", "formatted_#{resource.name_prefix}new_#{resource.singular}", "#{resource.new_path}.:format", action_options)
else
map.named_route("#{resource.name_prefix}#{action}_new_#{resource.singular}", "#{resource.new_path}/#{action}", action_options)
map.named_route("formatted_#{resource.name_prefix}#{action}_new_#{resource.singular}", "#{resource.new_path}/#{action}.:format", action_options)
# See http://dev.rubyonrails.org/ticket/8251
map.deprecated_named_route("#{action}_new_#{resource.name_prefix}#{resource.singular}", "#{resource.name_prefix}#{action}_new_#{resource.singular}", "#{resource.new_path}/#{action}", action_options)
map.deprecated_named_route("formatted_#{action}_new_#{resource.name_prefix}#{resource.singular}", "formatted_#{resource.name_prefix}#{action}_new_#{resource.singular}", "#{resource.new_path}/#{action}.:format", action_options)
end
end
end
Expand All @@ -467,8 +471,9 @@ def map_member_actions(map, resource)
resource.member_methods.each do |method, actions|
actions.each do |action|
action_options = action_options_for(action, resource, method)
map.named_route("#{resource.name_prefix}#{action}_#{resource.singular}", "#{resource.member_path}/#{action}", action_options)
map.named_route("formatted_#{resource.name_prefix}#{action}_#{resource.singular}", "#{resource.member_path}/#{action}.:format",action_options)
# See http://dev.rubyonrails.org/ticket/8251
map.deprecated_named_route("#{action}_#{resource.name_prefix}#{resource.singular}", "#{resource.name_prefix}#{action}_#{resource.singular}", "#{resource.member_path}/#{action}", action_options)
map.deprecated_named_route("formatted_#{action}_#{resource.name_prefix}#{resource.singular}", "formatted_#{resource.name_prefix}#{action}_#{resource.singular}", "#{resource.member_path}/#{action}.:format",action_options)
end
end

Expand Down
22 changes: 20 additions & 2 deletions actionpack/lib/action_controller/routing.rb
Expand Up @@ -1020,6 +1020,11 @@ def named_route(name, path, options = {})
@set.add_named_route(name, path, options)
end

def deprecated_named_route(name, deprecated_name, path, options = {})
named_route(name, path, options)
@set.add_deprecated_named_route(name, deprecated_name, path, options) unless deprecated_name == name
end

# Enables the use of resources in a module by setting the name_prefix, path_prefix, and namespace for the model.
# Example:
#
Expand Down Expand Up @@ -1052,7 +1057,7 @@ def method_missing(route_name, *args, &proc)
class NamedRouteCollection #:nodoc:
include Enumerable

attr_reader :routes, :helpers
attr_reader :routes, :helpers, :deprecated_named_routes

def initialize
clear!
Expand All @@ -1061,6 +1066,7 @@ def initialize
def clear!
@routes = {}
@helpers = []
@deprecated_named_routes = {}

@module ||= Module.new
@module.instance_methods.each do |selector|
Expand All @@ -1074,6 +1080,12 @@ def add(name, route)
end

def get(name)
if @deprecated_named_routes.has_key?(name.to_sym)
ActiveSupport::Deprecation.warn(
"The named route \"#{name}\" uses a format that has been deprecated. " +
"You should use \"#{@deprecated_named_routes[name]}\" instead", caller
)
end
routes[name.to_sym]
end

Expand Down Expand Up @@ -1169,7 +1181,7 @@ def initialize
self.routes = []
self.named_routes = NamedRouteCollection.new
end

# Subclasses and plugins may override this method to specify a different
# RouteBuilder instance, so that other route DSL's can be created.
def builder
Expand Down Expand Up @@ -1222,10 +1234,16 @@ def add_route(path, options = {})
end

def add_named_route(name, path, options = {})
# TODO - is options EVER used?
name = options[:name_prefix] + name.to_s if options[:name_prefix]
named_routes[name.to_sym] = add_route(path, options)
end

def add_deprecated_named_route(name, deprecated_name, path, options = {})
add_named_route(deprecated_name, path, options)
named_routes.deprecated_named_routes[deprecated_name.to_sym] = name
end

def options_as_params(options)
# If an explicit :controller was given, always make :action explicit
# too, so that action expiry works as expected for things like
Expand Down

0 comments on commit 557e193

Please sign in to comment.