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    # posts_rss_path => 'posts/rss'
   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 # posts_rss_path => '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.

posted by Png takiuchi on Fri 14 Mar 2008 at 19:00

Comments:

or Preview
Social Bookmarks
  • Delicious
  • B_entry452
  • Clip_16_12_w
Services from s21g
twpro(ツイプロ)
Twitterプロフィールを快適検索
地価2009
土地の値段を調べてみよう
MyRestaurant
自分だけのレストラン手帳
Formula
ブログに数式を埋め込める数式コミュニティ