Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Introduce map.resources :cards, :as => 'tarjetas' to use a custom res…

…ource name in the URL: cards_path == '/tarjetas'. Closes #10578.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8785 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit fadaba679ac81aaab5c035bce61a369c1b33b1fe 1 parent 9e1d91c
Jeremy Kemper jeremy authored
2  actionpack/CHANGELOG
View
@@ -1,5 +1,7 @@
*SVN*
+* Introduce map.resources :cards, :as => 'tarjetas' to use a custom resource name in the URL: cards_path == '/tarjetas'. #10578 [blj]
+
* TestSession supports indifferent access. #7372 [tamc, Arsen7, mhackett, julik, jean.helou]
* Make assert_routing aware of the HTTP method used. #8039 [mpalmer]
12 actionpack/lib/action_controller/resources.rb
View
@@ -44,13 +44,14 @@ module ActionController
module Resources
class Resource #:nodoc:
attr_reader :collection_methods, :member_methods, :new_methods
- attr_reader :path_prefix, :name_prefix
+ attr_reader :path_prefix, :name_prefix, :path_segment
attr_reader :plural, :singular
attr_reader :options
def initialize(entities, options)
@plural ||= entities
@singular ||= options[:singular] || plural.to_s.singularize
+ @path_segment = options.delete(:as) || @plural
@options = options
@@ -75,7 +76,7 @@ def conditions
end
def path
- @path ||= "#{path_prefix}/#{plural}"
+ @path ||= "#{path_prefix}/#{path_segment}"
end
def new_path
@@ -236,6 +237,13 @@ def initialize(entity, options)
# * <tt>:singular</tt> - specify the singular name used in the member routes.
# * <tt>:requirements</tt> - set custom routing parameter requirements.
# * <tt>:conditions</tt> - specify custom routing recognition conditions. Resources sets the :method value for the method-specific routes.
+ # * <tt>:as</tt> - specify a different resource name to use in the URL path. For example:
+ # # products_path == '/productos'
+ # map.resources :products, :as => 'productos' do |product|
+ # # product_reviews_path(product) == '/productos/1234/comentarios'
+ # product.resources :product_reviews, :as => 'comentarios'
+ # end
+ #
# * <tt>:path_prefix</tt> - set a prefix to the routes with required route variables.
#
# Weblog comments usually belong to a post, so you might use resources like:
39 actionpack/test/controller/resources_test.rb
View
@@ -609,6 +609,35 @@ def test_nested_resources_in_nested_namespace
end
end
+ def test_with_path_segment
+ with_restful_routing :messages, :as => 'reviews' do
+ assert_simply_restful_for :messages, :as => 'reviews'
+ end
+ end
+
+ def test_multiple_with_path_segment_and_controller
+ with_routing do |set|
+ set.draw do |map|
+ map.resources :products do |products|
+ products.resources :product_reviews, :as => 'reviews', :controller => 'messages'
+ end
+ map.resources :tutors do |tutors|
+ tutors.resources :tutor_reviews, :as => 'reviews', :controller => 'comments'
+ end
+ end
+
+ assert_simply_restful_for :product_reviews, :controller=>'messages', :as => 'reviews', :name_prefix => 'product_', :path_prefix => 'products/1/', :options => {:product_id => '1'}
+ assert_simply_restful_for :tutor_reviews,:controller=>'comments', :as => 'reviews', :name_prefix => 'tutor_', :path_prefix => 'tutors/1/', :options => {:tutor_id => '1'}
+ end
+ end
+
+ def test_with_path_segment_path_prefix_requirements
+ expected_options = {:controller => 'messages', :action => 'show', :thread_id => '1.1.1', :id => '1'}
+ with_restful_routing :messages, :as => 'comments',:path_prefix => '/thread/:thread_id', :requirements => { :thread_id => /[0-9]\.[0-9]\.[0-9]/ } do
+ assert_recognizes(expected_options, :path => 'thread/1.1.1/comments/1', :method => :get)
+ end
+ end
+
protected
def with_restful_routing(*args)
with_routing do |set|
@@ -639,7 +668,7 @@ def assert_restful_routes_for(controller_name, options = {})
options[:options] ||= {}
options[:options][:controller] = options[:controller] || controller_name.to_s
- collection_path = "/#{options[:path_prefix]}#{controller_name}"
+ collection_path = "/#{options[:path_prefix]}#{options[:as] || controller_name}"
member_path = "#{collection_path}/1"
new_path = "#{collection_path}/new"
edit_member_path = "#{member_path}/edit"
@@ -692,9 +721,9 @@ def assert_restful_named_routes_for(controller_name, singular_name = nil, option
get :index, options[:options]
options[:options].delete :action
- full_prefix = "/#{options[:path_prefix]}#{controller_name}"
+ full_prefix = "/#{options[:path_prefix]}#{options[:as] || controller_name}"
name_prefix = options[:name_prefix]
-
+
assert_named_route "#{full_prefix}", "#{name_prefix}#{controller_name}_path", options[:options]
assert_named_route "#{full_prefix}.xml", "formatted_#{name_prefix}#{controller_name}_path", options[:options].merge( :format => 'xml')
assert_named_route "#{full_prefix}/1", "#{name_prefix}#{singular_name}_path", options[:options].merge(:id => '1')
@@ -712,7 +741,7 @@ def assert_singleton_routes_for(singleton_name, options = {})
options[:options] ||= {}
options[:options][:controller] = options[:controller] || singleton_name.to_s.pluralize
- full_path = "/#{options[:path_prefix]}#{singleton_name}"
+ full_path = "/#{options[:path_prefix]}#{options[:as] || singleton_name}"
new_path = "#{full_path}/new"
edit_path = "#{full_path}/edit"
formatted_edit_path = "#{full_path}/edit.xml"
@@ -751,7 +780,7 @@ def assert_singleton_named_routes_for(singleton_name, options = {})
get :show, options[:options]
options[:options].delete :action
- full_path = "/#{options[:path_prefix]}#{singleton_name}"
+ full_path = "/#{options[:path_prefix]}#{options[:as] || singleton_name}"
name_prefix = options[:name_prefix]
assert_named_route "#{full_path}", "#{name_prefix}#{singleton_name}_path", options[:options]
Please sign in to comment.
Something went wrong with that request. Please try again.