mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Allow name_prefix to be pass into scope
This commit is contained in:
parent
40ad54e381
commit
e8489b43e2
2 changed files with 24 additions and 48 deletions
|
@ -4,16 +4,12 @@ module ActionDispatch
|
||||||
module Resources
|
module Resources
|
||||||
class Resource #:nodoc:
|
class Resource #:nodoc:
|
||||||
attr_reader :plural, :singular
|
attr_reader :plural, :singular
|
||||||
attr_reader :path_prefix, :name_prefix
|
|
||||||
|
|
||||||
def initialize(entities, options = {})
|
def initialize(entities, options = {})
|
||||||
entities = entities.to_s
|
entities = entities.to_s
|
||||||
|
|
||||||
@plural = entities.pluralize
|
@plural = entities.pluralize
|
||||||
@singular = entities.singularize
|
@singular = entities.singularize
|
||||||
|
|
||||||
@path_prefix = options[:path_prefix]
|
|
||||||
@name_prefix = options[:name_prefix]
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def name
|
def name
|
||||||
|
@ -25,41 +21,17 @@ module ActionDispatch
|
||||||
end
|
end
|
||||||
|
|
||||||
def member_name
|
def member_name
|
||||||
if name_prefix
|
|
||||||
"#{name_prefix}_#{singular}"
|
|
||||||
else
|
|
||||||
singular
|
singular
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
def collection_name
|
def collection_name
|
||||||
if name_prefix
|
|
||||||
"#{name_prefix}_#{plural}"
|
|
||||||
else
|
|
||||||
plural
|
plural
|
||||||
end
|
end
|
||||||
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
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
class SingletonResource < Resource #:nodoc:
|
class SingletonResource < Resource #:nodoc:
|
||||||
def initialize(entity, options = {})
|
def initialize(entity, options = {})
|
||||||
super(entity)
|
super
|
||||||
end
|
end
|
||||||
|
|
||||||
def name
|
def name
|
||||||
|
@ -76,8 +48,7 @@ module ActionDispatch
|
||||||
return self
|
return self
|
||||||
end
|
end
|
||||||
|
|
||||||
name_prefix = @scope[:options][:name_prefix] if @scope[:options]
|
resource = SingletonResource.new(resources.pop)
|
||||||
resource = SingletonResource.new(resources.pop, :name_prefix => name_prefix)
|
|
||||||
|
|
||||||
if @scope[:scope_level] == :resources
|
if @scope[:scope_level] == :resources
|
||||||
parent_resource = @scope[:scope_level_options][:name]
|
parent_resource = @scope[:scope_level_options][:name]
|
||||||
|
@ -99,8 +70,8 @@ module ActionDispatch
|
||||||
post "", :to => :create
|
post "", :to => :create
|
||||||
put "", :to => :update
|
put "", :to => :update
|
||||||
delete "", :to => :destroy
|
delete "", :to => :destroy
|
||||||
get "new", :to => :new, :as => resource.new_name
|
get "new", :to => :new, :as => "new_#{resource.singular}"
|
||||||
get "edit", :to => :edit, :as => resource.edit_name
|
get "edit", :to => :edit, :as => "edit_#{resource.singular}"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -117,8 +88,7 @@ module ActionDispatch
|
||||||
return self
|
return self
|
||||||
end
|
end
|
||||||
|
|
||||||
name_prefix = @scope[:options][:name_prefix] if @scope[:options]
|
resource = Resource.new(resources.pop)
|
||||||
resource = Resource.new(resources.pop, :name_prefix => name_prefix)
|
|
||||||
|
|
||||||
if @scope[:scope_level] == :resources
|
if @scope[:scope_level] == :resources
|
||||||
parent_resource = @scope[:scope_level_options][:name]
|
parent_resource = @scope[:scope_level_options][:name]
|
||||||
|
@ -139,14 +109,14 @@ module ActionDispatch
|
||||||
collection do
|
collection do
|
||||||
get "", :to => :index, :as => resource.collection_name
|
get "", :to => :index, :as => resource.collection_name
|
||||||
post "", :to => :create
|
post "", :to => :create
|
||||||
get "new", :to => :new, :as => resource.new_name
|
get "new", :to => :new, :as => "new_#{resource.singular}"
|
||||||
end
|
end
|
||||||
|
|
||||||
member do
|
member do
|
||||||
get "", :to => :show, :as => resource.member_name
|
get "", :to => :show, :as => resource.member_name
|
||||||
put "", :to => :update
|
put "", :to => :update
|
||||||
delete "", :to => :destroy
|
delete "", :to => :destroy
|
||||||
get "edit", :to => :edit, :as => resource.edit_name
|
get "edit", :to => :edit, :as => "edit_#{resource.singular}"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -230,6 +200,13 @@ module ActionDispatch
|
||||||
path_set = false
|
path_set = false
|
||||||
end
|
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)
|
if controller = options.delete(:controller)
|
||||||
controller_set = true
|
controller_set = true
|
||||||
controller, @scope[:controller] = @scope[:controller], controller
|
controller, @scope[:controller] = @scope[:controller], controller
|
||||||
|
@ -251,6 +228,7 @@ module ActionDispatch
|
||||||
self
|
self
|
||||||
ensure
|
ensure
|
||||||
@scope[:path] = path if path_set
|
@scope[:path] = path if path_set
|
||||||
|
@scope[:name_prefix] = name_prefix if name_prefix_set
|
||||||
@scope[:controller] = controller if controller_set
|
@scope[:controller] = controller if controller_set
|
||||||
@scope[:options] = options
|
@scope[:options] = options
|
||||||
@scope[:blocks] = blocks
|
@scope[:blocks] = blocks
|
||||||
|
@ -404,7 +382,9 @@ module ActionDispatch
|
||||||
validate_defaults!(app, defaults, segment_keys)
|
validate_defaults!(app, defaults, segment_keys)
|
||||||
app = Constraints.new(app, blocks)
|
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
|
self
|
||||||
end
|
end
|
||||||
|
|
|
@ -93,7 +93,7 @@ class TestRoutingMapper < ActionDispatch::IntegrationTest
|
||||||
end
|
end
|
||||||
|
|
||||||
controller :articles do
|
controller :articles do
|
||||||
scope 'articles' do
|
scope 'articles', :name_prefix => 'article' do
|
||||||
scope :path => ':title', :title => /[a-z]+/, :as => :with_title do
|
scope :path => ':title', :title => /[a-z]+/, :as => :with_title do
|
||||||
match ':id', :to => :with_id
|
match ':id', :to => :with_id
|
||||||
end
|
end
|
||||||
|
@ -255,11 +255,9 @@ class TestRoutingMapper < ActionDispatch::IntegrationTest
|
||||||
|
|
||||||
get '/projects/1/companies/1/avatar'
|
get '/projects/1/companies/1/avatar'
|
||||||
assert_equal 'avatars#show', @response.body
|
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')
|
assert_equal '/projects/1/companies/1/avatar', project_company_avatar_path(:project_id => '1', :company_id => '1')
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
def test_project_images
|
def test_project_images
|
||||||
with_test_routes do
|
with_test_routes do
|
||||||
|
@ -337,9 +335,7 @@ class TestRoutingMapper < ActionDispatch::IntegrationTest
|
||||||
|
|
||||||
get '/projects/1/posts/1/subscription'
|
get '/projects/1/posts/1/subscription'
|
||||||
assert_equal 'subscriptions#show', @response.body
|
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')
|
assert_equal '/projects/1/posts/1/subscription', project_post_subscription_path(:project_id => '1', :post_id => '1')
|
||||||
end
|
|
||||||
|
|
||||||
get '/projects/1/posts/1/comments'
|
get '/projects/1/posts/1/comments'
|
||||||
assert_equal 'comments#index', @response.body
|
assert_equal 'comments#index', @response.body
|
||||||
|
@ -407,7 +403,7 @@ class TestRoutingMapper < ActionDispatch::IntegrationTest
|
||||||
|
|
||||||
assert_raise(ActionController::RoutingError) { get '/articles/123/1' }
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue