Skip to content

Instantly share code, notes, and snippets.

@Lordnibbler
Last active October 31, 2021 02:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Lordnibbler/7539651 to your computer and use it in GitHub Desktop.
Save Lordnibbler/7539651 to your computer and use it in GitHub Desktop.
class DomainConstraint
def initialize(domain)
@domains = [domain].flatten
end
def matches?(request)
request.subdomain.present? ? domain_to_match = request.subdomain + "." + request.domain : domain_to_match = request.domain
@domains.include? domain_to_match
end
end
require 'spec_helper'
describe "Something", js: true do
it "is a fake spec" do
# this won't work in CI/test environments
# 'another.localhost:3001' is not mapped in /etc/hosts
visit foo_path(subdomain: 'another')
end
end
MyApp::Application.routes.draw do
constraints DomainConstraint.new(['subdomain.domain.com', 'another.domain.com']) do
resources :foo
end
end
module UrlHelper
def with_subdomain(subdomain)
subdomain = (subdomain || "")
subdomain += "." unless subdomain.empty?
[subdomain, request.domain].join
end
# allow link_to :subdomain => ""
def url_for(options = nil)
if options.kind_of?(Hash) && options.has_key?(:subdomain)
options[:host] = with_subdomain(options.delete(:subdomain))
end
super
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment