Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

merge asok/master, updated specs

  • Loading branch information...
commit a0ed77629a1a4b364656c508bff1de9444e8b86e 2 parents dd06bce + ce1ae45
@pboling pboling authored
View
3  .gitignore
@@ -1,7 +1,10 @@
.DS_Store
*.log
+**.swp
.svn
/pkg
.idea
.idea/*
*.gem
+/coverage
+/coverage.data
View
1  .rspec
@@ -0,0 +1 @@
+--colour
View
6 README.rdoc
@@ -1,3 +1,7 @@
+= Changes in this fork
+
+[current_subdomain] will return all subdomains except for mirrors. For the URL http://www.stuff.example.com it will return "stuff" if "www" is configured as a mirror
+
= SubdomainFu
SubdomainFu provides a modern implementation of subdomain handling in Rails.
@@ -13,7 +17,7 @@ you are looking to use it on Rails 2.x, please install version v0.5.x instead.
SubdomainFu is available both as a traditional plugin and a GemPlugin. To
install it as a traditional plugin:
- script/plugin install git://github.com/intridea/subdomain-fu.git
+ script/plugin install git://github.com/ramontayag/subdomain-fu.git
To use it as a gem, add it to your Gemfile. Until Rails 3 is officially released,
the Rails 3 version of the plugin is only available as a prerelease gem:
View
61 Rakefile
@@ -1,14 +1,4 @@
require 'rake'
-# require 'spec/rake/spectask'
-#
-# desc 'Default: run specs.'
-# task :default => :spec
-#
-# desc 'Run the specs'
-# Spec::Rake::SpecTask.new(:spec) do |t|
-# t.spec_opts = ['--colour --format progress --loadby mtime --reverse']
-# t.spec_files = FileList['spec/**/*_spec.rb']
-# end
begin
require 'jeweler'
@@ -23,34 +13,33 @@ begin
gemspec.authors = ["Michael Bleigh"]
end
Jeweler::GemcutterTasks.new
+ FileList['tasks/**/*.rake'].each { |task| import task }
rescue LoadError
puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
end
+begin
+ require 'rspec/core/rake_task'
+
+ task :cleanup_rcov_files do
+ rm_rf 'coverage.data'
+ end
+
+ desc "Run all specs."
+ RSpec::Core::RakeTask.new(:spec) do |t|
+ t.rspec_opts = %w[--color]
+ end
-# These are new tasks
-# begin
-# require 'rake/contrib/sshpublisher'
-# namespace :rubyforge do
-#
-# desc "Release gem and RDoc documentation to RubyForge"
-# task :release => ["rubyforge:release:gem", "rubyforge:release:docs"]
-#
-# namespace :release do
-# desc "Publish RDoc to RubyForge."
-# task :docs => [:rdoc] do
-# config = YAML.load(
-# File.read(File.expand_path('~/.rubyforge/user-config.yml'))
-# )
-#
-# host = "#{config['username']}@rubyforge.org"
-# remote_dir = "/var/www/gforge-projects/the-perfect-gem/"
-# local_dir = 'rdoc'
-#
-# Rake::SshDirPublisher.new(host, remote_dir, local_dir).upload
-# end
-# end
-# end
-# rescue LoadError
-# puts "Rake SshDirPublisher is unavailable or your rubyforge environment is not configured."
-# end
+ namespace :spec do
+ desc "Run all specs using rcov."
+ RSpec::Core::RakeTask.new(:rcov => :cleanup_rcov_files) do |t|
+ t.rcov = true
+ t.rcov_opts = '-Ilib:spec --exclude "gems/.*,features"'
+ t.rcov_opts << %[--text-report --sort coverage --html --aggregate coverage.data]
+ end
+ end
+
+ task :default => :spec
+rescue LoadError
+ puts "RSpec-2 not available."
+end
View
2  VERSION.yml
@@ -1,5 +1,5 @@
---
:major: 1
-:build: beta2
+:build: beta3
:minor: 0
:patch: 0
View
3  init.rb
@@ -0,0 +1,3 @@
+require 'subdomain_fu/url_rewriter'
+require 'subdomain_fu/subdomain_fu'
+require 'subdomain_fu/plugin'
View
390 lib/subdomain-fu.rb
@@ -1,195 +1,197 @@
require 'subdomain_fu/url_rewriter'
-
-module SubdomainFu
- class << self
- attr_accessor :config
-
- def config
- self.config = Configuration.new unless @config
- @config
- end
- end
-
- # The configurable options of Subdomain Fu. Use like so:
- #
- # SubdomainFu.configure do |config|
- # config.tld_size = 2
- # config.preferred_mirror = 'www'
- # end
- #
- # Available configurations are:
- #
- # <tt>tld_size</tt>: :: The size of the top-level domain. For example, 'localhost' is 0, 'example.com' is 1, and 'example.co.uk' is 2.
- # <tt>mirrors</tt>: :: An array of subdomains that should be equivalent to no subdomain. Defaults to <tt>['www']</tt>.
- # <tt>preferred_mirror</tt>: The preferred mirror subdomain to which to rewrite URLs. No subdomain is used by default.
- # <tt>override_only_path</tt>: :: If <tt>true</tt>, changing the subdomain will emit a full URL in url_for options, even if it wouldn't have otherwise.
- def self.configure
- self.config ||= Configuration.new
- yield(self.config)
- end
-
- class Configuration
- attr_accessor :tld_sizes, :mirrors, :preferred_mirror, :override_only_path
-
- @@defaults = {
- :tld_sizes => {:development => 1, :test => 1, :production => 1},
- :mirrors => %w(www),
- :preferred_mirror => nil,
- :override_only_path => false
- }
-
- def initialize
- @@defaults.each_pair do |k, v|
- self.send("#{k}=", v)
- end
- end
-
- def tld_size=(size)
- tld_sizes[Rails.env.to_sym] = size
- end
-
- def tld_size
- tld_sizes[Rails.env.to_sym]
- end
- end
-
- def self.has_domain?(host)
- !host.blank? && !(/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.match(host))
- end
-
- # Is the current subdomain either nil or not a mirror?
- def self.has_subdomain?(subdomain)
- subdomain != false && !subdomain.blank? && !SubdomainFu.config.mirrors.include?(subdomain)
- end
-
- def self.is_mirror?(subdomain)
- subdomain != false && !subdomain.blank? && SubdomainFu.config.mirrors.include?(subdomain)
- end
-
- # Is the subdomain a preferred mirror
- def self.preferred_mirror?(subdomain)
- subdomain == SubdomainFu.config.preferred_mirror || SubdomainFu.config.preferred_mirror.nil?
- end
-
- # Gets the subdomain from the host based on the TLD size
- def self.subdomain_from(host)
- return nil unless has_domain?(host)
- parts = host.split('.')
- sub = parts[0..-(SubdomainFu.config.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
- end
-
- def self.host_without_subdomain(host)
- parts = host.split('.')
- parts[-(SubdomainFu.config.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)
- change_subdomain_of_host(subdomain || SubdomainFu.config.preferred_mirror, host)
- else
- if has_subdomain?(subdomain) || preferred_mirror?(subdomain_from(host)) ||
- (subdomain.nil? && has_subdomain?(subdomain_from(host)))
- host
- else
- change_subdomain_of_host(SubdomainFu.config.preferred_mirror, 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
-
- # Is this subdomain host equivalent to the host without subdomain in this host string?
- def self.same_host?(subdomain_host, host)
- SubdomainFu.host_without_subdomain(host) == subdomain_host
- end
-
- def self.override_only_path?
- config.override_only_path
- end
-
- def self.needs_rewrite?(subdomain, host)
- case subdomain
- when nil
- #rewrite when there is a preferred mirror set and there is no subdomain on the host
- return true if config.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 config.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
-
- #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
-
- #returns nil or the subdomain(s)
- def self.current_subdomain(request)
- subdomain = request.subdomains(SubdomainFu.config.tld_size).join(".")
- if has_subdomain?(subdomain)
- subdomain
- else
- nil
- end
- end
-
- #returns nil or the domain or ip
- #Enables subdomain-fu to more completely replace DHH's account_location plugin
- def self.current_domain(request)
- return request.domain unless has_domain?(request.domain)
- domain = ""
- domain << request.subdomains[1..-1].join(".") + "." if request.subdomains.length > 1
- domain << request.domain + request.port_string
- end
-
- module Controller
- def self.included(controller)
- controller.helper_method(:current_subdomain)
- controller.helper_method(:current_domain)
- end
-
- protected
- def current_subdomain
- SubdomainFu.current_subdomain(request)
- end
- def current_domain
- SubdomainFu.current_domain(request)
- end
- end
-end
-
-require 'subdomain_fu/rails'
+require 'subdomain_fu/subdomain_fu'
+require 'subdomain_fu/engine'
+#
+#module SubdomainFu
+# class << self
+# attr_accessor :config
+#
+# def config
+# self.config = Configuration.new unless @config
+# @config
+# end
+# end
+#
+# # The configurable options of Subdomain Fu. Use like so:
+# #
+# # SubdomainFu.configure do |config|
+# # config.tld_size = 2
+# # config.preferred_mirror = 'www'
+# # end
+# #
+# # Available configurations are:
+# #
+# # <tt>tld_size</tt>: :: The size of the top-level domain. For example, 'localhost' is 0, 'example.com' is 1, and 'example.co.uk' is 2.
+# # <tt>mirrors</tt>: :: An array of subdomains that should be equivalent to no subdomain. Defaults to <tt>['www']</tt>.
+# # <tt>preferred_mirror</tt>: The preferred mirror subdomain to which to rewrite URLs. No subdomain is used by default.
+# # <tt>override_only_path</tt>: :: If <tt>true</tt>, changing the subdomain will emit a full URL in url_for options, even if it wouldn't have otherwise.
+# def self.configure
+# self.config ||= Configuration.new
+# yield(self.config)
+# end
+#
+# class Configuration
+# attr_accessor :tld_sizes, :mirrors, :preferred_mirror, :override_only_path
+#
+# @@defaults = {
+# :tld_sizes => {:development => 1, :test => 1, :production => 1},
+# :mirrors => %w(www),
+# :preferred_mirror => nil,
+# :override_only_path => false
+# }
+#
+# def initialize
+# @@defaults.each_pair do |k, v|
+# self.send("#{k}=", v)
+# end
+# end
+#
+# def tld_size=(size)
+# tld_sizes[Rails.env.to_sym] = size
+# end
+#
+# def tld_size
+# tld_sizes[Rails.env.to_sym]
+# end
+# end
+#
+# def self.has_domain?(host)
+# !host.blank? && !(/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.match(host))
+# end
+#
+# # Is the current subdomain either nil or not a mirror?
+# def self.has_subdomain?(subdomain)
+# subdomain != false && !subdomain.blank? && !SubdomainFu.config.mirrors.include?(subdomain)
+# end
+#
+# def self.is_mirror?(subdomain)
+# subdomain != false && !subdomain.blank? && SubdomainFu.config.mirrors.include?(subdomain)
+# end
+#
+# # Is the subdomain a preferred mirror
+# def self.preferred_mirror?(subdomain)
+# subdomain == SubdomainFu.config.preferred_mirror || SubdomainFu.config.preferred_mirror.nil?
+# end
+#
+# # Gets the subdomain from the host based on the TLD size
+# def self.subdomain_from(host)
+# return nil unless has_domain?(host)
+# parts = host.split('.')
+# sub = parts[0..-(SubdomainFu.config.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
+# end
+#
+# def self.host_without_subdomain(host)
+# parts = host.split('.')
+# parts[-(SubdomainFu.config.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)
+# change_subdomain_of_host(subdomain || SubdomainFu.config.preferred_mirror, host)
+# else
+# if has_subdomain?(subdomain) || preferred_mirror?(subdomain_from(host)) ||
+# (subdomain.nil? && has_subdomain?(subdomain_from(host)))
+# host
+# else
+# change_subdomain_of_host(SubdomainFu.config.preferred_mirror, 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
+#
+# # Is the host without subdomain equivalent to the subdomain_host in this subdomain_host string?
+# def self.same_host?(subdomain_host, host)
+# SubdomainFu.host_without_subdomain(host) == subdomain_host
+# end
+#
+# def self.override_only_path?
+# config.override_only_path
+# end
+#
+# def self.needs_rewrite?(subdomain, host)
+# case subdomain
+# when nil
+# #rewrite when there is a preferred mirror set and there is no subdomain on the host
+# return true if config.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 config.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
+#
+# #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
+#
+# #returns nil or the subdomain(s)
+# def self.current_subdomain(request)
+# subdomain = request.subdomains(SubdomainFu.config.tld_size).join(".")
+# if has_subdomain?(subdomain)
+# subdomain
+# else
+# nil
+# end
+# end
+#
+# #returns nil or the domain or ip
+# #Enables subdomain-fu to more completely replace DHH's account_location plugin
+# def self.current_domain(request)
+# return request.domain unless has_domain?(request.domain)
+# domain = ""
+# domain << request.subdomains[1..-1].join(".") + "." if request.subdomains.length > 1
+# domain << request.domain + request.port_string
+# end
+#
+# module Controller
+# def self.included(controller)
+# controller.helper_method(:current_subdomain)
+# controller.helper_method(:current_domain)
+# end
+#
+# protected
+# def current_subdomain
+# SubdomainFu.current_subdomain(request)
+# end
+# def current_domain
+# SubdomainFu.current_domain(request)
+# end
+# end
+#end
+#
+#require 'subdomain_fu/rails'
View
2  lib/subdomain_fu/rails.rb → lib/subdomain_fu/engine.rb
@@ -1,4 +1,4 @@
-module SubdomainFU
+module SubdomainFu
class Engine < ::Rails::Engine
initializer "setup for rails" do
ActionController::Base.send :include, SubdomainFu::Controller
View
1  lib/subdomain_fu/plugin.rb
@@ -0,0 +1 @@
+ActionController::Base.send :include, SubdomainFu::Controller
View
190 lib/subdomain_fu/subdomain_fu.rb
@@ -0,0 +1,190 @@
+module SubdomainFu
+ class << self
+ attr_accessor :config
+
+ def config
+ self.config = Configuration.new unless @config
+ @config
+ end
+ end
+
+ # The configurable options of Subdomain Fu. Use like so:
+ #
+ # SubdomainFu.configure do |config|
+ # config.tld_size = 2
+ # config.preferred_mirror = 'www'
+ # end
+ #
+ # Available configurations are:
+ #
+ # <tt>tld_size</tt>: :: The size of the top-level domain. For example, 'localhost' is 0, 'example.com' is 1, and 'example.co.uk' is 2.
+ # <tt>mirrors</tt>: :: An array of subdomains that should be equivalent to no subdomain. Defaults to <tt>['www']</tt>.
+ # <tt>preferred_mirror</tt>: The preferred mirror subdomain to which to rewrite URLs. No subdomain is used by default.
+ # <tt>override_only_path</tt>: :: If <tt>true</tt>, changing the subdomain will emit a full URL in url_for options, even if it wouldn't have otherwise.
+ def self.configure
+ self.config ||= Configuration.new
+ yield(self.config)
+ end
+
+ class Configuration
+ attr_accessor :tld_sizes, :mirrors, :preferred_mirror, :override_only_path
+
+ @@defaults = {
+ :tld_sizes => { :development => 0, :test => 0, :production => 1 }.freeze,
+ :mirrors => %w(www).freeze,
+ :preferred_mirror => nil,
+ :override_only_path => false
+ }.freeze
+
+ def initialize
+ @@defaults.each_pair do |k, v|
+ self.send("#{k}=", (v.dup rescue v))
+ end
+ end
+
+ def self.defaults
+ @@defaults
+ end
+
+ def tld_size=(size)
+ tld_sizes[Rails.env.to_sym] = size
+ end
+
+ def tld_size
+ tld_sizes[Rails.env.to_sym]
+ end
+ end
+
+
+ def self.has_domain?(host)
+ !host.blank? && !(/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.match(host))
+ end
+
+ # Is the current subdomain either nil or not a mirror?
+ def self.has_subdomain?(subdomain)
+ subdomain != false && !subdomain.blank? && !SubdomainFu.config.mirrors.include?(subdomain)
+ end
+
+ def self.is_mirror?(subdomain)
+ subdomain != false && !subdomain.blank? && SubdomainFu.config.mirrors.include?(subdomain)
+ end
+
+ # Is the subdomain a preferred mirror
+ def self.preferred_mirror?(subdomain)
+ subdomain == SubdomainFu.config.preferred_mirror || SubdomainFu.config.preferred_mirror.nil?
+ end
+
+ # Gets the subdomain from the host based on the TLD size
+ def self.subdomain_from(host)
+ return nil unless has_domain?(host)
+ parts = host.split('.')
+ sub = parts[0..-(SubdomainFu.config.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
+ end
+
+ def self.host_without_subdomain(host)
+ parts = host.split('.')
+ Array(parts[-(config.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)
+ change_subdomain_of_host(subdomain || SubdomainFu.config.preferred_mirror, host)
+ else
+ if has_subdomain?(subdomain) || preferred_mirror?(subdomain_from(host)) ||
+ (subdomain.nil? && has_subdomain?(subdomain_from(host)))
+ host
+ else
+ change_subdomain_of_host(SubdomainFu.config.preferred_mirror, 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
+
+ # Is the host without subdomain equivalent to the subdomain_host in this subdomain_host string?
+ def self.same_host?(subdomain_host, host)
+ SubdomainFu.host_without_subdomain(host) == subdomain_host
+ end
+
+ def self.override_only_path?
+ config.override_only_path
+ end
+
+ def self.needs_rewrite?(subdomain, host)
+ return false if host.count('.') < config.tld_size
+ case subdomain
+ when nil
+ #rewrite when there is a preferred mirror set and there is no subdomain on the host
+ return config.preferred_mirror && subdomain_from(host).nil?
+ 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 config.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
+ end
+
+ (!has_subdomain?(subdomain) && preferred_mirror?(subdomain) && !preferred_mirror?(subdomain_from(host))) ||
+ !same_subdomain?(subdomain, host)
+ end
+
+ #returns nil or the subdomain(s)
+ def self.current_subdomain(request)
+ subdomain = (request.subdomains(SubdomainFu.config.tld_size) - config.mirrors).join(".")
+ if has_subdomain?(subdomain)
+ subdomain
+ else
+ nil
+ end
+ end
+
+ #returns nil or the domain or ip
+ #Enables subdomain-fu to more completely replace DHH's account_location plugin
+ def self.current_domain(request)
+ return request.domain unless has_domain?(request.domain)
+ domain = ""
+ domain << request.subdomains[1..-1].join(".") + "." if request.subdomains.length > 1
+ domain << request.domain.to_s + request.port_string
+ end
+
+ module Controller
+ def self.included(controller)
+ controller.helper_method(:current_subdomain)
+ controller.helper_method(:current_domain)
+ end
+
+ protected
+ def current_subdomain
+ SubdomainFu.current_subdomain(request)
+ end
+ def current_domain
+ SubdomainFu.current_domain(request)
+ end
+ end
+end
View
1  lib/subdomain_fu/url_rewriter.rb
@@ -7,7 +7,6 @@ def url_for_with_subdomains(options, path_segments=nil)
if SubdomainFu.needs_rewrite?(options[:subdomain], (options[:host] || (@request && @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 && @request.host_with_port))
- # puts "options[:host]: #{options[:host].inspect}"
else
options.delete(:subdomain)
end
View
7 spec/spec.opts
@@ -1,7 +0,0 @@
---colour
---format
-specdoc
---loadby
-mtime
---reverse
---backtrace
View
45 spec/spec_helper.rb
@@ -1,7 +1,10 @@
-#require 'spec'
-require 'rspec/core'
+require 'action_controller/railtie'
+require 'active_support/core_ext/hash/slice'
+require 'rspec'
require 'subdomain-fu'
+Rails.env = 'test'
+
plugin_spec_dir = File.dirname(__FILE__)
$LOAD_PATH.unshift(plugin_spec_dir)
@@ -12,28 +15,34 @@
# require all files inside spec_helpers
#Dir[File.join(plugin_spec_dir, "spec_helpers/*.rb")].each { |file| require file }
+#ActiveRecord::Base.logger = Logger.new(plugin_spec_dir + "/debug.log")
+
+
+module SubdomainFu
+ class TestApplication < Rails::Application
+ end
+end
-ActiveRecord::Base.logger = Logger.new(plugin_spec_dir + "/debug.log")
-Rails3::Application.routes.draw do |map|
- 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"
+SubdomainFu::TestApplication.routes.draw do
+ match '/needs_subdomain' => "fu#awesome", :as => 'needs_subdomain'
+ match '/no_subdomain' => "fu#lame", :as => 'no_subdomain'
+ match '/needs_awesome' => "fu#lame", :as => 'needs_awesome'
- map.resources :foos do |fu|
- fu.resources :bars
+ resources :foos do
+ 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"}
+ match '/' => "site#home", :constraints => { :subdomain => '' }
+ #match '/' => "app#home", :constraints => { :subdomain => true }
+ match '/' => "mobile#home", :constraints => { :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]+/}
+ #match '/subdomain_here' => "app#success", :constraints => { :subdomain => true }
+ match '/no_subdomain_here' => "site#success", :constraints => { :subdomain => '' }
+ match '/m_subdomain_here' => "mobile#success", :constraints => { :subdomain => "m" }
+ match '/numbers_only_here' => "numbers#success", :constraints => { :subdomain => /[0-9]+/ }
- map.connect '/:controller/:action/:id'
+ match ':controller(/:action(/:id(.:format)))'
end
class Paramed
@@ -46,4 +55,4 @@ def to_param
end
end
-include ActionController::UrlWriter
+include Rails.application.routes.url_helpers
View
58 spec/subdomain_fu_spec.rb
@@ -1,33 +1,33 @@
-require File.dirname(__FILE__) + '/spec_helper'
+require 'spec_helper'
describe "SubdomainFu" do
before do
- SubdomainFu.tld_sizes = SubdomainFu::DEFAULT_TLD_SIZES.dup
- SubdomainFu.mirrors = SubdomainFu::DEFAULT_MIRRORS.dup
- SubdomainFu.preferred_mirror = nil
+ SubdomainFu.config.tld_sizes = SubdomainFu::Configuration.defaults[:tld_sizes].dup
+ SubdomainFu.config.mirrors = SubdomainFu::Configuration.defaults[:mirrors].dup
+ SubdomainFu.config.preferred_mirror = nil
end
describe "TLD Sizes" do
before do
- SubdomainFu.tld_sizes = SubdomainFu::DEFAULT_TLD_SIZES.dup
+ SubdomainFu.config.tld_sizes = SubdomainFu::Configuration.defaults[:tld_sizes].dup
end
- it { SubdomainFu.tld_sizes.should be_kind_of(Hash) }
+ it { SubdomainFu.config.tld_sizes.should be_kind_of(Hash) }
it "should have default values for development, test, and production" do
- SubdomainFu.tld_sizes[:development].should == 0
- SubdomainFu.tld_sizes[:test].should == 0
- SubdomainFu.tld_sizes[:production].should == 1
+ SubdomainFu.config.tld_sizes[:development].should == 0
+ SubdomainFu.config.tld_sizes[:test].should == 0
+ SubdomainFu.config.tld_sizes[:production].should == 1
end
it "#tld_size should be for the current environment" do
- SubdomainFu.tld_size.should == SubdomainFu.tld_sizes[RAILS_ENV.to_sym]
+ SubdomainFu.config.tld_size.should == SubdomainFu.config.tld_sizes[Rails.env.to_sym]
end
it "should be able to be set for the current environment" do
- SubdomainFu.tld_size = 5
- SubdomainFu.tld_size.should == 5
- SubdomainFu.tld_sizes[:test].should == 5
+ SubdomainFu.config.tld_size = 5
+ SubdomainFu.config.tld_size.should == 5
+ SubdomainFu.config.tld_sizes[:test].should == 5
end
end
@@ -63,7 +63,7 @@
end
it "should be false for mirrored subdomains" do
- SubdomainFu.has_subdomain?(SubdomainFu.mirrors.first).should be_false
+ SubdomainFu.has_subdomain?(SubdomainFu.config.mirrors.first).should be_false
end
it "shoud be false for a nil or blank subdomain" do
@@ -76,11 +76,11 @@
describe "#subdomain_from" do
it "should return the subdomain based on the TLD of the current environment" do
SubdomainFu.subdomain_from("awesome.localhost").should == "awesome"
- SubdomainFu.tld_size = 2
+ SubdomainFu.config.tld_size = 2
SubdomainFu.subdomain_from("awesome.localhost.co.uk").should == "awesome"
- SubdomainFu.tld_size = 1
+ SubdomainFu.config.tld_size = 1
SubdomainFu.subdomain_from("awesome.localhost.com").should == "awesome"
- SubdomainFu.tld_size = 0
+ SubdomainFu.config.tld_size = 0
end
it "should join deep subdomains with a period" do
@@ -93,6 +93,7 @@
end
it "#host_without_subdomain should chop of the subdomain and return the rest" do
+ SubdomainFu.host_without_subdomain("localhost:3000").should == "localhost:3000"
SubdomainFu.host_without_subdomain("awesome.localhost:3000").should == "localhost:3000"
SubdomainFu.host_without_subdomain("something.awful.localhost:3000").should == "localhost:3000"
end
@@ -100,7 +101,7 @@
describe "#preferred_mirror?" do
describe "when preferred_mirror is false" do
before do
- SubdomainFu.preferred_mirror = false
+ SubdomainFu.config.preferred_mirror = false
end
it "should return true for false" do
@@ -122,6 +123,11 @@
SubdomainFu.rewrite_host_for_subdomains("cool","www.localhost").should == "cool.localhost"
end
+ it "should not change the subdomain for a host the same or smaller than the tld size" do
+ SubdomainFu.config.tld_size = 1
+ SubdomainFu.rewrite_host_for_subdomains("cool","localhost").should == "localhost"
+ end
+
it "should remove the subdomain if passed false when it's not a mirror" do
SubdomainFu.rewrite_host_for_subdomains(false,"cool.localhost").should == "localhost"
end
@@ -136,7 +142,7 @@
describe "when preferred_mirror is false" do
before do
- SubdomainFu.preferred_mirror = false
+ SubdomainFu.config.preferred_mirror = false
end
it "should remove the subdomain if passed false when it is a mirror" do
@@ -179,6 +185,11 @@
SubdomainFu.current_subdomain(request).should be_nil
end
+ it "should return current subdomain without a mirror" do
+ request = mock("request", :subdomains => ["www", "stuff"])
+ SubdomainFu.current_subdomain(request).should == "stuff"
+ end
+
it "should return the whole thing (including a .) if there's multiple subdomains" do
request = mock("request", :subdomains => ["awesome","rad"])
SubdomainFu.current_subdomain(request).should == "awesome.rad"
@@ -195,6 +206,11 @@
request = mock("request", :subdomains => [], :domain => "", :port_string => "")
SubdomainFu.current_domain(request).should == ""
end
+
+ it "should return an IP address if there is only an IP address" do
+ request = mock("request", :subdomains => [], :domain => "127.0.0.1", :port_string => "")
+ SubdomainFu.current_domain(request).should == "127.0.0.1"
+ end
it "should return the current domain if there is only one level of subdomains" do
request = mock("request", :subdomains => ["www"], :domain => "example.com", :port_string => "")
@@ -245,7 +261,7 @@
describe "when preferred_mirror is false" do
before do
- SubdomainFu.preferred_mirror = false
+ SubdomainFu.config.preferred_mirror = false
end
it { SubdomainFu.needs_rewrite?("www","www.localhost").should be_false }
@@ -262,7 +278,7 @@
describe "when preferred_mirror is string" do
before do
- SubdomainFu.preferred_mirror = "www"
+ SubdomainFu.config.preferred_mirror = "www"
end
it { SubdomainFu.needs_rewrite?("www","www.localhost").should be_false }
View
26 spec/url_rewriter_spec.rb
@@ -1,11 +1,11 @@
-require File.dirname(__FILE__) + '/spec_helper'
+require 'spec_helper'
describe "SubdomainFu URL Writing" do
before do
- SubdomainFu.tld_size = 1
- SubdomainFu.mirrors = SubdomainFu::DEFAULT_MIRRORS.dup
- SubdomainFu.preferred_mirror = nil
- SubdomainFu.override_only_path = false
+ SubdomainFu.config.tld_size = 1
+ SubdomainFu.config.mirrors = SubdomainFu::Configuration.defaults[:mirrors].dup
+ SubdomainFu.config.override_only_path = true
+ SubdomainFu.config.preferred_mirror = nil
default_url_options[:host] = "example.com"
end
@@ -23,11 +23,12 @@
end
it "should should not force the full url with :only_path if override_only_path is false (default)" do
+ SubdomainFu.config.override_only_path = false
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
+ SubdomainFu.config.override_only_path = true
url_for(:controller => "something", :action => "other", :subdomain => "awesome", :only_path => true).should == "http://awesome.example.com/something/other"
end
end
@@ -48,6 +49,7 @@
end
it "should should not force the full url with _path" do
+ SubdomainFu.config.override_only_path = false
needs_subdomain_path(:subdomain => "awesome").should == "/needs_subdomain"
end
@@ -57,6 +59,7 @@
end
it "should not force the full url even if it's a different subdomain" do
+ SubdomainFu.config.override_only_path = false
default_url_options[:host] = "awesome.example.com"
needs_subdomain_path(:subdomain => "crazy").should == "/needs_subdomain"
end
@@ -73,7 +76,7 @@
describe "With override_only_path set to true" do
before(:each) do
- SubdomainFu.override_only_path = true
+ SubdomainFu.config.override_only_path = true
end
it "should should force the full url, even with _path" do
@@ -141,7 +144,8 @@
describe "Preferred Mirror" do
before do
- SubdomainFu.preferred_mirror = "www"
+ SubdomainFu.config.preferred_mirror = "www"
+ SubdomainFu.config.override_only_path = true
end
it "should switch to the preferred mirror instead of no subdomain" do
@@ -160,18 +164,18 @@
end
it "should force a switch to no subdomain on a mirror if preferred_mirror is false" do
- SubdomainFu.preferred_mirror = false
+ SubdomainFu.config.preferred_mirror = false
default_url_options[:host] = "www.example.com"
needs_subdomain_url(:subdomain => false).should == "http://example.com/needs_subdomain"
end
after do
- SubdomainFu.preferred_mirror = nil
+ SubdomainFu.config.preferred_mirror = nil
end
end
after do
- SubdomainFu.tld_size = 0
+ SubdomainFu.config.tld_size = 0
default_url_options[:host] = "localhost"
end
end
View
35 subdomain-fu.gemspec
@@ -1,49 +1,48 @@
# Generated by jeweler
# DO NOT EDIT THIS FILE DIRECTLY
-# Instead, edit Jeweler::Tasks in rakefile, and run the gemspec command
+# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
# -*- encoding: utf-8 -*-
Gem::Specification.new do |s|
s.name = %q{subdomain-fu}
- s.version = "1.0.0.beta2"
+ s.version = "1.0.0.beta3"
s.required_rubygems_version = Gem::Requirement.new("> 1.3.1") if s.respond_to? :required_rubygems_version=
s.authors = ["Michael Bleigh"]
- s.date = %q{2010-04-30}
+ s.date = %q{2011-01-04}
s.description = %q{SubdomainFu is a Rails plugin to provide all of the basic functionality necessary to handle multiple subdomain applications (such as Basecamp-esque subdomain accounts and more).}
s.email = %q{michael@intridea.com}
s.extra_rdoc_files = [
- "CHANGELOG",
- "README.rdoc"
+ "README.rdoc"
]
s.files = [
"CHANGELOG",
- "MIT-LICENSE",
- "README.rdoc",
- "Rakefile",
- "VERSION.yml",
- "lib/subdomain-fu.rb",
- "lib/subdomain_fu/rails.rb",
- "lib/subdomain_fu/url_rewriter.rb",
- "subdomain-fu.gemspec"
+ "MIT-LICENSE",
+ "README.rdoc",
+ "Rakefile",
+ "VERSION.yml",
+ "lib/subdomain-fu.rb",
+ "lib/subdomain_fu/engine.rb",
+ "lib/subdomain_fu/plugin.rb",
+ "lib/subdomain_fu/subdomain_fu.rb",
+ "lib/subdomain_fu/url_rewriter.rb"
]
s.homepage = %q{http://github.com/mbleigh/subdomain-fu}
- s.rdoc_options = ["--charset=UTF-8"]
s.require_paths = ["lib"]
s.rubyforge_project = %q{subdomain-fu}
- s.rubygems_version = %q{1.3.6}
+ s.rubygems_version = %q{1.3.7}
s.summary = %q{SubdomainFu is a Rails plugin that provides subdomain routing and URL writing helpers.}
s.test_files = [
"spec/spec_helper.rb",
- "spec/subdomain_fu_spec.rb",
- "spec/url_rewriter_spec.rb"
+ "spec/subdomain_fu_spec.rb",
+ "spec/url_rewriter_spec.rb"
]
if s.respond_to? :specification_version then
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
s.specification_version = 3
- if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
else
end
else
Please sign in to comment.
Something went wrong with that request. Please try again.