Skip to content

Instantly share code, notes, and snippets.

@glebm
Created November 6, 2012 13:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save glebm/4024878 to your computer and use it in GitHub Desktop.
Save glebm/4024878 to your computer and use it in GitHub Desktop.
Get subdomains working in tests (scary)
# -*- encoding : utf-8 -*-
module UrlSubdomain
def subdomain!(sub = 'account')
# Some day this hack won't be necessary
# (when Capybara, Rspec, and Rails learn to work together)
# For now it only works if we run it twice
# I do not know why, and it would be a waste of time to find out
2.times do
sub = 'account' if sub.blank?
host = "#{sub}.#{DOMAIN}"
@http_host = "http://#{host}"
Capybara.app_host = @http_host if defined?(Capybara)
page.driver.browser.current_host = @http_host if respond_to?(:page)
default_url_opts = {locale: I18n.locale, host: host}
begin
# Prevent caching of :@url_options
if integration_session
integration_session.instance_variable_set(:@url_options, nil)
else
reset!
end
rescue
nil
end
self.default_url_options = default_url_opts if respond_to? :default_url_options=
# Hack to work around https://github.com/rspec/rspec-rails/issues/255
# or maybe https://github.com/rails/journey/issues/42
ActionDispatch::Routing::RouteSet.send :define_method, :default_url_options do
default_url_opts
end
ActionDispatch::Routing::RoutesProxy.default_url_options = default_url_opts
host!(host) rescue nil
BaseMailer.subdomain! sub
end
debug_log "SET HOST TO #@http_host"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment