Froze rails gems
[depot.git] / vendor / rails / railties / doc / guides / source / creating_plugins / custom_route.txt
1 == Add a Custom Route ==
2
3 Testing routes in plugins can be complex, especially if the controllers are also in the plugin itself. Jamis Buck showed a great example of this in http://weblog.jamisbuck.org/2006/10/26/monkey-patching-rails-extending-routes-2.
4
5 *vendor/plugins/yaffle/test/routing_test.rb*
6
7 [source, ruby]
8 --------------------------------------------------------
9 require "#{File.dirname(__FILE__)}/test_helper"
10
11 class RoutingTest < Test::Unit::TestCase
12
13 def setup
14 ActionController::Routing::Routes.draw do |map|
15 map.yaffles
16 end
17 end
18
19 def test_yaffles_route
20 assert_recognition :get, "/yaffles", :controller => "yaffles_controller", :action => "index"
21 end
22
23 private
24
25 # yes, I know about assert_recognizes, but it has proven problematic to
26 # use in these tests, since it uses RouteSet#recognize (which actually
27 # tries to instantiate the controller) and because it uses an awkward
28 # parameter order.
29 def assert_recognition(method, path, options)
30 result = ActionController::Routing::Routes.recognize_path(path, :method => method)
31 assert_equal options, result
32 end
33 end
34 --------------------------------------------------------
35
36 *vendor/plugins/yaffle/init.rb*
37
38 [source, ruby]
39 --------------------------------------------------------
40 require "routing"
41 ActionController::Routing::RouteSet::Mapper.send :include, Yaffle::Routing::MapperExtensions
42 --------------------------------------------------------
43
44 *vendor/plugins/yaffle/lib/routing.rb*
45
46 [source, ruby]
47 --------------------------------------------------------
48 module Yaffle #:nodoc:
49 module Routing #:nodoc:
50 module MapperExtensions
51 def yaffles
52 @set.add_route("/yaffles", {:controller => "yaffles_controller", :action => "index"})
53 end
54 end
55 end
56 end
57 --------------------------------------------------------
58
59 *config/routes.rb*
60
61 [source, ruby]
62 --------------------------------------------------------
63 ActionController::Routing::Routes.draw do |map|
64 ...
65 map.yaffles
66 end
67 --------------------------------------------------------
68
69 You can also see if your routes work by running `rake routes` from your app directory.