Module: SubdomainRoutes::RewriteSubdomainOptions

Includes:
SplitHost
Included in:
RoutingAssertions, UrlRewriter, UrlWriter
Defined in:
lib/subdomain_routes/url_writer.rb

Instance Method Summary collapse

Instance Method Details

#rewrite_subdomain_options(options, host, port = nil, protocol = nil) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/subdomain_routes/url_writer.rb', line 12

def rewrite_subdomain_options(options, host, port = nil, protocol = nil)
  if subdomains = options[:subdomains]
    old_subdomain, domain = split_host(host)
    options_subdomain = options.has_key?(:subdomain) ? options[:subdomain].to_param.to_s.downcase : nil
    new_subdomain = options_subdomain || old_subdomain
    begin
      case subdomains
      when Array
        unless subdomains.include? new_subdomain
          if subdomains.size > 1 || options_subdomain
            raise ActionController::RoutingError, "expected subdomain in #{subdomains.inspect}, instead got subdomain #{new_subdomain.inspect}"
          else
            new_subdomain = subdomains.first
          end
        end
      when Symbol
        new_subdomain = options[subdomains].to_param.to_s.downcase
        unless new_subdomain.blank? || SubdomainRoutes.valid_subdomain?(new_subdomain)
          raise ActionController::RoutingError, "subdomain #{new_subdomain.inspect} is invalid"
        end
      end            
    rescue ActionController::RoutingError => e
      raise ActionController::RoutingError, "Route for #{options.inspect} failed to generate (#{e.message})"
    end
    unless new_subdomain == old_subdomain
      options[:only_path] = false
      options[:host] = [ new_subdomain, domain ].reject(&:blank?).join('.')
      options[:port] ||= port if port
      options[:protocol] ||= protocol if protocol
    end
    options.delete(:subdomain)
  end
end

#subdomain_procsObject



8
9
10
# File 'lib/subdomain_routes/url_writer.rb', line 8

def subdomain_procs
  ActionController::Routing::Routes.subdomain_procs
end