aboutsummaryrefslogblamecommitdiffstats
path: root/railties/doc/guides/source/creating_plugins/routes.txt
blob: dc1bf09fd17875b82551b43cc1bd38b3c9d11b56 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
            
 


                                                                                                                                                                                                                                            
 

                                            

                                                        















                                                                                                






                                                                                        


                                                                           
 

                                                        
                        

                                                        
                                             
 

                                                        








                                                                                             

                                                                                            

                                                        

                  

                                                        
                                               




                                                                                      
== Routes ==

In a standard 'routes.rb' file you use routes like 'map.connect' or 'map.resources'.  You can add your own custom routes from a plugin.  This section will describe how to add a custom method called that can be called with 'map.yaffles'.

Testing routes from plugins is slightly different from testing routes in a standard rails app.  To begin, add a test like this:

*vendor/plugins/yaffle/test/routing_test.rb*

[source, ruby]
--------------------------------------------------------
require "#{File.dirname(__FILE__)}/test_helper"

class RoutingTest < Test::Unit::TestCase

  def setup
    ActionController::Routing::Routes.draw do |map|
      map.yaffles
    end
  end

  def test_yaffles_route
    assert_recognition :get, "/yaffles", :controller => "yaffles_controller", :action => "index"
  end

  private

    def assert_recognition(method, path, options)
      result = ActionController::Routing::Routes.recognize_path(path, :method => method)
      assert_equal options, result
    end
end
--------------------------------------------------------

Once you see the tests fail by running 'rake', you can make them pass with:

*vendor/plugins/yaffle/lib/yaffle.rb*

[source, ruby]
--------------------------------------------------------
require "yaffle/routing"
--------------------------------------------------------

*vendor/plugins/yaffle/lib/yaffle/routing.rb*

[source, ruby]
--------------------------------------------------------
module Yaffle #:nodoc:
  module Routing #:nodoc:
    module MapperExtensions
      def yaffles
        @set.add_route("/yaffles", {:controller => "yaffles_controller", :action => "index"})
      end
    end
  end
end

ActionController::Routing::RouteSet::Mapper.send :include, Yaffle::Routing::MapperExtensions
--------------------------------------------------------

*config/routes.rb*

[source, ruby]
--------------------------------------------------------
ActionController::Routing::Routes.draw do |map|
  map.yaffles
end
--------------------------------------------------------

You can also see if your routes work by running `rake routes` from your app directory.