I think that the complex Rails applications
such as maintained over years tend to have messy config/routes.
Today I'd like to introduce a simple way to keep them clean.
The way is really simple. Use with_options for each controller with prefix options.
1 map.with_options(:controller => 'posts',
2 :name_prefix => 'posts_',
3 :path_prefix => 'posts') do |posts|
4
5 posts.rss 'rss', :action => 'rss'
6 end
Still complex?
Okey, I have written the plugin for you.
With this plugin, you can draw the routes belonging to some controller like this.
1 map.with_controller('posts') do |posts|
2 posts.rss
3 end
As you can see, there's one more thing.
I have also installed MapperWithController class into RouteSet at this plugin for making named routes be completely DRY.
1 class MapperWithController
2 def initialize(mapper)
3 @mapper = mapper
4 end
5
6 def method_missing(name, *args, &block)
7 path, *args = args
8 path ||= name.to_s
9 args.unshift path
10 @mapper.send name, *args, &block
11 end
12 end
map.namespace :posts
does nearly same thing, but it couldn't be used for this purpose because of it is optimized to making namespaces for a routing.