Module: Rails::LocalSubdomain

Extended by:
ActiveSupport::Concern
Defined in:
lib/rails/local_subdomain.rb,
lib/rails/local_subdomain/version.rb

Overview

Redirects to a specified domain (or ‘’lvh.me’‘ if not provided) when Rails is running in an LocalSubdomain.enabled_environments environment.

Constant Summary collapse

VERSION =
'1.0.9'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.enabled_environmentsObject

Should be monkey-patched to configure which Rails environments will have lvh.me subdomain support.



17
18
19
# File 'lib/rails/local_subdomain.rb', line 17

def self.enabled_environments
  %w(development test)
end

.enabled_in?(env) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/rails/local_subdomain.rb', line 21

def self.enabled_in?(env)
  enabled_environments.include?(env)
end

Instance Method Details

#lvh_me_domainObject



25
26
27
# File 'lib/rails/local_subdomain.rb', line 25

def lvh_me_domain
  'lvh.me'
end

#lvh_me_pathObject



29
30
31
# File 'lib/rails/local_subdomain.rb', line 29

def lvh_me_path
  request.env['ORIGINAL_FULLPATH']
end

#lvh_me_portObject



33
34
35
# File 'lib/rails/local_subdomain.rb', line 33

def lvh_me_port
  request.env['SERVER_PORT']
end

#lvh_me_protocolObject



37
38
39
# File 'lib/rails/local_subdomain.rb', line 37

def lvh_me_protocol
  request.env['rack.url_scheme']
end

#lvh_me_urlObject



41
42
43
44
# File 'lib/rails/local_subdomain.rb', line 41

def lvh_me_url
  "#{lvh_me_protocol}://#{lvh_me_domain}"\
  "#{lvh_me_port == '80' ? '' : ':' + lvh_me_port}#{lvh_me_path}"
end

#redirect_to_lvh_meObject



46
47
48
49
50
51
# File 'lib/rails/local_subdomain.rb', line 46

def redirect_to_lvh_me
  return unless LocalSubdomain.enabled_in?(Rails.env)
  return if served_by_lvh_me?

  redirect_to(lvh_me_url)
end

#served_by_lvh_me?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/rails/local_subdomain.rb', line 53

def served_by_lvh_me?
  !request.env['SERVER_NAME'][/#{lvh_me_domain}$/].nil?
end