Permalink
Browse files

Allow name_prefix to be pass into scope

  • Loading branch information...
1 parent 40ad54e commit e8489b43e2746d9b3605eef86731232fa823ce69 @josh josh committed Dec 8, 2009
Showing with 24 additions and 48 deletions.
  1. +20 −40 actionpack/lib/action_dispatch/routing/mapper.rb
  2. +4 −8 actionpack/test/dispatch/routing_test.rb
@@ -4,16 +4,12 @@ class Mapper
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 @@ def controller
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 @@ def resource(*resources, &block)
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 @@ def resource(*resources, &block)
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 @@ def resources(*resources, &block)
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 @@ def resources(*resources, &block)
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 @@ def scope(*args)
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 @@ def scope(*args)
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 @@ def match(*args)
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
@@ -93,7 +93,7 @@ def self.matches?(request)
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 @@ def test_projects_companies
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 @@ def test_projects_posts
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 @@ def test_articles_with_id
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

0 comments on commit e8489b4

Please sign in to comment.