Skip to content

Instantly share code, notes, and snippets.

@daniel-nelson
Created September 16, 2011 17:49
Show Gist options
  • Save daniel-nelson/1222686 to your computer and use it in GitHub Desktop.
Save daniel-nelson/1222686 to your computer and use it in GitHub Desktop.
module UrlHelper
def with_subdomain(subdomain)
subdomain = (subdomain || "")
subdomain += "." unless subdomain.empty?
[subdomain, request.domain, request.port_string].join
end
def url_for(options = nil)
if options.kind_of?(Hash) && options.has_key?(:subdomain)
options[:host] = with_subdomain(options.delete(:subdomain))
end
if request.subdomain != 'admin'
language_specific_path(super, I18n.locale)
else
super
end
end
def language_specific_path(path, language_code = I18n.locale)
prefix = path.slice!(/^https?:\/\/[^\/]*/) if path =~ /^https?:\/\//
prefix ||= ''
path.slice!(/^\/(en|es)/)
prefix + if language_code == :en
path.empty? ? '/' : path
else
"/#{language_code}#{path}"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment