Skip to content
This repository
Browse code

Add has_named_route? to the mapper API

  • Loading branch information...
commit 378ea96905e5f95be1a413b7bdd6fbb000265129 1 parent 97a607b
José Valim authored May 20, 2013
5  actionpack/lib/action_dispatch/routing/mapper.rb
@@ -515,6 +515,11 @@ def with_default_scope(scope, &block)
515 515
           end
516 516
         end
517 517
 
  518
+        # Query if the following named route was already defined.
  519
+        def has_named_route?(name)
  520
+          @set.named_routes.routes[name.to_sym]
  521
+        end
  522
+
518 523
         private
519 524
           def app_name(app)
520 525
             return unless app.respond_to?(:routes)
13  actionpack/test/dispatch/routing_test.rb
@@ -2662,6 +2662,19 @@ def test_redirect_argument_error
2662 2662
     assert_raises(ArgumentError) { routes.redirect Object.new }
2663 2663
   end
2664 2664
 
  2665
+  def test_named_route_check
  2666
+    before, after = nil
  2667
+
  2668
+    draw do
  2669
+      before = has_named_route?(:hello)
  2670
+      get "/hello", as: :hello, to: "hello#world"
  2671
+      after = has_named_route?(:hello)
  2672
+    end
  2673
+
  2674
+    assert !before, "expected to not have named route :hello before route definition"
  2675
+    assert after, "expected to have named route :hello after route definition"
  2676
+  end
  2677
+
2665 2678
   def test_explicitly_avoiding_the_named_route
2666 2679
     draw do
2667 2680
       scope :as => "routes" do

0 notes on commit 378ea96

Please sign in to comment.
Something went wrong with that request. Please try again.