Skip to content

Commit

Permalink
Pass :as to resources to change the resource name
Browse files Browse the repository at this point in the history
  • Loading branch information
josh committed Jan 13, 2010
1 parent 521ef3c commit bf9b81e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
9 changes: 5 additions & 4 deletions actionpack/lib/action_dispatch/routing/mapper.rb
Expand Up @@ -328,13 +328,14 @@ class Resource #:nodoc:

def initialize(entities, options = {})
entities = entities.to_s
@options = options

@plural = entities.pluralize
@singular = entities.singularize
end

def name
plural
@options[:as] || plural
end

def controller
Expand All @@ -360,7 +361,7 @@ def initialize(entity, options = {})
end

def name
singular
@options[:as] || singular
end
end

Expand All @@ -373,7 +374,7 @@ def resource(*resources, &block)
return self
end

resource = SingletonResource.new(resources.pop)
resource = SingletonResource.new(resources.pop, options)

if @scope[:scope_level] == :resources
nested do
Expand Down Expand Up @@ -407,7 +408,7 @@ def resources(*resources, &block)
return self
end

resource = Resource.new(resources.pop)
resource = Resource.new(resources.pop, options)

if @scope[:scope_level] == :resources
nested do
Expand Down
26 changes: 26 additions & 0 deletions actionpack/test/dispatch/routing_test.rb
Expand Up @@ -109,6 +109,12 @@ def self.matches?(request)
end
end

namespace :forum do
resources :products, :as => '' do
resources :questions
end
end

controller :articles do
scope '/articles', :name_prefix => 'article' do
scope :path => '/:title', :title => /[a-z]+/, :as => :with_title do
Expand Down Expand Up @@ -441,6 +447,26 @@ def test_update_project_person
end
end

def test_forum_products
with_test_routes do
get '/forum'
assert_equal 'forum/products#index', @response.body
assert_equal '/forum', forum_products_path

get '/forum/basecamp'
assert_equal 'forum/products#show', @response.body
assert_equal '/forum/basecamp', forum_product_path(:id => 'basecamp')

get '/forum/basecamp/questions'
assert_equal 'forum/questions#index', @response.body
assert_equal '/forum/basecamp/questions', forum_product_questions_path(:product_id => 'basecamp')

get '/forum/basecamp/questions/1'
assert_equal 'forum/questions#show', @response.body
assert_equal '/forum/basecamp/questions/1', forum_product_question_path(:product_id => 'basecamp', :id => 1)
end
end

def test_articles_perma
with_test_routes do
get '/articles/2009/08/18/rails-3'
Expand Down

0 comments on commit bf9b81e

Please sign in to comment.