Skip to content

Instantly share code, notes, and snippets.

@zliang-min
Created February 2, 2010 16:25
Show Gist options
  • Save zliang-min/292786 to your computer and use it in GitHub Desktop.
Save zliang-min/292786 to your computer and use it in GitHub Desktop.
add host condition to rails route
ActionController::Routing::Routes.draw do |map|
map.connect "/", :controller => "admin",
:conditions => { :subdomain => "admin" }
map.connect "/", :controller => "blog"
# ...
end
# jamisbuck/routing/routeset.rb
module JamisBuck
module Routing
module RouteSetExtensions
def self.included(base)
base.alias_method_chain :extract_request_environment, :host
end
def extract_request_environment_with_host(request)
env = extract_request_environment_without_host(request)
env.merge :host => request.host,
:domain => request.domain, :subdomain => request.subdomains.first
end
end
end
end
# jamis_buck/routing/route.rb
module JamisBuck
module Routing
module RouteExtensions
def self.included(base)
base.alias_method_chain :recognition_conditions, :host
end
def recognition_conditions_with_host
result = recognition_conditions_without_host
result << "conditions[:host] === env[:host]" if conditions[:host]
result << "conditions[:domain] === env[:domain]" if conditions[:domain]
result << "conditions[:subdomain] === env[:subdomain]" if conditions[:subdomain]
result
end
end
end
end
def recognize(path, env={})
if (match = /\A\/?\Z/.match(path)) && conditions[:subdomain] === env[:subdomain]
params = parameter_shell.dup
params
end
end
# init.rb
require 'jamis_buck/routing/routeset'
require 'jamis_buck/routing/route'
require 'action_controller/routing'
ActionController::Routing::RouteSet.send :include,
JamisBuck::Routing::RouteSetExtensions
ActionController::Routing::Route.send :include,
JamisBuck::Routing::RouteExtensions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment