Skip to content

Instantly share code, notes, and snippets.

@Lordnibbler
Last active October 31, 2021 02:36
Show Gist options
  • Save Lordnibbler/7539651 to your computer and use it in GitHub Desktop.
Save Lordnibbler/7539651 to your computer and use it in GitHub Desktop.

Revisions

  1. Lordnibbler revised this gist Nov 19, 2013. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions integration_spec.rb
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,8 @@

    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
  2. Lordnibbler revised this gist Nov 19, 2013. 1 changed file with 15 additions and 0 deletions.
    15 changes: 15 additions & 0 deletions url_helper.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    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
  3. Lordnibbler created this gist Nov 19, 2013.
    10 changes: 10 additions & 0 deletions domain_constraint.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    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
    7 changes: 7 additions & 0 deletions integration_spec.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    require 'spec_helper'

    describe "Something", js: true do
    it "is a fake spec" do
    visit foo_path(subdomain: 'another')
    end
    end
    5 changes: 5 additions & 0 deletions routes.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    MyApp::Application.routes.draw do
    constraints DomainConstraint.new(['subdomain.domain.com', 'another.domain.com']) do
    resources :foo
    end
    end