Last active
October 31, 2021 02:36
-
-
Save Lordnibbler/7539651 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
MyApp::Application.routes.draw do | |
constraints DomainConstraint.new(['subdomain.domain.com', 'another.domain.com']) do | |
resources :foo | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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