From 192e55c38ed9b48672b9e216c9805b782b835d78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Fri, 16 Dec 2011 10:29:45 +0100 Subject: Do not raise an exception if an invalid route was generated automatically. --- actionpack/lib/action_dispatch/routing/mapper.rb | 14 +++++++++++--- actionpack/test/dispatch/routing_test.rb | 1 + activerecord/lib/active_record/relation/delegation.rb | 2 +- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb index 88e422c05d..4d83c6dee1 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -1286,7 +1286,7 @@ module ActionDispatch action = nil end - if !options.fetch(:as) { true } + if !options.fetch(:as, true) options.delete(:as) else options[:as] = name_for_action(options[:as], action) @@ -1472,8 +1472,16 @@ module ActionDispatch [name_prefix, member_name, prefix] end - candidate = name.select(&:present?).join("_").presence - candidate unless as.nil? && @set.routes.find { |r| r.name == candidate } + if candidate = name.select(&:present?).join("_").presence + # If a name was not explicitly given, we check if it is valid + # and return nil in case it isn't. Otherwise, we pass the invalid name + # forward so the underlying router engine treats it and raises an exception. + if as.nil? + candidate unless @set.routes.find { |r| r.name == candidate } || candidate !~ /\A[_a-z]/i + else + candidate + end + end end end diff --git a/actionpack/test/dispatch/routing_test.rb b/actionpack/test/dispatch/routing_test.rb index bc4e2e31c8..5325f81364 100644 --- a/actionpack/test/dispatch/routing_test.rb +++ b/actionpack/test/dispatch/routing_test.rb @@ -91,6 +91,7 @@ class TestRoutingMapper < ActionDispatch::IntegrationTest match "/local/:action", :controller => "local" match "/projects/status(.:format)" + match "/404", :to => lambda { |env| [404, {"Content-Type" => "text/plain"}, ["NOT FOUND"]] } constraints(:ip => /192\.168\.1\.\d\d\d/) do get 'admin' => "queenbee#index" diff --git a/activerecord/lib/active_record/relation/delegation.rb b/activerecord/lib/active_record/relation/delegation.rb index 47fcc08550..f5fdf437bf 100644 --- a/activerecord/lib/active_record/relation/delegation.rb +++ b/activerecord/lib/active_record/relation/delegation.rb @@ -8,7 +8,7 @@ module ActiveRecord :connection, :columns_hash, :auto_explain_threshold_in_seconds, :to => :klass def self.delegate_to_scoped_klass(method) - if method.to_s =~ /[a-z]\w*!?/ + if method.to_s =~ /\A[a-zA-Z_]\w*[!?]?\z/ module_eval <<-RUBY, __FILE__, __LINE__ + 1 def #{method}(*args, &block) scoping { @klass.#{method}(*args, &block) } -- cgit v1.2.3