Skip to content
This repository
Browse code

Pass :as to resources to change the resource name

  • Loading branch information...
commit bf9b81e2cbfd4333f0b813ac07ea9d9c982e7779 1 parent 521ef3c
Joshua Peek josh authored
9 actionpack/lib/action_dispatch/routing/mapper.rb
@@ -328,13 +328,14 @@ class Resource #:nodoc:
328 328
329 329 def initialize(entities, options = {})
330 330 entities = entities.to_s
  331 + @options = options
331 332
332 333 @plural = entities.pluralize
333 334 @singular = entities.singularize
334 335 end
335 336
336 337 def name
337   - plural
  338 + @options[:as] || plural
338 339 end
339 340
340 341 def controller
@@ -360,7 +361,7 @@ def initialize(entity, options = {})
360 361 end
361 362
362 363 def name
363   - singular
  364 + @options[:as] || singular
364 365 end
365 366 end
366 367
@@ -373,7 +374,7 @@ def resource(*resources, &block)
373 374 return self
374 375 end
375 376
376   - resource = SingletonResource.new(resources.pop)
  377 + resource = SingletonResource.new(resources.pop, options)
377 378
378 379 if @scope[:scope_level] == :resources
379 380 nested do
@@ -407,7 +408,7 @@ def resources(*resources, &block)
407 408 return self
408 409 end
409 410
410   - resource = Resource.new(resources.pop)
  411 + resource = Resource.new(resources.pop, options)
411 412
412 413 if @scope[:scope_level] == :resources
413 414 nested do
26 actionpack/test/dispatch/routing_test.rb
@@ -109,6 +109,12 @@ def self.matches?(request)
109 109 end
110 110 end
111 111
  112 + namespace :forum do
  113 + resources :products, :as => '' do
  114 + resources :questions
  115 + end
  116 + end
  117 +
112 118 controller :articles do
113 119 scope '/articles', :name_prefix => 'article' do
114 120 scope :path => '/:title', :title => /[a-z]+/, :as => :with_title do
@@ -441,6 +447,26 @@ def test_update_project_person
441 447 end
442 448 end
443 449
  450 + def test_forum_products
  451 + with_test_routes do
  452 + get '/forum'
  453 + assert_equal 'forum/products#index', @response.body
  454 + assert_equal '/forum', forum_products_path
  455 +
  456 + get '/forum/basecamp'
  457 + assert_equal 'forum/products#show', @response.body
  458 + assert_equal '/forum/basecamp', forum_product_path(:id => 'basecamp')
  459 +
  460 + get '/forum/basecamp/questions'
  461 + assert_equal 'forum/questions#index', @response.body
  462 + assert_equal '/forum/basecamp/questions', forum_product_questions_path(:product_id => 'basecamp')
  463 +
  464 + get '/forum/basecamp/questions/1'
  465 + assert_equal 'forum/questions#show', @response.body
  466 + assert_equal '/forum/basecamp/questions/1', forum_product_question_path(:product_id => 'basecamp', :id => 1)
  467 + end
  468 + end
  469 +
444 470 def test_articles_perma
445 471 with_test_routes do
446 472 get '/articles/2009/08/18/rails-3'

0 comments on commit bf9b81e

Please sign in to comment.
Something went wrong with that request. Please try again.