Rails and Stuff at Abel killed Cain

Ruby on Rails and other Webdevelopment

I had the following problem to solve using rails:

  1. Having 2 Subdomains, www and clients
  2. a client always has a path-prefix to use google analytics for each of this subdirs. so i had the following urls:
    www.domain.tld/foo.html  and clients.domain.tld/3kunsdf7w/foo.html
  3. I did not want to have a code on www.
  4. i did not want to alter all tempaltes with new paramters and differen routes

Solving this, it was ok for me to duplicate all routes with a client_  prefix using textmate, so i took onl 4 seconds.. but what next, how to tell rails to use the clients_xy_path when i call xy_path?

To keep it short, i did not find a nice  solution to let rails switch the routes. So i added a get_ to all of these path and urls via sed, also done fast. Now, instead of xy_path(:bar=>’foo’) the call was get_xy_path(:bar=>’foo’) – and yes, they really dont exist! :)

my goal:

1
2
3
def method
      client? ? clientspath : wwwpath
end

my solution:

1
2
3
4
5
6
7
8
9
10
11
12
13
module PathHelper
  def method_missing(method_id, *arguments)
     if match = /^get_([_a-zA-Z]\w*)_(path|url)$/.match(method_id.to_s)
       return (!session[:pid].nil? and session[:pid].to_i != 1) ?
          eval("client_#{match[1]}_#{match[2]}(arguments.first)") :
          eval("#{match[1]}_#{match[2]}(arguments.first)")
     end
  end
end
 
[ActionView::Base,ActionController::Base].each{|x|
  x.send :include, PathHelper
}

hope this helps anybody!

Any suggestions for a better solution w/o changing railscode?

Write a Comment