pboling-subdomain-fu 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/CHANGELOG ADDED
@@ -0,0 +1,7 @@
1
+ == 2009-08-24
2
+
3
+ * Merged forks that added lots of features, tests, and fixes
4
+
5
+ == 2008-06-25
6
+
7
+ * Removed stray puts
data/MIT-LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ Copyright (c) 2008 Michael Bleigh (http://www.mbleigh.com) and
2
+ Intridea, Inc (http://www.intridea.com)
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining
5
+ a copy of this software and associated documentation files (the
6
+ "Software"), to deal in the Software without restriction, including
7
+ without limitation the rights to use, copy, modify, merge, publish,
8
+ distribute, sublicense, and/or sell copies of the Software, and to
9
+ permit persons to whom the Software is furnished to do so, subject to
10
+ the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,96 @@
1
+ = SubdomainFu
2
+
3
+ SubdomainFu provides a modern implementation of subdomain handling in Rails.
4
+ It takes aspects from account_location, request_routing, and other snippets
5
+ found around the web and combines them to provide a single, simple solution
6
+ for subdomain-based route and url management.
7
+
8
+ == Installation
9
+
10
+ SubdomainFu is available both as a traditional plugin and a GemPlugin. To
11
+ install it as a traditional plugin (Rails 2.1 or later):
12
+
13
+ script/plugin install git://github.com/pboling/subdomain-fu.git
14
+
15
+ To use it as a GemPlugin, add it to your environment.rb:
16
+
17
+ config.gem 'pboling-subdomain-fu', :source => "http://gems.github.com", :lib => "subdomain-fu"
18
+
19
+
20
+ == Examples
21
+
22
+ SubdomainFu works inside of Rails's URL Writing mechanisms to provide an easy and seamless
23
+ way to link and otherwise understand cross-subdomain routing. You can use the :subdomain
24
+ option both in named and non-named routes as well as in generated resources routes.
25
+
26
+ Let's say my domain is 'intridea.com'. Here are some examples of the use of the :subdomain
27
+ option:
28
+
29
+ url_for(:controller => "my_controller",
30
+ :action => "my_action",
31
+ :subdomain => "awesome") # => http://awesome.intridea.com/my_controller/my_action
32
+
33
+ Now let's say I'm at http://awesome.intridea.com/ and I want back to the root.
34
+ Specifying "false" will remove any current subdomain:
35
+
36
+ users_url(:subdomain => false) # => http://intridea.com/users
37
+
38
+ Note that this plugin does not honor the :only_path notion of routing when doing
39
+ so would go against the intent of the command. For example, if I were at http://intridea.com
40
+ again:
41
+
42
+ users_path(:subdomain => "fun") # => http://fun.intridea.com/users
43
+ users_path(:subdomain => false) # => /users
44
+
45
+ In this way you can rest assured that you will never misdirect your links to the
46
+ same subdomain when you meant to change it.
47
+
48
+ == Configuration
49
+
50
+ You may need to configure SubdomainFu based on your development setup. The
51
+ configuration required is:
52
+
53
+ tld_size
54
+ --------
55
+
56
+ A hash for each environment of the size of the top-level domain name.
57
+ (something.com = 1, localhost = 0, etc.)
58
+
59
+ SubdomainFu.tld_size = 1 # sets for current environment
60
+ SubdomainFu.tld_sizes = {:development => 0,
61
+ :test => 0,
62
+ :production => 1} # set all at once (also the defaults)
63
+
64
+ mirrors
65
+ -------
66
+
67
+ Mirrors are the subdomains that are equivalent to no subdomain (i.e. they 'mirror')
68
+ the usage of the root domain.
69
+
70
+ SubdomainFu.mirrors = %w(www site we) # Defaults to %w(www)
71
+
72
+ preferred_mirror
73
+ ----------------
74
+
75
+ SubdomainFu also understands the notion of a 'preferred mirror', that is, if you
76
+ always want your links going to 'www.yourdomain.com' instead of 'yourdomain.com',
77
+ you can set the preferred mirror like so:
78
+
79
+ SubdomainFu.preferred_mirror = "www"
80
+
81
+ Now when you create a link to a false subdomain
82
+
83
+ == Known Issues / Future Work
84
+
85
+ SubdomainFu will eventually integrate with Rails' routing internals to provide
86
+ the ability to specify routes based on the condition of a specific subdomain or
87
+ simply whether a subdomain is present (or a mirror).
88
+
89
+ == Resources
90
+
91
+ * Acts As Community Project: http://actsascommunity.com/projects/subdomain-fu
92
+ * GitHub Repository: http://github.com/mbleigh/subdomain-fu
93
+ * Lighthouse: http://mbleigh.lighthouseapp.com/projects/13148-subdomain-fu
94
+
95
+ Copyright (c) 2008 Michael Bleigh (http://www.mbleigh.com/) and
96
+ Intridea, Inc. (http://www.intridea.com/). Released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,27 @@
1
+ require 'rake'
2
+ require 'spec/rake/spectask'
3
+
4
+ desc 'Default: run specs.'
5
+ task :default => :spec
6
+
7
+ desc 'Run the specs'
8
+ Spec::Rake::SpecTask.new(:spec) do |t|
9
+ t.spec_opts = ['--colour --format progress --loadby mtime --reverse']
10
+ t.spec_files = FileList['spec/**/*_spec.rb']
11
+ end
12
+
13
+ begin
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gemspec|
16
+ gemspec.name = "subdomain-fu"
17
+ gemspec.summary = "SubdomainFu is a Rails plugin that provides subdomain routing and URL writing helpers."
18
+ gemspec.email = "michael@intridea.com"
19
+ gemspec.homepage = "http://github.com/mbleigh/subdomain-fu"
20
+ gemspec.files = FileList["[A-Z]*", "{lib,spec,rails}/**/*"] - FileList["**/*.log"]
21
+ gemspec.description = "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)."
22
+ gemspec.authors = ["Michael Bleigh"]
23
+ end
24
+ rescue LoadError
25
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
26
+ end
27
+
data/VERSION.yml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ :major: 0
3
+ :minor: 2
4
+ :patch: 1
@@ -0,0 +1,119 @@
1
+ require 'subdomain_fu/routing_extensions'
2
+ require 'subdomain_fu/url_rewriter'
3
+
4
+ module SubdomainFu
5
+ # The length of the period-split top-level domain for each environment.
6
+ # For example, "localhost" has a tld_size of zero, and "something.co.uk"
7
+ # has a tld_size of two.
8
+ #
9
+ # To set a tld size for a given environment, just call SubdomainFu.tld_sizes[:environment] = value
10
+ DEFAULT_TLD_SIZES = {:development => 0, :test => 0, :production => 1}
11
+ mattr_accessor :tld_sizes
12
+ @@tld_sizes = DEFAULT_TLD_SIZES.dup
13
+
14
+ # Subdomains that are equivalent to going to the website with no subdomain at all.
15
+ # Defaults to "www" as the only member.
16
+ DEFAULT_MIRRORS = %w(www)
17
+ mattr_accessor :mirrors
18
+ @@mirrors = DEFAULT_MIRRORS.dup
19
+
20
+ mattr_accessor :preferred_mirror
21
+ @@preferred_mirror = nil
22
+
23
+ mattr_accessor :override_only_path
24
+ @@override_only_path = false
25
+
26
+ # Returns the TLD Size of the current environment.
27
+ def self.tld_size
28
+ tld_sizes[RAILS_ENV.to_sym]
29
+ end
30
+
31
+ # Sets the TLD Size of the current environment
32
+ def self.tld_size=(value)
33
+ tld_sizes[RAILS_ENV.to_sym] = value
34
+ end
35
+
36
+ # Is the current subdomain either nil or a mirror?
37
+ def self.has_subdomain?(subdomain)
38
+ subdomain != false && !subdomain.blank? && !SubdomainFu.mirrors.include?(subdomain)
39
+ end
40
+
41
+ # Is the subdomain a preferred mirror
42
+ def self.preferred_mirror?(subdomain)
43
+ subdomain == SubdomainFu.preferred_mirror || SubdomainFu.preferred_mirror.nil?
44
+ end
45
+
46
+ # Gets the subdomain from the host based on the TLD size
47
+ def self.subdomain_from(host)
48
+ return nil if host.nil? || /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.match(host)
49
+ parts = host.split('.')
50
+ sub = parts[0..-(SubdomainFu.tld_size+2)].join(".")
51
+ sub.blank? ? nil : sub
52
+ end
53
+
54
+ # Gets only non-mirror subdomains from the host based on the TLD size
55
+ def self.non_mirror_subdomain_from(host)
56
+ sub = subdomain_from(host)
57
+ has_subdomain?(sub) ? sub : nil
58
+ end
59
+
60
+ def self.host_without_subdomain(host)
61
+ parts = host.split('.')
62
+ parts[-(SubdomainFu.tld_size+1)..-1].join(".")
63
+ end
64
+
65
+ # Rewrites the subdomain of the host unless they are equivalent (i.e. mirrors of each other)
66
+ def self.rewrite_host_for_subdomains(subdomain, host)
67
+ if needs_rewrite?(subdomain, host)
68
+ change_subdomain_of_host(subdomain || SubdomainFu.preferred_mirror, host)
69
+ else
70
+ if has_subdomain?(subdomain) || preferred_mirror?(subdomain_from(host)) ||
71
+ (subdomain.nil? && has_subdomain?(subdomain_from(host)))
72
+ host
73
+ else
74
+ change_subdomain_of_host(SubdomainFu.preferred_mirror, host)
75
+ end
76
+ end
77
+ end
78
+
79
+ # Changes the subdomain of the host to whatever is passed in.
80
+ def self.change_subdomain_of_host(subdomain, host)
81
+ host = SubdomainFu.host_without_subdomain(host)
82
+ host = "#{subdomain}.#{host}" if subdomain
83
+ host
84
+ end
85
+
86
+ # Is this subdomain equivalent to the subdomain found in this host string?
87
+ def self.same_subdomain?(subdomain, host)
88
+ subdomain = nil unless subdomain
89
+ (subdomain == subdomain_from(host)) ||
90
+ (!has_subdomain?(subdomain) && !has_subdomain?(subdomain_from(host)))
91
+ end
92
+
93
+ def self.needs_rewrite?(subdomain, host)
94
+ return false if subdomain.nil?
95
+
96
+ (!has_subdomain?(subdomain) && preferred_mirror?(subdomain) && !preferred_mirror?(subdomain_from(host))) ||
97
+ !same_subdomain?(subdomain, host)
98
+ end
99
+
100
+ def self.current_subdomain(request)
101
+ subdomain = request.subdomains(SubdomainFu.tld_size).join(".")
102
+ if has_subdomain?(subdomain)
103
+ subdomain
104
+ else
105
+ nil
106
+ end
107
+ end
108
+
109
+ module Controller
110
+ def self.included(controller)
111
+ controller.helper_method(:current_subdomain)
112
+ end
113
+
114
+ protected
115
+ def current_subdomain
116
+ SubdomainFu.current_subdomain(request)
117
+ end
118
+ end
119
+ end
@@ -0,0 +1,61 @@
1
+ # Thanks to Jamis Buck for ideas on this stuff
2
+ # http://weblog.jamisbuck.org/2006/10/26/monkey-patching-rails-extending-routes-2
3
+ # This is not yet a working part of SubdomainFu.
4
+
5
+ module SubdomainFu
6
+ module RouteExtensions
7
+ def self.included(base)
8
+ base.alias_method_chain :recognition_conditions, :subdomain
9
+ end
10
+
11
+ def recognition_conditions_with_subdomain
12
+ result = recognition_conditions_without_subdomain
13
+ result << "conditions[:subdomain] === env[:subdomain]" if conditions[:subdomain] && conditions[:subdomain] != true && conditions[:subdomain] != false
14
+ result << "SubdomainFu.has_subdomain?(env[:subdomain])" if conditions[:subdomain] == true
15
+ result << "!SubdomainFu.has_subdomain?(env[:subdomain])" if conditions[:subdomain] == false
16
+ result
17
+ end
18
+ end
19
+
20
+ module RouteSetExtensions
21
+ def self.included(base)
22
+ base.alias_method_chain :extract_request_environment, :subdomain
23
+ end
24
+
25
+ def extract_request_environment_with_subdomain(request)
26
+ env = extract_request_environment_without_subdomain(request)
27
+ env.merge(:host => request.host, :domain => request.domain, :subdomain => SubdomainFu.subdomain_from(request.host))
28
+ end
29
+ end
30
+
31
+ module MapperExtensions
32
+ def quick_map(has_unless, *args, &block)
33
+ options = args.find{|a| a.is_a?(Hash)}
34
+ namespace_str = options ? options.delete(:namespace).to_s : args.join('_or_')
35
+ namespace_str += '_' unless namespace_str.blank?
36
+ mapped_exp = args.map(&:to_s).join('|')
37
+ conditions_hash = { :subdomain => ( has_unless ? /[^(#{mapped_exp})]/ : /(#{mapped_exp})/) }
38
+ with_options(:conditions => conditions_hash, :name_prefix => namespace_str, &block)
39
+ end
40
+ # Adds methods to Mapper to apply an options with a method. Example
41
+ # map.subdomain :blog { |blog| blog.resources :pages }
42
+ # or
43
+ # map.unless_subdomain :blog { |not_blog| not_blog.resources :people }
44
+ def subdomain(*args, &block)
45
+ quick_map(false, *args, &block)
46
+ end
47
+ def unless_subdomain(*args, &block)
48
+ quick_map(true, *args, &block)
49
+ end
50
+ end
51
+ end
52
+
53
+ ActionController::Routing::RouteSet::Mapper.send :include, SubdomainFu::MapperExtensions
54
+ ActionController::Routing::RouteSet.send :include, SubdomainFu::RouteSetExtensions
55
+ ActionController::Routing::Route.send :include, SubdomainFu::RouteExtensions
56
+
57
+ # UrlRewriter::RESERVED_OPTIONS is only available in Rails >= 2.2
58
+ # http://www.portallabs.com/blog/2008/12/02/fixing-subdomain_fu-with-named-routes-rails-22/
59
+ if Rails::VERSION::MAJOR >= 2 and Rails::VERSION::MINOR >= 2
60
+ ActionController::UrlRewriter::RESERVED_OPTIONS << :subdomain
61
+ end
@@ -0,0 +1,47 @@
1
+ module ActionController
2
+ module UrlWriter
3
+ def url_for_with_subdomains(options)
4
+ if SubdomainFu.needs_rewrite?(options[:subdomain], options[:host] || default_url_options[:host])
5
+ options[:only_path] = false if SubdomainFu.override_only_path
6
+ options[:host] = SubdomainFu.rewrite_host_for_subdomains(options.delete(:subdomain), options[:host] || default_url_options[:host])
7
+ else
8
+ options.delete(:subdomain)
9
+ end
10
+ url_for_without_subdomains(options)
11
+ end
12
+ alias_method_chain :url_for, :subdomains
13
+ end
14
+
15
+ class UrlRewriter #:nodoc:
16
+ private
17
+
18
+ def rewrite_url_with_subdomains(options)
19
+ if SubdomainFu.needs_rewrite?(options[:subdomain], (options[:host] || @request.host_with_port))
20
+ options[:only_path] = false if SubdomainFu.override_only_path
21
+ options[:host] = SubdomainFu.rewrite_host_for_subdomains(options.delete(:subdomain), options[:host] || @request.host_with_port)
22
+ else
23
+ options.delete(:subdomain)
24
+ end
25
+ rewrite_url_without_subdomains(options)
26
+ end
27
+ alias_method_chain :rewrite_url, :subdomains
28
+ end
29
+
30
+ if Rails::VERSION::MAJOR >= 2 and Rails::VERSION::MINOR <= 1
31
+ # hack for http://www.portallabs.com/blog/2008/10/22/fixing-subdomain_fu-with-named-routes/
32
+ module Routing
33
+ module Optimisation
34
+ class PositionalArgumentsWithAdditionalParams
35
+ def guard_condition_with_subdomains
36
+ # don't allow optimisation if a subdomain is present - fixes a problem
37
+ # with the subdomain appearing in the query instead of being rewritten
38
+ # see http://mbleigh.lighthouseapp.com/projects/13148/tickets/8-improper-generated-urls-with-named-routes-for-a-singular-resource
39
+ guard_condition_without_subdomains + " && !args.last.has_key?(:subdomain)"
40
+ end
41
+
42
+ alias_method_chain :guard_condition, :subdomains
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
data/rails/init.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'action_controller'
2
+
3
+ # Add this path to ruby load path
4
+ $:.unshift "#{File.dirname(__FILE__)}/../lib"
5
+
6
+ require 'subdomain-fu' unless defined?(SubdomainFu)
7
+
8
+ ActionController::Base.send :include, SubdomainFu::Controller
9
+
10
+ RAILS_DEFAULT_LOGGER.info("** SubdomainFu: initialized properly")
data/spec/spec.opts ADDED
@@ -0,0 +1,7 @@
1
+ --colour
2
+ --format
3
+ specdoc
4
+ --loadby
5
+ mtime
6
+ --reverse
7
+ --backtrace
@@ -0,0 +1,42 @@
1
+ begin
2
+ require File.dirname(__FILE__) + '/../../../../spec/spec_helper'
3
+ rescue LoadError
4
+ puts "You need to install rspec in your base app"
5
+ exit
6
+ end
7
+
8
+ plugin_spec_dir = File.dirname(__FILE__)
9
+ ActiveRecord::Base.logger = Logger.new(plugin_spec_dir + "/debug.log")
10
+
11
+ ActionController::Routing::Routes.draw do |map|
12
+ map.needs_subdomain '/needs_subdomain', :controller => "fu", :action => "awesome"
13
+ map.no_subdomain '/no_subdomain', :controller => "fu", :action => "lame"
14
+ map.needs_awesome '/needs_awesome', :controller => "fu", :action => "lame"
15
+
16
+ map.resources :foos do |fu|
17
+ fu.resources :bars
18
+ end
19
+
20
+ map.connect '/', :controller => "site", :action => "home", :conditions => {:subdomain => false}
21
+ map.connect '/', :controller => "app", :action => "home", :conditions => {:subdomain => true}
22
+ map.connect '/', :controller => "mobile", :action => "home", :conditions => {:subdomain => "m"}
23
+
24
+ map.connect '/subdomain_here', :controller => "app", :action => "success", :conditions => {:subdomain => true}
25
+ map.connect '/no_subdomain_here', :controller => "site", :action => "success", :conditions => {:subdomain => false}
26
+ map.connect '/m_subdomain_here', :controller => "mobile", :action => "success", :conditions => {:subdomain => "m"}
27
+ map.connect '/numbers_only_here', :controller => "numbers", :action => "success", :conditions => {:subdomain => /[0-9]+/}
28
+
29
+ map.connect '/:controller/:action/:id'
30
+ end
31
+
32
+ class Paramed
33
+ def initialize(param)
34
+ @param = param
35
+ end
36
+
37
+ def to_param
38
+ @param || "param"
39
+ end
40
+ end
41
+
42
+ include ActionController::UrlWriter
@@ -0,0 +1,191 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe "SubdomainFu" do
4
+ before do
5
+ SubdomainFu.tld_sizes = SubdomainFu::DEFAULT_TLD_SIZES.dup
6
+ SubdomainFu.mirrors = SubdomainFu::DEFAULT_MIRRORS.dup
7
+ SubdomainFu.preferred_mirror = nil
8
+ end
9
+
10
+ describe "TLD Sizes" do
11
+ before do
12
+ SubdomainFu.tld_sizes = SubdomainFu::DEFAULT_TLD_SIZES.dup
13
+ end
14
+
15
+ it { SubdomainFu.tld_sizes.should be_kind_of(Hash) }
16
+
17
+ it "should have default values for development, test, and production" do
18
+ SubdomainFu.tld_sizes[:development].should == 0
19
+ SubdomainFu.tld_sizes[:test].should == 0
20
+ SubdomainFu.tld_sizes[:production].should == 1
21
+ end
22
+
23
+ it "#tld_size should be for the current environment" do
24
+ SubdomainFu.tld_size.should == SubdomainFu.tld_sizes[RAILS_ENV.to_sym]
25
+ end
26
+
27
+ it "should be able to be set for the current environment" do
28
+ SubdomainFu.tld_size = 5
29
+ SubdomainFu.tld_size.should == 5
30
+ SubdomainFu.tld_sizes[:test].should == 5
31
+ end
32
+ end
33
+
34
+ describe "#has_subdomain?" do
35
+ it "should be true for non-mirrored subdomains" do
36
+ SubdomainFu.has_subdomain?("awesome").should be_true
37
+ end
38
+
39
+ it "should be false for mirrored subdomains" do
40
+ SubdomainFu.has_subdomain?(SubdomainFu.mirrors.first).should be_false
41
+ end
42
+
43
+ it "shoud be false for a nil or blank subdomain" do
44
+ SubdomainFu.has_subdomain?("").should be_false
45
+ SubdomainFu.has_subdomain?(nil).should be_false
46
+ SubdomainFu.has_subdomain?(false).should be_false
47
+ end
48
+ end
49
+
50
+ describe "#subdomain_from" do
51
+ it "should return the subdomain based on the TLD of the current environment" do
52
+ SubdomainFu.subdomain_from("awesome.localhost").should == "awesome"
53
+ SubdomainFu.tld_size = 2
54
+ SubdomainFu.subdomain_from("awesome.localhost.co.uk").should == "awesome"
55
+ SubdomainFu.tld_size = 1
56
+ SubdomainFu.subdomain_from("awesome.localhost.com").should == "awesome"
57
+ SubdomainFu.tld_size = 0
58
+ end
59
+
60
+ it "should join deep subdomains with a period" do
61
+ SubdomainFu.subdomain_from("awesome.coolguy.localhost").should == "awesome.coolguy"
62
+ end
63
+
64
+ it "should return nil for no subdomain" do
65
+ SubdomainFu.subdomain_from("localhost").should be_nil
66
+ end
67
+ end
68
+
69
+ it "#host_without_subdomain should chop of the subdomain and return the rest" do
70
+ SubdomainFu.host_without_subdomain("awesome.localhost:3000").should == "localhost:3000"
71
+ SubdomainFu.host_without_subdomain("something.awful.localhost:3000").should == "localhost:3000"
72
+ end
73
+
74
+ describe "#preferred_mirror?" do
75
+ describe "when preferred_mirror is false" do
76
+ before do
77
+ SubdomainFu.preferred_mirror = false
78
+ end
79
+
80
+ it "should return true for false" do
81
+ SubdomainFu.preferred_mirror?(false).should be_true
82
+ end
83
+ end
84
+ end
85
+
86
+ describe "#rewrite_host_for_subdomains" do
87
+ it "should not change the same subdomain" do
88
+ SubdomainFu.rewrite_host_for_subdomains("awesome","awesome.localhost").should == "awesome.localhost"
89
+ end
90
+
91
+ it "should not change an equivalent (mirrored) subdomain" do
92
+ SubdomainFu.rewrite_host_for_subdomains("www","localhost").should == "localhost"
93
+ end
94
+
95
+ it "should change the subdomain if it's different" do
96
+ SubdomainFu.rewrite_host_for_subdomains("cool","www.localhost").should == "cool.localhost"
97
+ end
98
+
99
+ it "should remove the subdomain if passed false when it's not a mirror" do
100
+ SubdomainFu.rewrite_host_for_subdomains(false,"cool.localhost").should == "localhost"
101
+ end
102
+
103
+ it "should not remove the subdomain if passed false when it is a mirror" do
104
+ SubdomainFu.rewrite_host_for_subdomains(false,"www.localhost").should == "www.localhost"
105
+ end
106
+
107
+ it "should not remove the subdomain if passed nil when it's not a mirror" do
108
+ SubdomainFu.rewrite_host_for_subdomains(nil,"cool.localhost").should == "cool.localhost"
109
+ end
110
+
111
+ describe "when preferred_mirror is false" do
112
+ before do
113
+ SubdomainFu.preferred_mirror = false
114
+ end
115
+
116
+ it "should remove the subdomain if passed false when it is a mirror" do
117
+ SubdomainFu.rewrite_host_for_subdomains(false,"www.localhost").should == "localhost"
118
+ end
119
+
120
+ it "should not remove the subdomain if passed nil when it's not a mirror" do
121
+ SubdomainFu.rewrite_host_for_subdomains(nil,"cool.localhost").should == "cool.localhost"
122
+ end
123
+ end
124
+ end
125
+
126
+ describe "#change_subdomain_of_host" do
127
+ it "should change it if passed a different one" do
128
+ SubdomainFu.change_subdomain_of_host("awesome","cool.localhost").should == "awesome.localhost"
129
+ end
130
+
131
+ it "should remove it if passed nil" do
132
+ SubdomainFu.change_subdomain_of_host(nil,"cool.localhost").should == "localhost"
133
+ end
134
+
135
+ it "should add it if there isn't one" do
136
+ SubdomainFu.change_subdomain_of_host("awesome","localhost").should == "awesome.localhost"
137
+ end
138
+ end
139
+
140
+ describe "#current_subdomain" do
141
+ it "should return the current subdomain if there is one" do
142
+ request = mock("request", :subdomains => ["awesome"])
143
+ SubdomainFu.current_subdomain(request).should == "awesome"
144
+ end
145
+
146
+ it "should return nil if there's no subdomain" do
147
+ request = mock("request", :subdomains => [])
148
+ SubdomainFu.current_subdomain(request).should be_nil
149
+ end
150
+
151
+ it "should return nil if the current subdomain is a mirror" do
152
+ request = mock("request", :subdomains => ["www"])
153
+ SubdomainFu.current_subdomain(request).should be_nil
154
+ end
155
+
156
+ it "should return the whole thing (including a .) if there's multiple subdomains" do
157
+ request = mock("request", :subdomains => ["awesome","rad"])
158
+ SubdomainFu.current_subdomain(request).should == "awesome.rad"
159
+ end
160
+ end
161
+
162
+ describe "#same_subdomain?" do
163
+ it { SubdomainFu.same_subdomain?("www","www.localhost").should be_true }
164
+ it { SubdomainFu.same_subdomain?("www","localhost").should be_true }
165
+ it { SubdomainFu.same_subdomain?("awesome","www.localhost").should be_false }
166
+ it { SubdomainFu.same_subdomain?("cool","awesome.localhost").should be_false }
167
+ it { SubdomainFu.same_subdomain?(nil,"www.localhost").should be_true }
168
+ it { SubdomainFu.same_subdomain?("www","awesome.localhost").should be_false }
169
+ end
170
+
171
+ describe "#needs_rewrite?" do
172
+ it { SubdomainFu.needs_rewrite?("www","www.localhost").should be_false }
173
+ it { SubdomainFu.needs_rewrite?("www","localhost").should be_false }
174
+ it { SubdomainFu.needs_rewrite?("awesome","www.localhost").should be_true }
175
+ it { SubdomainFu.needs_rewrite?("cool","awesome.localhost").should be_true }
176
+ it { SubdomainFu.needs_rewrite?(nil,"www.localhost").should be_false }
177
+ it { SubdomainFu.needs_rewrite?(nil,"awesome.localhost").should be_false }
178
+ it { SubdomainFu.needs_rewrite?(false,"awesome.localhost").should be_true }
179
+ it { SubdomainFu.needs_rewrite?(false,"www.localhost").should be_false }
180
+ it { SubdomainFu.needs_rewrite?("www","awesome.localhost").should be_true }
181
+
182
+ describe "when preferred_mirror is false" do
183
+ before do
184
+ SubdomainFu.preferred_mirror = false
185
+ end
186
+
187
+ it { SubdomainFu.needs_rewrite?(false,"www.localhost").should be_true }
188
+ it { SubdomainFu.needs_rewrite?(nil,"awesome.localhost").should be_false }
189
+ end
190
+ end
191
+ end
@@ -0,0 +1,146 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe "SubdomainFu URL Writing" do
4
+ before do
5
+ SubdomainFu.tld_size = 1
6
+ SubdomainFu.mirrors = SubdomainFu::DEFAULT_MIRRORS.dup
7
+ SubdomainFu.preferred_mirror = nil
8
+ default_url_options[:host] = "example.com"
9
+ end
10
+
11
+ describe "#url_for" do
12
+ it "should be able to add a subdomain" do
13
+ url_for(:controller => "something", :action => "other", :subdomain => "awesome").should == "http://awesome.example.com/something/other"
14
+ end
15
+
16
+ it "should be able to remove a subdomain" do
17
+ url_for(:controller => "something", :action => "other", :subdomain => false, :host => "awesome.example.com").should == "http://example.com/something/other"
18
+ end
19
+
20
+ it "should not change a mirrored subdomain" do
21
+ url_for(:controller => "something", :action => "other", :subdomain => false, :host => "www.example.com").should == "http://www.example.com/something/other"
22
+ end
23
+
24
+ it "should should not force the full url with :only_path if override_only_path is false (default)" do
25
+ url_for(:controller => "something", :action => "other", :subdomain => "awesome", :only_path => true).should == "/something/other"
26
+ end
27
+
28
+ it "should should force the full url, even with :only_path if override_only_path is true" do
29
+ SubdomainFu.override_only_path = true
30
+ url_for(:controller => "something", :action => "other", :subdomain => "awesome", :only_path => true).should == "http://awesome.example.com/something/other"
31
+ end
32
+ end
33
+
34
+ describe "Standard Routes" do
35
+ it "should be able to add a subdomain" do
36
+ needs_subdomain_url(:subdomain => "awesome").should == "http://awesome.example.com/needs_subdomain"
37
+ end
38
+
39
+ it "should be able to remove a subdomain" do
40
+ default_url_options[:host] = "awesome.example.com"
41
+ needs_subdomain_url(:subdomain => false).should == "http://example.com/needs_subdomain"
42
+ end
43
+
44
+ it "should not change a mirrored subdomain" do
45
+ default_url_options[:host] = "www.example.com"
46
+ needs_subdomain_url(:subdomain => false).should == "http://www.example.com/needs_subdomain"
47
+ end
48
+
49
+ it "should should force the full url, even with _path" do
50
+ needs_subdomain_path(:subdomain => "awesome").should == needs_subdomain_url(:subdomain => "awesome")
51
+ end
52
+
53
+ it "should not force the full url if it's the same as the current subdomain" do
54
+ default_url_options[:host] = "awesome.example.com"
55
+ needs_subdomain_path(:subdomain => "awesome").should == "/needs_subdomain"
56
+ end
57
+
58
+ it "should force the full url if it's a different subdomain" do
59
+ default_url_options[:host] = "awesome.example.com"
60
+ needs_subdomain_path(:subdomain => "crazy").should == "http://crazy.example.com/needs_subdomain"
61
+ end
62
+
63
+ it "should not force the full url if the current subdomain is nil and so is the target" do
64
+ needs_subdomain_path(:subdomain => nil).should == "/needs_subdomain"
65
+ end
66
+
67
+ it "should not force the full url if no :subdomain option is given" do
68
+ needs_subdomain_path.should == "/needs_subdomain"
69
+ default_url_options[:host] = "awesome.example.com"
70
+ needs_subdomain_path.should == "/needs_subdomain"
71
+ end
72
+ end
73
+
74
+ describe "Resourced Routes" do
75
+ it "should be able to add a subdomain" do
76
+ foo_path(:id => "something", :subdomain => "awesome").should == "http://awesome.example.com/foos/something"
77
+ end
78
+
79
+ it "should be able to remove a subdomain" do
80
+ default_url_options[:host] = "awesome.example.com"
81
+ foo_path(:id => "something", :subdomain => false).should == "http://example.com/foos/something"
82
+ end
83
+
84
+ it "should work when passed in a paramable object" do
85
+ foo_path(Paramed.new("something"), :subdomain => "awesome").should == "http://awesome.example.com/foos/something"
86
+ end
87
+
88
+ it "should work when passed in a paramable object" do
89
+ foo_path(Paramed.new("something"), :subdomain => "awesome").should == "http://awesome.example.com/foos/something"
90
+ end
91
+
92
+ it "should work when passed in a paramable object and no subdomain to a _path" do
93
+ default_url_options[:host] = "awesome.example.com"
94
+ foo_path(Paramed.new("something")).should == "/foos/something"
95
+ end
96
+
97
+ it "should work when passed in a paramable object and no subdomain to a _url" do
98
+ default_url_options[:host] = "awesome.example.com"
99
+ foo_url(Paramed.new("something")).should == "http://awesome.example.com/foos/something"
100
+ end
101
+
102
+ it "should work on nested resource collections" do
103
+ foo_bars_path(Paramed.new("something"), :subdomain => "awesome").should == "http://awesome.example.com/foos/something/bars"
104
+ end
105
+
106
+ it "should work on nested resource members" do
107
+ foo_bar_path(Paramed.new("something"),Paramed.new("else"), :subdomain => "awesome").should == "http://awesome.example.com/foos/something/bars/else"
108
+ end
109
+ end
110
+
111
+ describe "Preferred Mirror" do
112
+ before do
113
+ SubdomainFu.preferred_mirror = "www"
114
+ end
115
+
116
+ it "should switch to the preferred mirror instead of no subdomain" do
117
+ default_url_options[:host] = "awesome.example.com"
118
+ needs_subdomain_url(:subdomain => false).should == "http://www.example.com/needs_subdomain"
119
+ end
120
+
121
+ it "should switch to the preferred mirror automatically" do
122
+ default_url_options[:host] = "example.com"
123
+ needs_subdomain_url.should == "http://www.example.com/needs_subdomain"
124
+ end
125
+
126
+ it "should work when passed in a paramable object and no subdomain to a _url" do
127
+ default_url_options[:host] = "awesome.example.com"
128
+ foo_url(Paramed.new("something")).should == "http://awesome.example.com/foos/something"
129
+ end
130
+
131
+ it "should force a switch to no subdomain on a mirror if preferred_mirror is false" do
132
+ SubdomainFu.preferred_mirror = false
133
+ default_url_options[:host] = "www.example.com"
134
+ needs_subdomain_url(:subdomain => false).should == "http://example.com/needs_subdomain"
135
+ end
136
+
137
+ after do
138
+ SubdomainFu.preferred_mirror = nil
139
+ end
140
+ end
141
+
142
+ after do
143
+ SubdomainFu.tld_size = 0
144
+ default_url_options[:host] = "localhost"
145
+ end
146
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pboling-subdomain-fu
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.1
5
+ platform: ruby
6
+ authors:
7
+ - Michael Bleigh
8
+ - Peter Boling
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2009-08-24 00:00:00 -07:00
14
+ default_executable:
15
+ dependencies: []
16
+
17
+ description: 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).
18
+ email: michael@intridea.com
19
+ executables: []
20
+
21
+ extensions: []
22
+
23
+ extra_rdoc_files: []
24
+
25
+ files:
26
+ - CHANGELOG
27
+ - MIT-LICENSE
28
+ - README.rdoc
29
+ - Rakefile
30
+ - VERSION.yml
31
+ - lib/subdomain-fu.rb
32
+ - lib/subdomain_fu/routing_extensions.rb
33
+ - lib/subdomain_fu/url_rewriter.rb
34
+ - rails/init.rb
35
+ - spec/spec.opts
36
+ - spec/spec_helper.rb
37
+ - spec/subdomain_fu_spec.rb
38
+ - spec/url_rewriter_spec.rb
39
+ has_rdoc: true
40
+ homepage: http://github.com/mbleigh/subdomain-fu
41
+ post_install_message:
42
+ rdoc_options:
43
+ - --charset=UTF-8
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: "0"
51
+ version:
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: "0"
57
+ version:
58
+ requirements: []
59
+
60
+ rubyforge_project:
61
+ rubygems_version: 1.2.0
62
+ signing_key:
63
+ specification_version: 3
64
+ summary: SubdomainFu is a Rails plugin that provides subdomain routing and URL writing helpers.
65
+ test_files:
66
+ - spec/spec_helper.rb
67
+ - spec/subdomain_fu_spec.rb
68
+ - spec/url_rewriter_spec.rb