Skip to content

Commit

Permalink
added some more subdomain cases; support for default prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
peppyheppy committed Aug 2, 2011
1 parent 04486b3 commit a073a8b
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
localized (0.0.7)
localized (0.0.8)
bitfields (>= 0.4.0)
rails (>= 3.0)

Expand Down
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -13,7 +13,7 @@ subdomain. It also contains a way for overriding the "site" in url helpers.
127.0.0.1 www.it.mysite.localhost
127.0.0.1 www.ca.mysite.localhost

3. Create your "change locale widget" using simple url helpers:
3. Create your "change locale widget" using simple url helpers while assuming that the default site is 'us'.

root_url(:site => :us) # => http://www.mysite.com/
root_url(:site => :it) # => http://www.it.mysite.com/
Expand Down
6 changes: 5 additions & 1 deletion lib/localized/config.rb
Expand Up @@ -12,7 +12,11 @@ def self.configuration
end

def self.default_host_prefix
@default_host_prefix ||= self.configuration[:default_host_prefix].to_s
@default_host_prefix ||= begin
prefix = self.configuration[:default_host_prefix]
# support nil or empty prefixes
prefix.blank? ? nil : prefix.to_s
end
end

def self.site_to_locale_map
Expand Down
12 changes: 10 additions & 2 deletions lib/localized/helper.rb
Expand Up @@ -45,7 +45,15 @@ def with_locale(site = nil)
caller(5)
}

subdomain = (Localized::Config.default_site != site ? "#{site}." : nil)
[Localized::Config.default_host_prefix, subdomain, request.domain, request.port_string].join
# get the site/subdomain
subdomain = (Localized::Config.default_site != site ? "#{site}" : nil)

# preserve all of other subdomains
subdomains = request.subdomains
subdomains.delete(Localized::Config.default_host_prefix)
if Localized::Config.supported_sites.include?(subdomains.first.to_s.to_sym)
subdomains.delete(subdomains.first)
end
"#{[Localized::Config.default_host_prefix, subdomain, *subdomains, request.domain].compact.join('.')}#{request.port_string}"
end
end
2 changes: 1 addition & 1 deletion spec/config/localized.yml
@@ -1,4 +1,4 @@
default_host_prefix: 'www.'
default_host_prefix: 'www'
default_site: 'us'
site_locale_map:
au: 'en-AU'
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/localized/config_spec.rb
Expand Up @@ -30,7 +30,7 @@

it "should have default host prefix" do
Localized::Config.should respond_to :default_host_prefix
Localized::Config.default_host_prefix.should == "www."
Localized::Config.default_host_prefix.should == "www"
end

it "should have supported sites" do
Expand Down
20 changes: 20 additions & 0 deletions spec/lib/localized/helper_spec.rb
Expand Up @@ -35,6 +35,26 @@
I18n.locale = original_locale
end

describe "with existing subdomain" do
it "should add new subdomain to existing subdomains" do
@request.host = "www.admin.mysite.test"
root_url(:site => 'it').should == 'http://www.it.admin.mysite.test/'
end

it "should preserve existing subdomains" do
@request.host = "www.it.admin.mysite.test"
root_url(:site => 'us').should == 'http://www.admin.mysite.test/'
end
end
it "should support ports" do
@request.host = "www.it.admin.mysite.test:3009"
root_url(:site => 'us').should == 'http://www.admin.mysite.test:3009/'
end
it "should support a nil or empty prefix" do
Localized::Config.stub!(:default_host_prefix).and_return(nil)
@request.host = "it.admin.mysite.test"
root_url(:site => 'us').should == 'http://admin.mysite.test/'
end
end

describe "when host is an ip address" do
Expand Down

0 comments on commit a073a8b

Please sign in to comment.