提交 1f84061c 编写于 作者: A Andrew White 提交者: José Valim

Don't use module to work out shallow name prefix and path as it may not...

Don't use module to work out shallow name prefix and path as it may not accurately reflect the actual namespace [#4899 state:resolved]
Signed-off-by: NJosé Valim <jose.valim@gmail.com>
上级 ed3f042e
...@@ -33,7 +33,7 @@ def call(env) ...@@ -33,7 +33,7 @@ def call(env)
end end
class Mapping #:nodoc: class Mapping #:nodoc:
IGNORE_OPTIONS = [:to, :as, :controller, :action, :via, :on, :constraints, :defaults, :only, :except, :anchor, :shallow] IGNORE_OPTIONS = [:to, :as, :controller, :action, :via, :on, :constraints, :defaults, :only, :except, :anchor, :shallow, :shallow_path, :shallow_prefix]
def initialize(set, scope, args) def initialize(set, scope, args)
@set, @scope = set, scope @set, @scope = set, scope
...@@ -343,7 +343,7 @@ def controller(controller) ...@@ -343,7 +343,7 @@ def controller(controller)
def namespace(path) def namespace(path)
path = path.to_s path = path.to_s
scope(:path => path, :name_prefix => path, :module => path) { yield } scope(:path => path, :name_prefix => path, :module => path, :shallow_path => path, :shallow_prefix => path) { yield }
end end
def constraints(constraints = {}) def constraints(constraints = {})
...@@ -378,10 +378,18 @@ def merge_path_scope(parent, child) ...@@ -378,10 +378,18 @@ def merge_path_scope(parent, child)
Mapper.normalize_path("#{parent}/#{child}") Mapper.normalize_path("#{parent}/#{child}")
end end
def merge_shallow_path_scope(parent, child)
Mapper.normalize_path("#{parent}/#{child}")
end
def merge_name_prefix_scope(parent, child) def merge_name_prefix_scope(parent, child)
parent ? "#{parent}_#{child}" : child parent ? "#{parent}_#{child}" : child
end end
def merge_shallow_prefix_scope(parent, child)
parent ? "#{parent}_#{child}" : child
end
def merge_module_scope(parent, child) def merge_module_scope(parent, child)
parent ? "#{parent}/#{child}" : child parent ? "#{parent}/#{child}" : child
end end
...@@ -662,10 +670,10 @@ def nested ...@@ -662,10 +670,10 @@ def nested
with_scope_level(:nested) do with_scope_level(:nested) do
if parent_resource.shallow? if parent_resource.shallow?
with_exclusive_scope do with_exclusive_scope do
if @scope[:module].blank? if @scope[:shallow_path].blank?
scope(*parent_resource.nested_scope) { yield } scope(*parent_resource.nested_scope) { yield }
else else
scope(@scope[:module], :name_prefix => @scope[:module].tr('/', '_')) do scope(@scope[:shallow_path], :name_prefix => @scope[:shallow_prefix]) do
scope(*parent_resource.nested_scope) { yield } scope(*parent_resource.nested_scope) { yield }
end end
end end
...@@ -848,7 +856,7 @@ def path_for_action(action, path_names) ...@@ -848,7 +856,7 @@ def path_for_action(action, path_names)
"#{@scope[:path]}(.:format)" "#{@scope[:path]}(.:format)"
when :show, :update, :destroy when :show, :update, :destroy
if parent_resource.shallow? if parent_resource.shallow?
"#{@scope[:module]}/#{parent_resource.path}/:id(.:format)" "#{@scope[:shallow_path]}/#{parent_resource.path}/:id(.:format)"
else else
"#{@scope[:path]}(.:format)" "#{@scope[:path]}(.:format)"
end end
...@@ -856,7 +864,7 @@ def path_for_action(action, path_names) ...@@ -856,7 +864,7 @@ def path_for_action(action, path_names)
"#{@scope[:path]}/#{action_path(:new)}(.:format)" "#{@scope[:path]}/#{action_path(:new)}(.:format)"
when :edit when :edit
if parent_resource.shallow? if parent_resource.shallow?
"#{@scope[:module]}/#{parent_resource.path}/:id/#{action_path(:edit)}(.:format)" "#{@scope[:shallow_path]}/#{parent_resource.path}/:id/#{action_path(:edit)}(.:format)"
else else
"#{@scope[:path]}/#{action_path(:edit)}(.:format)" "#{@scope[:path]}/#{action_path(:edit)}(.:format)"
end end
...@@ -866,7 +874,7 @@ def path_for_action(action, path_names) ...@@ -866,7 +874,7 @@ def path_for_action(action, path_names)
"#{@scope[:path]}/#{action_path(action)}(.:format)" "#{@scope[:path]}/#{action_path(action)}(.:format)"
else else
if parent_resource.shallow? if parent_resource.shallow?
"#{@scope[:module]}/#{parent_resource.path}/:id/#{action_path(action)}(.:format)" "#{@scope[:shallow_path]}/#{parent_resource.path}/:id/#{action_path(action)}(.:format)"
else else
"#{@scope[:path]}/#{action_path(action)}(.:format)" "#{@scope[:path]}/#{action_path(action)}(.:format)"
end end
...@@ -880,7 +888,7 @@ def path_for_custom_action ...@@ -880,7 +888,7 @@ def path_for_custom_action
@scope[:path] @scope[:path]
else else
if parent_resource.shallow? if parent_resource.shallow?
"#{@scope[:module]}/#{parent_resource.path}/:id" "#{@scope[:shallow_path]}/#{parent_resource.path}/:id"
else else
@scope[:path] @scope[:path]
end end
...@@ -901,7 +909,7 @@ def options_for_action(action, options) ...@@ -901,7 +909,7 @@ def options_for_action(action, options)
def name_for_action(action) def name_for_action(action)
name_prefix = @scope[:name_prefix].blank? ? "" : "#{@scope[:name_prefix]}_" name_prefix = @scope[:name_prefix].blank? ? "" : "#{@scope[:name_prefix]}_"
shallow_prefix = @scope[:module].blank? ? "" : "#{@scope[:module].tr('/', '_')}_" shallow_prefix = @scope[:shallow_prefix].blank? ? "" : "#{@scope[:shallow_prefix]}_"
case action case action
when :index, :create when :index, :create
......
...@@ -278,8 +278,11 @@ def self.matches?(request) ...@@ -278,8 +278,11 @@ def self.matches?(request)
resource :dashboard, :constraints => { :ip => /192\.168\.1\.\d{1,3}/ } resource :dashboard, :constraints => { :ip => /192\.168\.1\.\d{1,3}/ }
scope :module => 'api' do scope :module => :api do
resource :token resource :token
resources :errors, :shallow => true do
resources :notices
end
end end
scope :path => 'api' do scope :path => 'api' do
...@@ -1350,6 +1353,18 @@ def test_custom_resource_routes_are_scoped ...@@ -1350,6 +1353,18 @@ def test_custom_resource_routes_are_scoped
end end
end end
def test_shallow_nested_routes_ignore_module
with_test_routes do
get '/errors/1/notices'
assert_equal 'api/notices#index', @response.body
assert_equal '/errors/1/notices', error_notices_path(:error_id => '1')
get '/notices/1'
assert_equal 'api/notices#show', @response.body
assert_equal '/notices/1', notice_path(:id => '1')
end
end
private private
def with_test_routes def with_test_routes
yield yield
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册