module SubdomainRouter::Controller

Controller mixin that adds subdomain management features.

Public Instance Methods

url_for(options={}) click to toggle source

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.

@param [Hash] options Options for the URL. @option options [String, nil, false] :subdomain The subdomain to route to.

If `false`, uses the default subdomain (e.g., "www"). If `nil`, uses the
current subdomain.

@return [String] The generated URL. @raise [ArgumentError] If the `:subdomain` option is invalid.

Calls superclass method
# 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