1543059f424f9fe5b7654413e6c8dc22

I'm trying to retain the rails routing DSL but maintain it in two files. This is the only way I've found so far to get it to work.

I can't use another ActionController::Routing::Routes.draw block because here is the draw code from Rails:

def draw
clear!
yield Mapper.new(self)
install_helpers
end

Simply loading the file doesn't retain variable scope.

1
2
3
ActionController::Routing::Routes.draw do |map|
  eval(File.open(File.dirname(__FILE__) + '/../path/to/another/routes/file.rb', 'r').read)
end

Refactorings

No refactoring yet !

235c02c720a68fe290dd23562d3b5afc

Jesse Newland

March 7, 2008, March 07, 2008 23:29, permalink

No rating. Login to rate!

Works on edge. Why are you doing this, though?

duck punch!

1
2
3
4
5
6
7
8
9
10
module ActionController
  module Routing
    class RouteSet
      def add_routes
        yield Mapper.new(self)
        install_helpers([ActionController::Base, ActionView::Base], true)
      end    
    end
  end
end

first routes file

1
2
3
ActionController::Routing::Routes.draw do |map|
  map.resources :foo
end

second routes file

1
2
3
ActionController::Routing::Routes.add_routes do |map|
  map.resources :bar
end

third routes file

1
2
3
ActionController::Routing::Routes.add_routes do |map|
  map.resources :baz
end
70ca58d0e0e0eabbdb74d177417d09d7

leethal

April 7, 2008, April 07, 2008 10:58, permalink

No rating. Login to rate!

Routes

1
2
3
4
ActionController::Routing::Routes.draw do |map|
  require '../lib/extra_routes.rb' 
  ExtraRoutes.add(map)
end

The module (lib/extra_routes.rb)

1
2
3
4
5
6
7
8
module ExtraRoutes
  def add(map)
    map.resources :foo
    map.burger :bar
  end

  module_function :add
end

Your refactoring





Format Copy from initial code

or Cancel