Skip to content

Instantly share code, notes, and snippets.

@gumayunov
Created August 30, 2010 17:48
Show Gist options
  • Save gumayunov/557758 to your computer and use it in GitHub Desktop.
Save gumayunov/557758 to your computer and use it in GitHub Desktop.
module RoutingFilter
class OldSiteRewrite < Filter
def around_recognize(path, env, &block)
additional_params = {}
if path =~ /^\/?([\w_-]+)\/?$/ && not_reserved($1)
path.replace "/categories/#{$1}"
elsif path =~ /^\/?([\w_-]+)\/([\w_-]+)\/?$/ && not_reserved($1)
path.replace "/reviews/#{$2}"
additional_params[:category] = $1
end
yield.tap do |params|
additional_params.each_pair{|k,v| params[k] =v}
end
end
def around_generate(params, &block)
category = params.delete :category
yield.tap do |result|
if params[:controller] == 'categories'
result[0].sub! "/categories", ""
elsif params[:controller] == 'reviews' && category
result[0].sub! "/reviews", "/#{category}"
end
end
end
private
def not_reserved(word)
return !(%w{reviews interviews categories tags pages}.include? word)
end
end
end
ActionController::Routing::Routes.draw do |map|
map.connect 'pix', :controller => 'reviews', :action => 'index_logos'
map.connect 'one', :controller => 'reviews', :action => 'index_onepage'
map.filter :old_site_rewrite
map.resources :reviews
map.resources :interviews
map.resources :categories
map.resources :tags
map.resources :pages
map.root :controller => 'reviews', :action => 'index'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment