Module: Amalgam::UrlHelper

Defined in:
app/helpers/amalgam/url_helper.rb

Instance Method Summary collapse

Instance Method Details

#locale_url(locale) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/helpers/amalgam/url_helper.rb', line 3

def locale_url(locale)
  available_locales = ::I18n::available_locales.collect{|m| m.to_s.downcase}

  path_list = request.fullpath.split('/')
  if path_list.present?
    if available_locales.include?(path_list[1].downcase)
      path_list[1] = locale
      return "#{request.protocol}#{request.host_with_port}#{path_list.join('/')}"
    end
  else
    return "#{request.protocol}#{request.host_with_port}/#{locale}"
  end

  if locale_param = request.fullpath.match(/locale=[\w|-]*/)
    url = "#{request.protocol}#{request.host_with_port}#{request.fullpath}"
    url[locale_param[0]] = "locale=#{locale}"
    return url
  end

  domain_list = request.host_with_port.split('.')
  if available_locales.include?(domain_list.first.downcase)
    domain_list[0] = locale
    return "#{request.protocol}#{domain_list.join('.')}#{request.fullpath}"
  end

  if request.fullpath.include?('?')
    return "#{request.protocol}#{request.host_with_port}#{request.fullpath}&locale=#{locale}"
  else
    return "#{request.protocol}#{request.host_with_port}#{request.fullpath}?locale=#{locale}"
  end
end

#url_for(options = nil) ⇒ Object



41
42
43
44
45
46
# File 'app/helpers/amalgam/url_helper.rb', line 41

def url_for(options = nil)
  if options.kind_of?(Hash) && options.has_key?(:subdomain)
    options[:host] = with_subdomain(options.delete(:subdomain))
  end
  super
end

#with_subdomain(subdomain) ⇒ Object



35
36
37
38
39
# File 'app/helpers/amalgam/url_helper.rb', line 35

def with_subdomain(subdomain)
  subdomain = (subdomain || "")
  subdomain += "." unless subdomain.empty?
  [subdomain, request.domain, request.port_string].join
end