Skip to content

Instantly share code, notes, and snippets.

@between40and2
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save between40and2/5fa56f35f0636d565c80 to your computer and use it in GitHub Desktop.
Save between40and2/5fa56f35f0636d565c80 to your computer and use it in GitHub Desktop.
Rails Routing System components

input part

use part

##Methodology to examine routing system.

Playing with Rails console

$ rails c
> app.main_app.routes  
    => #<ActionDispatch::Routing::RouteSet:0x007fe88539eae8> 
> app.main_app.routes.url_helpers.ancestors
    => [#<Module:0x007fe883dd2070>, #<Module:0x007fe88539e9a8>] 
>     

Digging source code

$ find . -type f | xargs grep add_route
./actionpack/lib/action_dispatch/journey/routes.rb:    # added to the table by calling Routes#add_route.
./actionpack/lib/action_dispatch/journey/routes.rb:      def add_route(app, path, conditions, defaults, name = nil)
./actionpack/lib/action_dispatch/routing/mapper.rb:              add_route(path, options)
./actionpack/lib/action_dispatch/routing/mapper.rb:        def add_route(action, options) # :nodoc:
./actionpack/lib/action_dispatch/routing/mapper.rb:          @set.add_route(app, conditions, requirements, defaults, as, anchor)
./actionpack/lib/action_dispatch/routing/route_set.rb:      def add_route(app, conditions = {}, requirements = {}, defaults = {}, name = nil, anchor = true)
./actionpack/lib/action_dispatch/routing/route_set.rb:        route = @set.add_route(app, path, conditions, defaults, name)

mapper, as a generator/input of routing system.

Call Chain

in Resources::Resource

get/post -> map_method -> match -> decomposed_match -> add_route

What you usually see in routes.rb file, are related to ActionDispatch::Routing::RouteSet

JxRailsMd::Engine.routes.draw do

When you call Rails::Application.reload_routes!, it will return an array of ActionDispatch::Routing::RouteSet .

If you are running an app with one engine invovled, this returns an array of 2 elements.

  • one for app named main_app,
  • one for engine named after the engine, such as jx_rails_md.

Class of ActionDispatch::Routing::RouteSet

mounted?
_generate_prefix

ActionDispatch::Routing::RouteSet

inspect
formatter
formatter=

named_routes                    # ActionDispatch::Routing::RouteSet::NamedRouteCollection 
named_routes=
default_scope
default_scope=

disable_clear_and_finalize
disable_clear_and_finalize=
resources_path_names
resources_path_names=
default_url_options
default_url_options=
request_class
request_class=

draw
append
prepend
eval_block
finalize!
clear!
mounted_helpers
define_mounted_helper
url_helpers
empty?
add_route
extra_keys
generate_extras
generate
optimize_routes_generation?
url_for
call
recognize_path

url_helpers                 # a unnamed module

Following methods are Journey related

formatter                       # ActionDispatch::Journey::Formatter 
formatter=
router                          # ActionDispatch::Journey::Router 
router=
routes                          # ActionDispatch::Journey::Routes
set                             # same as `routes`, comparing __id__
set=

Ways to RoutesProxy

controller#main_app

app#main_app

routes_set#??

Implementation in active_dispatch/rounting/routes_proxy.rb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment