Module: Subdomainitis::RouteSetMethods

Defined in:
lib/subdomainitis.rb

Instance Method Summary collapse

Instance Method Details

#add_maindomain_route(name) ⇒ Object



171
172
173
# File 'lib/subdomainitis.rb', line 171

def add_maindomain_route(name)
  maindomain_routes[name] = true
end

#add_subdomain_route(name, subdomain_key) ⇒ Object



167
168
169
# File 'lib/subdomainitis.rb', line 167

def add_subdomain_route(name, subdomain_key)
  subdomain_routes[name] = subdomain_key
end

#clear!Object



123
124
125
126
# File 'lib/subdomainitis.rb', line 123

def clear!
  subdomainitis_defaults!
  super
end

#host_name(subdomain_parameter, host) ⇒ Object

Raises:



175
176
177
178
179
180
181
182
183
184
185
# File 'lib/subdomainitis.rb', line 175

def host_name(subdomain_parameter, host)
  raise HostRequired.new unless host

  subdomain_parameter = if subdomain_parameter.respond_to?(:to_param)
    subdomain_parameter.to_param
  else
    subdomain_parameter
  end

  ([subdomain_parameter] + host.split(".")[subdomain_index..-1]).join(".")
end

#main_domain_host(host) ⇒ Object



187
188
189
# File 'lib/subdomainitis.rb', line 187

def main_domain_host(host)
  host.split(".")[subdomain_index..-1].join(".")
end

#maindomain_url_for(args) ⇒ Object

Raises:



157
158
159
160
161
162
163
164
165
# File 'lib/subdomainitis.rb', line 157

def maindomain_url_for(args)
  raise HostRequired.new if args[:only_path]

  url_for_without_subdomains(if use_fake_subdomains
    args
  else
    args.merge :host => main_domain_host(args[:host])
  end)
end

#subdomain_indexObject



191
192
193
# File 'lib/subdomainitis.rb', line 191

def subdomain_index
  -1 - tld_length
end

#subdomain_url_for(subdomain_key, args) ⇒ Object

Raises:



145
146
147
148
149
150
151
152
153
154
155
# File 'lib/subdomainitis.rb', line 145

def subdomain_url_for(subdomain_key, args)
  raise HostRequired.new if args[:only_path]
  subdomain_parameter = args.delete(subdomain_key)
  raise HostRequired.new unless subdomain_parameter

  url_for_without_subdomains(if use_fake_subdomains
    args.merge SUBDOMAIN_KEY => subdomain_parameter
  else
    args.merge :host => host_name(subdomain_parameter, args[:host])
  end)
end

#subdomainitis_defaults!Object



128
129
130
131
132
# File 'lib/subdomainitis.rb', line 128

def subdomainitis_defaults!
  self.tld_length = DEFAULT_TLD_LENGTH
  self.subdomain_routes = {}
  self.maindomain_routes = {}
end

#url_for_with_subdomains(args) ⇒ Object



134
135
136
137
138
139
140
141
142
143
# File 'lib/subdomainitis.rb', line 134

def url_for_with_subdomains(args)
  route_name = args[:use_route]
  if subdomain_key = subdomain_routes[route_name]
    subdomain_url_for(subdomain_key, args.dup)
  elsif maindomain_routes[route_name]
    maindomain_url_for(args.dup)
  else
    url_for_without_subdomains args
  end
end