Skip to content

Commit

Permalink
merge ryland/master
Browse files Browse the repository at this point in the history
  • Loading branch information
pboling committed Aug 24, 2009
2 parents 300bd1f + 4ca3d25 commit 1b28a5c
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 11 deletions.
11 changes: 10 additions & 1 deletion lib/subdomain-fu.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ module SubdomainFu

mattr_accessor :preferred_mirror
@@preferred_mirror = nil

mattr_accessor :override_only_path
@@override_only_path = false

# Returns the TLD Size of the current environment.
def self.tld_size
Expand Down Expand Up @@ -48,6 +51,12 @@ def self.subdomain_from(host)
sub.blank? ? nil : sub
end

# Gets only non-mirror subdomains from the host based on the TLD size
def self.non_mirror_subdomain_from(host)
sub = subdomain_from(host)
has_subdomain?(sub) ? sub : nil
end

def self.host_without_subdomain(host)
parts = host.split('.')
parts[-(SubdomainFu.tld_size+1)..-1].join(".")
Expand Down Expand Up @@ -107,4 +116,4 @@ def current_subdomain
SubdomainFu.current_subdomain(request)
end
end
end
end
4 changes: 2 additions & 2 deletions lib/subdomain_fu/routing_extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def self.included(base)

def extract_request_environment_with_subdomain(request)
env = extract_request_environment_without_subdomain(request)
env.merge(:host => request.host, :domain => request.domain, :subdomain => SubdomainFu.subdomain_from(request.host))
env.merge(:host => request.host, :domain => request.domain, :subdomain => SubdomainFu.non_mirror_subdomain_from(request.host))
end
end
end
Expand All @@ -37,4 +37,4 @@ def extract_request_environment_with_subdomain(request)
# http://www.portallabs.com/blog/2008/12/02/fixing-subdomain_fu-with-named-routes-rails-22/
if Rails::VERSION::MAJOR >= 2 and Rails::VERSION::MINOR >= 2
ActionController::UrlRewriter::RESERVED_OPTIONS << :subdomain
end
end
10 changes: 5 additions & 5 deletions lib/subdomain_fu/url_rewriter.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module ActionController
module UrlWriter
def url_for_with_subdomains(options)
if SubdomainFu.needs_rewrite?(options[:subdomain], options[:host] || default_url_options[:host]) || options[:only_path] == false
options[:only_path] = false
if SubdomainFu.needs_rewrite?(options[:subdomain], options[:host] || default_url_options[:host])
options[:only_path] = false if SubdomainFu.override_only_path
options[:host] = SubdomainFu.rewrite_host_for_subdomains(options.delete(:subdomain), options[:host] || default_url_options[:host])
else
options.delete(:subdomain)
Expand All @@ -16,8 +16,8 @@ class UrlRewriter #:nodoc:
private

def rewrite_url_with_subdomains(options)
if SubdomainFu.needs_rewrite?(options[:subdomain], options[:host] || @request.host_with_port) || options[:only_path] == false
options[:only_path] = false
if SubdomainFu.needs_rewrite?(options[:subdomain], (options[:host] || @request.host_with_port))
options[:only_path] = false if SubdomainFu.override_only_path
options[:host] = SubdomainFu.rewrite_host_for_subdomains(options.delete(:subdomain), options[:host] || @request.host_with_port)
else
options.delete(:subdomain)
Expand All @@ -44,4 +44,4 @@ def guard_condition_with_subdomains
end
end
end
end
end
11 changes: 8 additions & 3 deletions spec/url_rewriter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,13 @@
it "should not change a mirrored subdomain" do
url_for(:controller => "something", :action => "other", :subdomain => false, :host => "www.testapp.com").should == "http://www.testapp.com/something/other"
end

it "should should force the full url, even with :only_path" do

it "should should not force the full url with :only_path if override_only_path is false (default)" do
url_for(:controller => "something", :action => "other", :subdomain => "awesome", :only_path => true).should == "/something/other"
end

it "should should force the full url, even with :only_path if override_only_path is true" do
SubdomainFu.override_only_path = true
url_for(:controller => "something", :action => "other", :subdomain => "awesome", :only_path => true).should == "http://awesome.testapp.com/something/other"
end
end
Expand Down Expand Up @@ -138,4 +143,4 @@
SubdomainFu.tld_size = 0
default_url_options[:host] = "localhost"
end
end
end

0 comments on commit 1b28a5c

Please sign in to comment.