aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoshua Peek <josh@joshpeek.com>2009-12-07 19:24:33 -0600
committerJoshua Peek <josh@joshpeek.com>2009-12-07 19:24:33 -0600
commite8489b43e2746d9b3605eef86731232fa823ce69 (patch)
tree00ccf670669e6dae83fb17cf90689d9bf00b1dca
parent40ad54e3811913c2bc60c7ee292fa48862f12001 (diff)
downloadrails-e8489b43e2746d9b3605eef86731232fa823ce69.tar.gz
rails-e8489b43e2746d9b3605eef86731232fa823ce69.tar.bz2
rails-e8489b43e2746d9b3605eef86731232fa823ce69.zip
Allow name_prefix to be pass into scope
-rw-r--r--actionpack/lib/action_dispatch/routing/mapper.rb60
-rw-r--r--actionpack/test/dispatch/routing_test.rb12
2 files changed, 24 insertions, 48 deletions
diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb
index fab8a227bf..abcaa529ae 100644
--- a/actionpack/lib/action_dispatch/routing/mapper.rb
+++ b/actionpack/lib/action_dispatch/routing/mapper.rb
@@ -4,16 +4,12 @@ module ActionDispatch
module Resources
class Resource #:nodoc:
attr_reader :plural, :singular
- attr_reader :path_prefix, :name_prefix
def initialize(entities, options = {})
entities = entities.to_s
@plural = entities.pluralize
@singular = entities.singularize
-
- @path_prefix = options[:path_prefix]
- @name_prefix = options[:name_prefix]
end
def name
@@ -25,41 +21,17 @@ module ActionDispatch
end
def member_name
- if name_prefix
- "#{name_prefix}_#{singular}"
- else
- singular
- end
+ singular
end
def collection_name
- if name_prefix
- "#{name_prefix}_#{plural}"
- else
- plural
- end
- end
-
- def new_name
- if name_prefix
- "new_#{name_prefix}_#{singular}"
- else
- "new_#{singular}"
- end
- end
-
- def edit_name
- if name_prefix
- "edit_#{name_prefix}_#{singular}"
- else
- "edit_#{singular}"
- end
+ plural
end
end
class SingletonResource < Resource #:nodoc:
def initialize(entity, options = {})
- super(entity)
+ super
end
def name
@@ -76,8 +48,7 @@ module ActionDispatch
return self
end
- name_prefix = @scope[:options][:name_prefix] if @scope[:options]
- resource = SingletonResource.new(resources.pop, :name_prefix => name_prefix)
+ resource = SingletonResource.new(resources.pop)
if @scope[:scope_level] == :resources
parent_resource = @scope[:scope_level_options][:name]
@@ -99,8 +70,8 @@ module ActionDispatch
post "", :to => :create
put "", :to => :update
delete "", :to => :destroy
- get "new", :to => :new, :as => resource.new_name
- get "edit", :to => :edit, :as => resource.edit_name
+ get "new", :to => :new, :as => "new_#{resource.singular}"
+ get "edit", :to => :edit, :as => "edit_#{resource.singular}"
end
end
end
@@ -117,8 +88,7 @@ module ActionDispatch
return self
end
- name_prefix = @scope[:options][:name_prefix] if @scope[:options]
- resource = Resource.new(resources.pop, :name_prefix => name_prefix)
+ resource = Resource.new(resources.pop)
if @scope[:scope_level] == :resources
parent_resource = @scope[:scope_level_options][:name]
@@ -139,14 +109,14 @@ module ActionDispatch
collection do
get "", :to => :index, :as => resource.collection_name
post "", :to => :create
- get "new", :to => :new, :as => resource.new_name
+ get "new", :to => :new, :as => "new_#{resource.singular}"
end
member do
get "", :to => :show, :as => resource.member_name
put "", :to => :update
delete "", :to => :destroy
- get "edit", :to => :edit, :as => resource.edit_name
+ get "edit", :to => :edit, :as => "edit_#{resource.singular}"
end
end
end
@@ -230,6 +200,13 @@ module ActionDispatch
path_set = false
end
+ if name_prefix = options.delete(:name_prefix)
+ name_prefix_set = true
+ name_prefix, @scope[:name_prefix] = @scope[:name_prefix], (@scope[:name_prefix] ? "#{@scope[:name_prefix]}_#{name_prefix}" : name_prefix)
+ else
+ name_prefix_set = false
+ end
+
if controller = options.delete(:controller)
controller_set = true
controller, @scope[:controller] = @scope[:controller], controller
@@ -251,6 +228,7 @@ module ActionDispatch
self
ensure
@scope[:path] = path if path_set
+ @scope[:name_prefix] = name_prefix if name_prefix_set
@scope[:controller] = controller if controller_set
@scope[:options] = options
@scope[:blocks] = blocks
@@ -404,7 +382,9 @@ module ActionDispatch
validate_defaults!(app, defaults, segment_keys)
app = Constraints.new(app, blocks)
- @set.add_route(app, conditions, requirements, defaults, options[:as])
+ name = @scope[:name_prefix] ? "#{@scope[:name_prefix]}_#{options[:as]}" : options[:as]
+
+ @set.add_route(app, conditions, requirements, defaults, name)
self
end
diff --git a/actionpack/test/dispatch/routing_test.rb b/actionpack/test/dispatch/routing_test.rb
index 85616b5a59..86cf2ce335 100644
--- a/actionpack/test/dispatch/routing_test.rb
+++ b/actionpack/test/dispatch/routing_test.rb
@@ -93,7 +93,7 @@ class TestRoutingMapper < ActionDispatch::IntegrationTest
end
controller :articles do
- scope 'articles' do
+ scope 'articles', :name_prefix => 'article' do
scope :path => ':title', :title => /[a-z]+/, :as => :with_title do
match ':id', :to => :with_id
end
@@ -255,9 +255,7 @@ class TestRoutingMapper < ActionDispatch::IntegrationTest
get '/projects/1/companies/1/avatar'
assert_equal 'avatars#show', @response.body
- pending do
- assert_equal '/projects/1/companies/1/avatar', project_company_avatar_path(:project_id => '1', :company_id => '1')
- end
+ assert_equal '/projects/1/companies/1/avatar', project_company_avatar_path(:project_id => '1', :company_id => '1')
end
end
@@ -337,9 +335,7 @@ class TestRoutingMapper < ActionDispatch::IntegrationTest
get '/projects/1/posts/1/subscription'
assert_equal 'subscriptions#show', @response.body
- pending do
- assert_equal '/projects/1/posts/1/subscription', project_post_subscription_path(:project_id => '1', :post_id => '1')
- end
+ assert_equal '/projects/1/posts/1/subscription', project_post_subscription_path(:project_id => '1', :post_id => '1')
get '/projects/1/posts/1/comments'
assert_equal 'comments#index', @response.body
@@ -407,7 +403,7 @@ class TestRoutingMapper < ActionDispatch::IntegrationTest
assert_raise(ActionController::RoutingError) { get '/articles/123/1' }
- assert_equal '/articles/rails/1', with_title_path(:title => 'rails', :id => 1)
+ assert_equal '/articles/rails/1', article_with_title_path(:title => 'rails', :id => 1)
end
end