aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--actionpack/CHANGELOG2
-rw-r--r--actionpack/lib/action_controller/resources.rb12
-rw-r--r--actionpack/test/controller/resources_test.rb39
3 files changed, 46 insertions, 7 deletions
diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG
index 2e990c5b9b..4ae2cb1cd3 100644
--- a/actionpack/CHANGELOG
+++ b/actionpack/CHANGELOG
@@ -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]
diff --git a/actionpack/lib/action_controller/resources.rb b/actionpack/lib/action_controller/resources.rb
index 8ec6b84a53..52173bc8bb 100644
--- a/actionpack/lib/action_controller/resources.rb
+++ b/actionpack/lib/action_controller/resources.rb
@@ -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 @@ module ActionController
end
def path
- @path ||= "#{path_prefix}/#{plural}"
+ @path ||= "#{path_prefix}/#{path_segment}"
end
def new_path
@@ -236,6 +237,13 @@ module ActionController
# * <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:
diff --git a/actionpack/test/controller/resources_test.rb b/actionpack/test/controller/resources_test.rb
index 9031c633ca..c62a2043d9 100644
--- a/actionpack/test/controller/resources_test.rb
+++ b/actionpack/test/controller/resources_test.rb
@@ -609,6 +609,35 @@ class ResourcesTest < Test::Unit::TestCase
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 @@ class ResourcesTest < Test::Unit::TestCase
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 @@ class ResourcesTest < Test::Unit::TestCase
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 @@ class ResourcesTest < Test::Unit::TestCase
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 @@ class ResourcesTest < Test::Unit::TestCase
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]