Module: SubdomainRouter::Controller

Extended by:
ActiveSupport::Concern
Defined in:
lib/subdomain_router.rb

Overview

Controller mixin that adds subdomain management features.

Instance Method Summary collapse

Instance Method Details

#url_for(options = {}) ⇒ String

Adds to the ‘url_for` method the ability to route to different subdomains. Thus, all URL generation (including smart route methods) gains the `:subdomain` options.

For more information, see the Rails documentation.

Parameters:

  • options (Hash) (defaults to: {})

    Options for the URL.

Options Hash (options):

  • :subdomain (String, nil, false)

    The subdomain to route to. If ‘false`, uses the default subdomain (e.g., “www”). If `nil`, uses the current subdomain.

Returns:

  • (String)

    The generated URL.

Raises:

  • (ArgumentError)

    If the ‘:subdomain` option is invalid.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/subdomain_router.rb', line 27

def url_for(options={})
  return super unless options.is_a?(Hash)

  case options[:subdomain]
    when nil
      options.delete :subdomain
      super options
    when false, String
      subdomain = options.delete(:subdomain) || Config.default_subdomain
      host = options[:host] || (respond_to?(:request) && request.host) || Config.domain
      host_parts = host.split('.').last(Config.tld_components + 1)
      host_parts.unshift subdomain
      host_parts.delete_if &:blank?
      super options.merge(host: host_parts.join('.'))
    else
      raise ArgumentError, ":subdomain must be nil, false, or a string"
  end
end