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.
rails>>
map.with_options(:controller => 'posts',
:name_prefix => 'posts_',
:path_prefix => 'posts') do |posts|
posts_rss_path => 'posts/rss'
posts.rss 'rss', :action => 'rss'
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.
rails>>
map.with_controller('posts') do |posts|
posts.rss # posts_rss_path => 'posts/rss'
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.
rails>>
class MapperWithController
def initialize(mapper)
@mapper = mapper
end
def method_missing(name, *args, &block)
path, *args = args
path ||= name.to_s
args.unshift path
@mapper.send name, *args, &block
end
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.