Skip to content

Instantly share code, notes, and snippets.

@Jamedjo
Last active December 28, 2015 01:09
Show Gist options
  • Save Jamedjo/7418766 to your computer and use it in GitHub Desktop.
Save Jamedjo/7418766 to your computer and use it in GitHub Desktop.
Super basic rails subdomain library.

Simple Subdomain

Super simple library for using subdomains in your rails routes.

Lets you do

subdomain('online') do
  root :to => 'online#index'
end

in your routes.rb.

It also has subdomain(Subdomain::None) do which matches www.yourdomain.com as well as yourdomain.com, and subdomain(Subdomain::Any) do which matches as long as a subdomain other than www is present.

module Subdomain
module Any
def self.matches?(request)
request.subdomains.first.present? && request.subdomains.first != 'www'
end
end
module None
def self.matches?(request)
request.subdomains.first == 'www' || !request.subdomains.first.present?
end
end
def subdomain(subdomain)
if subdomain.class == Module
constraint = subdomain.method(:matches?)
else
constraint = lambda do |request|
request.subdomains.first.present? && request.subdomains.first == subdomain
end
end
constraints(constraint) do
yield
end
end
end
class ActionDispatch::Routing::Mapper
include ::Subdomain
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment