Skip to content

Commit

Permalink
fixed tests, added is_mirror?, improved readme, bumped version to 0.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
pboling committed Aug 27, 2009
1 parent d738057 commit 2e23072
Show file tree
Hide file tree
Showing 12 changed files with 146 additions and 80 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
== 2009-08-26

* Fixed needs_rewrite? method
* Added is_mirror? method
* Added tests, and fixed failing tests

== 2009-08-24

* Merged forks that added lots of features, tests, and fixes
Expand Down
3 changes: 2 additions & 1 deletion README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ you can set the preferred mirror like so:

SubdomainFu.preferred_mirror = "www"

Now when you create a link to a false subdomain
Now when you create a link with subdomain => false in the options the subdomain
will default to the preferred mirror.

== Known Issues / Future Work

Expand Down
4 changes: 2 additions & 2 deletions VERSION.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
---
:major: 0
:minor: 2
:patch: 1
:minor: 3
:patch: 0
2 changes: 1 addition & 1 deletion install.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# I use this to make life easier when installing and testing from source:
rm -rf subdomain-fu-*.gem && gem build subdomain-fu.gemspec && sudo gem uninstall subdomain-fu && sudo gem install subdomain-fu-0.2.1.gem --no-ri --no-rdoc
rm -rf subdomain-fu-*.gem && gem build subdomain-fu.gemspec && sudo gem uninstall subdomain-fu && sudo gem install subdomain-fu-0.3.0.gem --no-ri --no-rdoc
69 changes: 50 additions & 19 deletions lib/subdomain-fu.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,58 +10,62 @@ module SubdomainFu
DEFAULT_TLD_SIZES = {:development => 0, :test => 0, :production => 1}
mattr_accessor :tld_sizes
@@tld_sizes = DEFAULT_TLD_SIZES.dup

# Subdomains that are equivalent to going to the website with no subdomain at all.
# Defaults to "www" as the only member.
DEFAULT_MIRRORS = %w(www)
mattr_accessor :mirrors
@@mirrors = DEFAULT_MIRRORS.dup

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
tld_sizes[RAILS_ENV.to_sym]
end

# Sets the TLD Size of the current environment
def self.tld_size=(value)
tld_sizes[RAILS_ENV.to_sym] = value
end
# Is the current subdomain either nil or a mirror?

# Is the current subdomain either nil or not a mirror?
def self.has_subdomain?(subdomain)
subdomain != false && !subdomain.blank? && !SubdomainFu.mirrors.include?(subdomain)
end

def self.is_mirror?(subdomain)
subdomain != false && !subdomain.blank? && SubdomainFu.mirrors.include?(subdomain)
end

# Is the subdomain a preferred mirror
def self.preferred_mirror?(subdomain)
subdomain == SubdomainFu.preferred_mirror || SubdomainFu.preferred_mirror.nil?
end

# Gets the subdomain from the host based on the TLD size
def self.subdomain_from(host)
return nil if host.nil? || /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.match(host)
parts = host.split('.')
sub = parts[0..-(SubdomainFu.tld_size+2)].join(".")
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
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(".")
end

# Rewrites the subdomain of the host unless they are equivalent (i.e. mirrors of each other)
def self.rewrite_host_for_subdomains(subdomain, host)
if needs_rewrite?(subdomain, host)
Expand All @@ -75,28 +79,55 @@ def self.rewrite_host_for_subdomains(subdomain, host)
end
end
end

# Changes the subdomain of the host to whatever is passed in.
def self.change_subdomain_of_host(subdomain, host)
host = SubdomainFu.host_without_subdomain(host)
host = "#{subdomain}.#{host}" if subdomain
host
end

# Is this subdomain equivalent to the subdomain found in this host string?
def self.same_subdomain?(subdomain, host)
subdomain = nil unless subdomain
(subdomain == subdomain_from(host)) ||
(!has_subdomain?(subdomain) && !has_subdomain?(subdomain_from(host)))
end


def self.override_only_path?
self.override_only_path
end

def self.needs_rewrite?(subdomain, host)
return false if subdomain.nil?
case subdomain
when nil
#rewrite when there is a preferred mirror set and there is no subdomain on the host
return true if self.preferred_mirror && subdomain_from(host).nil?
return false
when false
h = subdomain_from(host)
#if the host has a subdomain
if !h.nil?
#rewrite when there is a subdomain in the host, and it is not a preferred mirror
return true if !preferred_mirror?(h)
#rewrite when there is a preferred mirror set and the subdomain of the host is not a mirror
return true if self.preferred_mirror && !is_mirror?(h)
#no rewrite if host already has mirror subdomain
#it { SubdomainFu.needs_rewrite?(false,"www.localhost").should be_false }
return false if is_mirror?(h)
end
return self.crazy_rewrite_rule(subdomain, host)
else
return self.crazy_rewrite_rule(subdomain, host)
end
end

(!has_subdomain?(subdomain) && preferred_mirror?(subdomain) && !preferred_mirror?(subdomain_from(host))) ||
#This is a black box of crazy! So I split some of the simpler logic out into the case statement above to make my brain happy!
def self.crazy_rewrite_rule(subdomain, host)
(!has_subdomain?(subdomain) && preferred_mirror?(subdomain) && !preferred_mirror?(subdomain_from(host))) ||
!same_subdomain?(subdomain, host)
end

def self.current_subdomain(request)
subdomain = request.subdomains(SubdomainFu.tld_size).join(".")
if has_subdomain?(subdomain)
Expand All @@ -105,12 +136,12 @@ def self.current_subdomain(request)
nil
end
end

module Controller
def self.included(controller)
controller.helper_method(:current_subdomain)
end

protected
def current_subdomain
SubdomainFu.current_subdomain(request)
Expand Down
6 changes: 3 additions & 3 deletions lib/subdomain_fu/routing_extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@ def recognition_conditions_with_subdomain
result
end
end

module RouteSetExtensions
def self.included(base)
base.alias_method_chain :extract_request_environment, :subdomain
base.alias_method_chain :extract_request_environment, :subdomain
end

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))
end
end

module MapperExtensions
def quick_map(has_unless, *args, &block)
options = args.find{|a| a.is_a?(Hash)}
Expand Down
15 changes: 8 additions & 7 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 if SubdomainFu.override_only_path
if SubdomainFu.needs_rewrite?(options[:subdomain], options[:host] || default_url_options[:host]) || options[:only_path] == false
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 @@ -11,22 +11,23 @@ def url_for_with_subdomains(options)
end
alias_method_chain :url_for, :subdomains
end

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 if SubdomainFu.override_only_path
if SubdomainFu.needs_rewrite?(options[:subdomain], (options[:host] || @request.host_with_port)) || options[:only_path] == false
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)
puts "options[:host]: #{options[:host].inspect}"
else
options.delete(:subdomain)
end
rewrite_url_without_subdomains(options)
end
alias_method_chain :rewrite_url, :subdomains
end

if Rails::VERSION::MAJOR >= 2 and Rails::VERSION::MINOR <= 1
# hack for http://www.portallabs.com/blog/2008/10/22/fixing-subdomain_fu-with-named-routes/
module Routing
Expand Down
2 changes: 0 additions & 2 deletions rails/init.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
require 'action_controller'

# Add this path to ruby load path
$:.unshift "#{File.dirname(__FILE__)}/../lib"

Expand Down
12 changes: 6 additions & 6 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,31 @@
map.needs_subdomain '/needs_subdomain', :controller => "fu", :action => "awesome"
map.no_subdomain '/no_subdomain', :controller => "fu", :action => "lame"
map.needs_awesome '/needs_awesome', :controller => "fu", :action => "lame"

map.resources :foos do |fu|
fu.resources :bars
end

map.connect '/', :controller => "site", :action => "home", :conditions => {:subdomain => false}
map.connect '/', :controller => "app", :action => "home", :conditions => {:subdomain => true}
map.connect '/', :controller => "mobile", :action => "home", :conditions => {:subdomain => "m"}

map.connect '/subdomain_here', :controller => "app", :action => "success", :conditions => {:subdomain => true}
map.connect '/no_subdomain_here', :controller => "site", :action => "success", :conditions => {:subdomain => false}
map.connect '/m_subdomain_here', :controller => "mobile", :action => "success", :conditions => {:subdomain => "m"}
map.connect '/numbers_only_here', :controller => "numbers", :action => "success", :conditions => {:subdomain => /[0-9]+/}

map.connect '/:controller/:action/:id'
end

class Paramed
def initialize(param)
@param = param
end

def to_param
@param || "param"
end
end

include ActionController::UrlWriter
include ActionController::UrlWriter
Loading

0 comments on commit 2e23072

Please sign in to comment.