1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Ensure that named routes do not overwrite previously defined routes.

This commit is contained in:
José Valim 2010-09-29 14:24:32 +02:00
parent b1ae796284
commit f63d35fba5
2 changed files with 31 additions and 10 deletions

View file

@ -62,7 +62,6 @@ module ActionDispatch
if using_match_shorthand?(path_without_format, @options) if using_match_shorthand?(path_without_format, @options)
to_shorthand = @options[:to].blank? to_shorthand = @options[:to].blank?
@options[:to] ||= path_without_format[1..-1].sub(%r{/([^/]*)$}, '#\1') @options[:to] ||= path_without_format[1..-1].sub(%r{/([^/]*)$}, '#\1')
@options[:as] ||= Mapper.normalize_name(path_without_format)
end end
@options.merge!(default_controller_and_action(to_shorthand)) @options.merge!(default_controller_and_action(to_shorthand))
@ -924,9 +923,14 @@ module ActionDispatch
if action.to_s =~ /^[\w\/]+$/ if action.to_s =~ /^[\w\/]+$/
options[:action] ||= action unless action.to_s.include?("/") options[:action] ||= action unless action.to_s.include?("/")
options[:as] = name_for_action(action, options[:as])
else else
options[:as] = name_for_action(options[:as]) action = nil
end
if options.key?(:as) && !options[:as]
options.delete(:as)
else
options[:as] = name_for_action(options[:as], action)
end end
super(path, options) super(path, options)
@ -1092,18 +1096,16 @@ module ActionDispatch
path || @scope[:path_names][name.to_sym] || name.to_s path || @scope[:path_names][name.to_sym] || name.to_s
end end
def prefix_name_for_action(action, as) def prefix_name_for_action(as, action)
if as.present? if as
as.to_s as.to_s
elsif as
nil
elsif !canonical_action?(action, @scope[:scope_level]) elsif !canonical_action?(action, @scope[:scope_level])
action.to_s action.to_s
end end
end end
def name_for_action(action, as=nil) def name_for_action(as, action)
prefix = prefix_name_for_action(action, as) prefix = prefix_name_for_action(as, action)
prefix = Mapper.normalize_name(prefix) if prefix prefix = Mapper.normalize_name(prefix) if prefix
name_prefix = @scope[:as] name_prefix = @scope[:as]
@ -1127,7 +1129,8 @@ module ActionDispatch
[name_prefix, member_name, prefix] [name_prefix, member_name, prefix]
end end
name.select(&:present?).join("_").presence candidate = name.select(&:present?).join("_").presence
candidate unless as.nil? && @set.routes.map(&:name).include?(candidate)
end end
end end

View file

@ -442,6 +442,15 @@ class TestRoutingMapper < ActionDispatch::IntegrationTest
get :preview, :on => :member get :preview, :on => :member
end end
scope :as => "routes" do
get "/c/:id", :as => :collision, :to => "collision#show"
get "/collision", :to => "collision#show"
get "/no_collision", :to => "collision#show", :as => nil
get "/fc/:id", :as => :forced_collision, :to => "forced_collision#show"
get "/forced_collision", :as => :forced_collision, :to => "forced_collision#show"
end
match '/purchases/:token/:filename', match '/purchases/:token/:filename',
:to => 'purchases#fetch', :to => 'purchases#fetch',
:token => /[[:alnum:]]{10}/, :token => /[[:alnum:]]{10}/,
@ -2120,6 +2129,15 @@ class TestRoutingMapper < ActionDispatch::IntegrationTest
assert_raises(ActionController::RoutingError){ list_todo_path(:list_id => '2', :id => '1') } assert_raises(ActionController::RoutingError){ list_todo_path(:list_id => '2', :id => '1') }
end end
def test_named_routes_collision_is_avoided_unless_explicitly_given_as
assert_equal "/c/1", routes_collision_path(1)
assert_equal "/forced_collision", routes_forced_collision_path
end
def test_explicitly_avoiding_the_named_route
assert !respond_to?(:routes_no_collision_path)
end
def test_controller_name_with_leading_slash_raise_error def test_controller_name_with_leading_slash_raise_error
assert_raise(ArgumentError) do assert_raise(ArgumentError) do
self.class.stub_controllers do |routes| self.class.stub_controllers do |routes|