mbleigh-subdomain-fu 0.1.0 → 0.3.0

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 CHANGED
@@ -1,3 +1,13 @@
1
+ == 2009-08-26
2
+
3
+ * Fixed needs_rewrite? method
4
+ * Added is_mirror? method
5
+ * Added tests, and fixed failing tests
6
+
7
+ == 2009-08-24
8
+
9
+ * Merged forks that added lots of features, tests, and fixes
10
+
1
11
  == 2008-06-25
2
12
 
3
- * Removed stray puts
13
+ * Removed stray puts
@@ -1,13 +1,11 @@
1
- SubdomainFu
2
- ===========
1
+ = SubdomainFu
3
2
 
4
3
  SubdomainFu provides a modern implementation of subdomain handling in Rails.
5
4
  It takes aspects from account_location, request_routing, and other snippets
6
5
  found around the web and combines them to provide a single, simple solution
7
6
  for subdomain-based route and url management.
8
7
 
9
- Installation
10
- ============
8
+ == Installation
11
9
 
12
10
  SubdomainFu is available both as a traditional plugin and a GemPlugin. To
13
11
  install it as a traditional plugin (Rails 2.1 or later):
@@ -19,8 +17,7 @@ To use it as a GemPlugin, add it to your environment.rb:
19
17
  config.gem 'mbleigh-subdomain-fu', :source => "http://gems.github.com", :lib => "subdomain-fu"
20
18
 
21
19
 
22
- Examples
23
- ========
20
+ == Examples
24
21
 
25
22
  SubdomainFu works inside of Rails's URL Writing mechanisms to provide an easy and seamless
26
23
  way to link and otherwise understand cross-subdomain routing. You can use the :subdomain
@@ -48,14 +45,12 @@ users_path(:subdomain => false) # => /users
48
45
  In this way you can rest assured that you will never misdirect your links to the
49
46
  same subdomain when you meant to change it.
50
47
 
51
- Configuration
52
- =============
48
+ == Configuration
53
49
 
54
50
  You may need to configure SubdomainFu based on your development setup. The
55
51
  configuration required is:
56
52
 
57
- tld_size
58
- --------
53
+ === TLD Size
59
54
 
60
55
  A hash for each environment of the size of the top-level domain name.
61
56
  (something.com = 1, localhost = 0, etc.)
@@ -65,16 +60,14 @@ SubdomainFu.tld_sizes = {:development => 0,
65
60
  :test => 0,
66
61
  :production => 1} # set all at once (also the defaults)
67
62
 
68
- mirrors
69
- -------
63
+ === Mirrors
70
64
 
71
65
  Mirrors are the subdomains that are equivalent to no subdomain (i.e. they 'mirror')
72
66
  the usage of the root domain.
73
67
 
74
68
  SubdomainFu.mirrors = %w(www site we) # Defaults to %w(www)
75
69
 
76
- preferred_mirror
77
- ----------------
70
+ === Preferred Mirror
78
71
 
79
72
  SubdomainFu also understands the notion of a 'preferred mirror', that is, if you
80
73
  always want your links going to 'www.yourdomain.com' instead of 'yourdomain.com',
@@ -82,21 +75,22 @@ you can set the preferred mirror like so:
82
75
 
83
76
  SubdomainFu.preferred_mirror = "www"
84
77
 
85
- Now when you create a link to a false subdomain
78
+ Now when you create a link with subdomain => false in the options the subdomain
79
+ will default to the preferred mirror.
86
80
 
87
- Known Issues / Future Work
88
- ==========================
81
+ == Routing
89
82
 
90
- SubdomainFu will eventually integrate with Rails' routing internals to provide
91
- the ability to specify routes based on the condition of a specific subdomain or
92
- simply whether a subdomain is present (or a mirror).
83
+ SubdomainFu can also work within Rails' routing for subdomain-specific routes. For instance, if you only wanted your administrative tools available in the "admin" subdomain you could add this to your routes.rb file:
93
84
 
94
- Resources
95
- =========
85
+ map.with_options :conditions => {:subdomain => 'admin} do |admin|
86
+ admin.resources :posts
87
+ admin.resources :users
88
+ end
89
+
90
+ == Resources
96
91
 
97
- * Acts As Community Project: http://actsascommunity.com/projects/subdomain-fu
98
92
  * GitHub Repository: http://github.com/mbleigh/subdomain-fu
99
- * Lighthouse: http://mbleigh.lighthouseapp.com/projects/13148-subdomain-fu
93
+ * RDocs: http://rdoc.info/projects/mbleigh/subdomain-fu
100
94
 
101
95
  Copyright (c) 2008 Michael Bleigh (http://www.mbleigh.com/) and
102
- Intridea, Inc. (http://www.intridea.com/). Released under the MIT license
96
+ Intridea, Inc. (http://www.intridea.com/). Released under the MIT license
data/VERSION.yml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ :minor: 4
3
+ :patch: 0
4
+ :major: 0
data/lib/subdomain-fu.rb CHANGED
@@ -10,48 +10,62 @@ module SubdomainFu
10
10
  DEFAULT_TLD_SIZES = {:development => 0, :test => 0, :production => 1}
11
11
  mattr_accessor :tld_sizes
12
12
  @@tld_sizes = DEFAULT_TLD_SIZES.dup
13
-
13
+
14
14
  # Subdomains that are equivalent to going to the website with no subdomain at all.
15
15
  # Defaults to "www" as the only member.
16
+ DEFAULT_MIRRORS = %w(www)
16
17
  mattr_accessor :mirrors
17
- @@mirrors = %w(www)
18
-
18
+ @@mirrors = DEFAULT_MIRRORS.dup
19
+
19
20
  mattr_accessor :preferred_mirror
20
21
  @@preferred_mirror = nil
21
-
22
+
23
+ mattr_accessor :override_only_path
24
+ @@override_only_path = false
25
+
22
26
  # Returns the TLD Size of the current environment.
23
27
  def self.tld_size
24
28
  tld_sizes[RAILS_ENV.to_sym]
25
29
  end
26
-
30
+
27
31
  # Sets the TLD Size of the current environment
28
32
  def self.tld_size=(value)
29
33
  tld_sizes[RAILS_ENV.to_sym] = value
30
34
  end
31
-
32
- # Is the current subdomain either nil or a mirror?
35
+
36
+ # Is the current subdomain either nil or not a mirror?
33
37
  def self.has_subdomain?(subdomain)
34
38
  subdomain != false && !subdomain.blank? && !SubdomainFu.mirrors.include?(subdomain)
35
39
  end
36
40
 
41
+ def self.is_mirror?(subdomain)
42
+ subdomain != false && !subdomain.blank? && SubdomainFu.mirrors.include?(subdomain)
43
+ end
44
+
37
45
  # Is the subdomain a preferred mirror
38
46
  def self.preferred_mirror?(subdomain)
39
47
  subdomain == SubdomainFu.preferred_mirror || SubdomainFu.preferred_mirror.nil?
40
48
  end
41
-
49
+
42
50
  # Gets the subdomain from the host based on the TLD size
43
51
  def self.subdomain_from(host)
44
- return nil unless host
52
+ return nil if host.nil? || /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.match(host)
45
53
  parts = host.split('.')
46
54
  sub = parts[0..-(SubdomainFu.tld_size+2)].join(".")
47
55
  sub.blank? ? nil : sub
48
56
  end
49
-
57
+
58
+ # Gets only non-mirror subdomains from the host based on the TLD size
59
+ def self.non_mirror_subdomain_from(host)
60
+ sub = subdomain_from(host)
61
+ has_subdomain?(sub) ? sub : nil
62
+ end
63
+
50
64
  def self.host_without_subdomain(host)
51
65
  parts = host.split('.')
52
66
  parts[-(SubdomainFu.tld_size+1)..-1].join(".")
53
67
  end
54
-
68
+
55
69
  # Rewrites the subdomain of the host unless they are equivalent (i.e. mirrors of each other)
56
70
  def self.rewrite_host_for_subdomains(subdomain, host)
57
71
  if needs_rewrite?(subdomain, host)
@@ -65,28 +79,55 @@ module SubdomainFu
65
79
  end
66
80
  end
67
81
  end
68
-
82
+
69
83
  # Changes the subdomain of the host to whatever is passed in.
70
84
  def self.change_subdomain_of_host(subdomain, host)
71
85
  host = SubdomainFu.host_without_subdomain(host)
72
86
  host = "#{subdomain}.#{host}" if subdomain
73
87
  host
74
88
  end
75
-
89
+
76
90
  # Is this subdomain equivalent to the subdomain found in this host string?
77
91
  def self.same_subdomain?(subdomain, host)
78
92
  subdomain = nil unless subdomain
79
93
  (subdomain == subdomain_from(host)) ||
80
94
  (!has_subdomain?(subdomain) && !has_subdomain?(subdomain_from(host)))
81
95
  end
82
-
96
+
97
+ def self.override_only_path?
98
+ self.override_only_path
99
+ end
100
+
83
101
  def self.needs_rewrite?(subdomain, host)
84
- return false if subdomain.nil?
102
+ case subdomain
103
+ when nil
104
+ #rewrite when there is a preferred mirror set and there is no subdomain on the host
105
+ return true if self.preferred_mirror && subdomain_from(host).nil?
106
+ return false
107
+ when false
108
+ h = subdomain_from(host)
109
+ #if the host has a subdomain
110
+ if !h.nil?
111
+ #rewrite when there is a subdomain in the host, and it is not a preferred mirror
112
+ return true if !preferred_mirror?(h)
113
+ #rewrite when there is a preferred mirror set and the subdomain of the host is not a mirror
114
+ return true if self.preferred_mirror && !is_mirror?(h)
115
+ #no rewrite if host already has mirror subdomain
116
+ #it { SubdomainFu.needs_rewrite?(false,"www.localhost").should be_false }
117
+ return false if is_mirror?(h)
118
+ end
119
+ return self.crazy_rewrite_rule(subdomain, host)
120
+ else
121
+ return self.crazy_rewrite_rule(subdomain, host)
122
+ end
123
+ end
85
124
 
86
- (!has_subdomain?(subdomain) && preferred_mirror?(subdomain) && !preferred_mirror?(subdomain_from(host))) ||
125
+ #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!
126
+ def self.crazy_rewrite_rule(subdomain, host)
127
+ (!has_subdomain?(subdomain) && preferred_mirror?(subdomain) && !preferred_mirror?(subdomain_from(host))) ||
87
128
  !same_subdomain?(subdomain, host)
88
129
  end
89
-
130
+
90
131
  def self.current_subdomain(request)
91
132
  subdomain = request.subdomains(SubdomainFu.tld_size).join(".")
92
133
  if has_subdomain?(subdomain)
@@ -95,15 +136,15 @@ module SubdomainFu
95
136
  nil
96
137
  end
97
138
  end
98
-
139
+
99
140
  module Controller
100
141
  def self.included(controller)
101
142
  controller.helper_method(:current_subdomain)
102
143
  end
103
-
144
+
104
145
  protected
105
146
  def current_subdomain
106
147
  SubdomainFu.current_subdomain(request)
107
148
  end
108
149
  end
109
- end
150
+ end
@@ -10,16 +10,16 @@ module SubdomainFu
10
10
 
11
11
  def recognition_conditions_with_subdomain
12
12
  result = recognition_conditions_without_subdomain
13
- result << "conditions[:subdomain] === env[:subdomain].to_s" if conditions[:subdomain] && conditions[:subdomain] != true && conditions[:subdomain] != false
13
+ result << "conditions[:subdomain] === env[:subdomain]" if conditions[:subdomain] && conditions[:subdomain] != true && conditions[:subdomain] != false
14
14
  result << "SubdomainFu.has_subdomain?(env[:subdomain])" if conditions[:subdomain] == true
15
15
  result << "!SubdomainFu.has_subdomain?(env[:subdomain])" if conditions[:subdomain] == false
16
16
  result
17
17
  end
18
18
  end
19
-
19
+
20
20
  module RouteSetExtensions
21
21
  def self.included(base)
22
- base.alias_method_chain :extract_request_environment, :subdomain
22
+ base.alias_method_chain :extract_request_environment, :subdomain
23
23
  end
24
24
 
25
25
  def extract_request_environment_with_subdomain(request)
@@ -27,8 +27,30 @@ module SubdomainFu
27
27
  env.merge(:host => request.host, :domain => request.domain, :subdomain => SubdomainFu.subdomain_from(request.host))
28
28
  end
29
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
30
51
  end
31
52
 
53
+ ActionController::Routing::RouteSet::Mapper.send :include, SubdomainFu::MapperExtensions
32
54
  ActionController::Routing::RouteSet.send :include, SubdomainFu::RouteSetExtensions
33
55
  ActionController::Routing::Route.send :include, SubdomainFu::RouteExtensions
34
56
 
@@ -36,4 +58,4 @@ ActionController::Routing::Route.send :include, SubdomainFu::RouteExtensions
36
58
  # http://www.portallabs.com/blog/2008/12/02/fixing-subdomain_fu-with-named-routes-rails-22/
37
59
  if Rails::VERSION::MAJOR >= 2 and Rails::VERSION::MINOR >= 2
38
60
  ActionController::UrlRewriter::RESERVED_OPTIONS << :subdomain
39
- end
61
+ end
@@ -2,7 +2,7 @@ module ActionController
2
2
  module UrlWriter
3
3
  def url_for_with_subdomains(options)
4
4
  if SubdomainFu.needs_rewrite?(options[:subdomain], options[:host] || default_url_options[:host]) || options[:only_path] == false
5
- options[:only_path] = false
5
+ options[:only_path] = false if SubdomainFu.override_only_path?
6
6
  options[:host] = SubdomainFu.rewrite_host_for_subdomains(options.delete(:subdomain), options[:host] || default_url_options[:host])
7
7
  else
8
8
  options.delete(:subdomain)
@@ -11,14 +11,15 @@ module ActionController
11
11
  end
12
12
  alias_method_chain :url_for, :subdomains
13
13
  end
14
-
14
+
15
15
  class UrlRewriter #:nodoc:
16
16
  private
17
-
17
+
18
18
  def rewrite_url_with_subdomains(options)
19
- if SubdomainFu.needs_rewrite?(options[:subdomain], options[:host] || @request.host_with_port) || options[:only_path] == false
20
- options[:only_path] = false
19
+ if SubdomainFu.needs_rewrite?(options[:subdomain], (options[:host] || @request.host_with_port)) || options[:only_path] == false
20
+ options[:only_path] = false if SubdomainFu.override_only_path?
21
21
  options[:host] = SubdomainFu.rewrite_host_for_subdomains(options.delete(:subdomain), options[:host] || @request.host_with_port)
22
+ puts "options[:host]: #{options[:host].inspect}"
22
23
  else
23
24
  options.delete(:subdomain)
24
25
  end
@@ -26,7 +27,7 @@ module ActionController
26
27
  end
27
28
  alias_method_chain :rewrite_url, :subdomains
28
29
  end
29
-
30
+
30
31
  if Rails::VERSION::MAJOR >= 2 and Rails::VERSION::MINOR <= 1
31
32
  # hack for http://www.portallabs.com/blog/2008/10/22/fixing-subdomain_fu-with-named-routes/
32
33
  module Routing
@@ -44,4 +45,4 @@ module ActionController
44
45
  end
45
46
  end
46
47
  end
47
- end
48
+ end
data/rails/init.rb CHANGED
@@ -1,5 +1,8 @@
1
- require 'subdomain-fu'
1
+ # Add this path to ruby load path
2
+ $:.unshift "#{File.dirname(__FILE__)}/../lib"
3
+
4
+ require 'subdomain-fu' unless defined?(SubdomainFu)
2
5
 
3
6
  ActionController::Base.send :include, SubdomainFu::Controller
4
7
 
5
- RAILS_DEFAULT_LOGGER.info("** SubdomainFu: initialized properly")
8
+ RAILS_DEFAULT_LOGGER.info("** SubdomainFu: initialized properly")
data/spec/spec_helper.rb CHANGED
@@ -12,20 +12,20 @@ ActionController::Routing::Routes.draw do |map|
12
12
  map.needs_subdomain '/needs_subdomain', :controller => "fu", :action => "awesome"
13
13
  map.no_subdomain '/no_subdomain', :controller => "fu", :action => "lame"
14
14
  map.needs_awesome '/needs_awesome', :controller => "fu", :action => "lame"
15
-
15
+
16
16
  map.resources :foos do |fu|
17
17
  fu.resources :bars
18
18
  end
19
-
19
+
20
20
  map.connect '/', :controller => "site", :action => "home", :conditions => {:subdomain => false}
21
21
  map.connect '/', :controller => "app", :action => "home", :conditions => {:subdomain => true}
22
22
  map.connect '/', :controller => "mobile", :action => "home", :conditions => {:subdomain => "m"}
23
-
23
+
24
24
  map.connect '/subdomain_here', :controller => "app", :action => "success", :conditions => {:subdomain => true}
25
25
  map.connect '/no_subdomain_here', :controller => "site", :action => "success", :conditions => {:subdomain => false}
26
26
  map.connect '/m_subdomain_here', :controller => "mobile", :action => "success", :conditions => {:subdomain => "m"}
27
27
  map.connect '/numbers_only_here', :controller => "numbers", :action => "success", :conditions => {:subdomain => /[0-9]+/}
28
-
28
+
29
29
  map.connect '/:controller/:action/:id'
30
30
  end
31
31
 
@@ -33,10 +33,10 @@ class Paramed
33
33
  def initialize(param)
34
34
  @param = param
35
35
  end
36
-
36
+
37
37
  def to_param
38
38
  @param || "param"
39
39
  end
40
40
  end
41
41
 
42
- include ActionController::UrlWriter
42
+ include ActionController::UrlWriter
@@ -3,6 +3,7 @@ require File.dirname(__FILE__) + '/spec_helper'
3
3
  describe "SubdomainFu" do
4
4
  before do
5
5
  SubdomainFu.tld_sizes = SubdomainFu::DEFAULT_TLD_SIZES.dup
6
+ SubdomainFu.mirrors = SubdomainFu::DEFAULT_MIRRORS.dup
6
7
  SubdomainFu.preferred_mirror = nil
7
8
  end
8
9
 
@@ -10,42 +11,42 @@ describe "SubdomainFu" do
10
11
  before do
11
12
  SubdomainFu.tld_sizes = SubdomainFu::DEFAULT_TLD_SIZES.dup
12
13
  end
13
-
14
+
14
15
  it { SubdomainFu.tld_sizes.should be_kind_of(Hash) }
15
-
16
+
16
17
  it "should have default values for development, test, and production" do
17
18
  SubdomainFu.tld_sizes[:development].should == 0
18
19
  SubdomainFu.tld_sizes[:test].should == 0
19
20
  SubdomainFu.tld_sizes[:production].should == 1
20
21
  end
21
-
22
+
22
23
  it "#tld_size should be for the current environment" do
23
24
  SubdomainFu.tld_size.should == SubdomainFu.tld_sizes[RAILS_ENV.to_sym]
24
25
  end
25
-
26
+
26
27
  it "should be able to be set for the current environment" do
27
28
  SubdomainFu.tld_size = 5
28
29
  SubdomainFu.tld_size.should == 5
29
30
  SubdomainFu.tld_sizes[:test].should == 5
30
31
  end
31
32
  end
32
-
33
+
33
34
  describe "#has_subdomain?" do
34
35
  it "should be true for non-mirrored subdomains" do
35
36
  SubdomainFu.has_subdomain?("awesome").should be_true
36
37
  end
37
-
38
+
38
39
  it "should be false for mirrored subdomains" do
39
40
  SubdomainFu.has_subdomain?(SubdomainFu.mirrors.first).should be_false
40
41
  end
41
-
42
+
42
43
  it "shoud be false for a nil or blank subdomain" do
43
44
  SubdomainFu.has_subdomain?("").should be_false
44
45
  SubdomainFu.has_subdomain?(nil).should be_false
45
46
  SubdomainFu.has_subdomain?(false).should be_false
46
47
  end
47
48
  end
48
-
49
+
49
50
  describe "#subdomain_from" do
50
51
  it "should return the subdomain based on the TLD of the current environment" do
51
52
  SubdomainFu.subdomain_from("awesome.localhost").should == "awesome"
@@ -55,16 +56,16 @@ describe "SubdomainFu" do
55
56
  SubdomainFu.subdomain_from("awesome.localhost.com").should == "awesome"
56
57
  SubdomainFu.tld_size = 0
57
58
  end
58
-
59
+
59
60
  it "should join deep subdomains with a period" do
60
61
  SubdomainFu.subdomain_from("awesome.coolguy.localhost").should == "awesome.coolguy"
61
62
  end
62
-
63
+
63
64
  it "should return nil for no subdomain" do
64
65
  SubdomainFu.subdomain_from("localhost").should be_nil
65
66
  end
66
67
  end
67
-
68
+
68
69
  it "#host_without_subdomain should chop of the subdomain and return the rest" do
69
70
  SubdomainFu.host_without_subdomain("awesome.localhost:3000").should == "localhost:3000"
70
71
  SubdomainFu.host_without_subdomain("something.awful.localhost:3000").should == "localhost:3000"
@@ -86,15 +87,15 @@ describe "SubdomainFu" do
86
87
  it "should not change the same subdomain" do
87
88
  SubdomainFu.rewrite_host_for_subdomains("awesome","awesome.localhost").should == "awesome.localhost"
88
89
  end
89
-
90
+
90
91
  it "should not change an equivalent (mirrored) subdomain" do
91
92
  SubdomainFu.rewrite_host_for_subdomains("www","localhost").should == "localhost"
92
93
  end
93
-
94
+
94
95
  it "should change the subdomain if it's different" do
95
96
  SubdomainFu.rewrite_host_for_subdomains("cool","www.localhost").should == "cool.localhost"
96
97
  end
97
-
98
+
98
99
  it "should remove the subdomain if passed false when it's not a mirror" do
99
100
  SubdomainFu.rewrite_host_for_subdomains(false,"cool.localhost").should == "localhost"
100
101
  end
@@ -121,43 +122,43 @@ describe "SubdomainFu" do
121
122
  end
122
123
  end
123
124
  end
124
-
125
+
125
126
  describe "#change_subdomain_of_host" do
126
127
  it "should change it if passed a different one" do
127
128
  SubdomainFu.change_subdomain_of_host("awesome","cool.localhost").should == "awesome.localhost"
128
129
  end
129
-
130
+
130
131
  it "should remove it if passed nil" do
131
132
  SubdomainFu.change_subdomain_of_host(nil,"cool.localhost").should == "localhost"
132
133
  end
133
-
134
+
134
135
  it "should add it if there isn't one" do
135
136
  SubdomainFu.change_subdomain_of_host("awesome","localhost").should == "awesome.localhost"
136
137
  end
137
138
  end
138
-
139
+
139
140
  describe "#current_subdomain" do
140
141
  it "should return the current subdomain if there is one" do
141
142
  request = mock("request", :subdomains => ["awesome"])
142
143
  SubdomainFu.current_subdomain(request).should == "awesome"
143
144
  end
144
-
145
+
145
146
  it "should return nil if there's no subdomain" do
146
147
  request = mock("request", :subdomains => [])
147
148
  SubdomainFu.current_subdomain(request).should be_nil
148
149
  end
149
-
150
+
150
151
  it "should return nil if the current subdomain is a mirror" do
151
152
  request = mock("request", :subdomains => ["www"])
152
153
  SubdomainFu.current_subdomain(request).should be_nil
153
154
  end
154
-
155
+
155
156
  it "should return the whole thing (including a .) if there's multiple subdomains" do
156
157
  request = mock("request", :subdomains => ["awesome","rad"])
157
158
  SubdomainFu.current_subdomain(request).should == "awesome.rad"
158
159
  end
159
160
  end
160
-
161
+
161
162
  describe "#same_subdomain?" do
162
163
  it { SubdomainFu.same_subdomain?("www","www.localhost").should be_true }
163
164
  it { SubdomainFu.same_subdomain?("www","localhost").should be_true }
@@ -183,8 +184,34 @@ describe "SubdomainFu" do
183
184
  SubdomainFu.preferred_mirror = false
184
185
  end
185
186
 
187
+ it { SubdomainFu.needs_rewrite?("www","www.localhost").should be_false }
188
+ it { SubdomainFu.needs_rewrite?("www","localhost").should be_false }
189
+ it { SubdomainFu.needs_rewrite?("awesome","www.localhost").should be_true }
190
+ it { SubdomainFu.needs_rewrite?("cool","awesome.localhost").should be_true }
191
+ it { SubdomainFu.needs_rewrite?(nil,"www.localhost").should be_false }
192
+ it { SubdomainFu.needs_rewrite?(nil,"awesome.localhost").should be_false }
193
+ it { SubdomainFu.needs_rewrite?(false,"awesome.localhost").should be_true }
194
+ #Only one different from default set of tests
186
195
  it { SubdomainFu.needs_rewrite?(false,"www.localhost").should be_true }
196
+ it { SubdomainFu.needs_rewrite?("www","awesome.localhost").should be_true }
197
+ end
198
+
199
+ describe "when preferred_mirror is string" do
200
+ before do
201
+ SubdomainFu.preferred_mirror = "www"
202
+ end
203
+
204
+ it { SubdomainFu.needs_rewrite?("www","www.localhost").should be_false }
205
+ it { SubdomainFu.needs_rewrite?("awesome","www.localhost").should be_true }
206
+ # Following is different from default set of tests
207
+ it { SubdomainFu.needs_rewrite?("www","localhost").should be_true }
208
+ it { SubdomainFu.needs_rewrite?("cool","awesome.localhost").should be_true }
209
+ it { SubdomainFu.needs_rewrite?(nil,"www.localhost").should be_false }
187
210
  it { SubdomainFu.needs_rewrite?(nil,"awesome.localhost").should be_false }
211
+ it { SubdomainFu.needs_rewrite?(false,"awesome.localhost").should be_true }
212
+ it { SubdomainFu.needs_rewrite?(false,"www.localhost").should be_false }
213
+ it { SubdomainFu.needs_rewrite?("www","awesome.localhost").should be_true }
188
214
  end
215
+
189
216
  end
190
- end
217
+ end
@@ -3,138 +3,147 @@ require File.dirname(__FILE__) + '/spec_helper'
3
3
  describe "SubdomainFu URL Writing" do
4
4
  before do
5
5
  SubdomainFu.tld_size = 1
6
+ SubdomainFu.mirrors = SubdomainFu::DEFAULT_MIRRORS.dup
7
+ SubdomainFu.override_only_path = true
6
8
  SubdomainFu.preferred_mirror = nil
7
- default_url_options[:host] = "testapp.com"
9
+ default_url_options[:host] = "example.com"
8
10
  end
9
-
11
+
10
12
  describe "#url_for" do
11
13
  it "should be able to add a subdomain" do
12
- url_for(:controller => "something", :action => "other", :subdomain => "awesome").should == "http://awesome.testapp.com/something/other"
14
+ url_for(:controller => "something", :action => "other", :subdomain => "awesome").should == "http://awesome.example.com/something/other"
13
15
  end
14
-
16
+
15
17
  it "should be able to remove a subdomain" do
16
- url_for(:controller => "something", :action => "other", :subdomain => false, :host => "awesome.testapp.com").should == "http://testapp.com/something/other"
18
+ url_for(:controller => "something", :action => "other", :subdomain => false, :host => "awesome.example.com").should == "http://example.com/something/other"
17
19
  end
18
-
20
+
19
21
  it "should not change a mirrored subdomain" do
20
- url_for(:controller => "something", :action => "other", :subdomain => false, :host => "www.testapp.com").should == "http://www.testapp.com/something/other"
22
+ url_for(:controller => "something", :action => "other", :subdomain => false, :host => "www.example.com").should == "http://www.example.com/something/other"
23
+ end
24
+
25
+ it "should should not force the full url with :only_path if override_only_path is false (default)" do
26
+ SubdomainFu.override_only_path = false
27
+ url_for(:controller => "something", :action => "other", :subdomain => "awesome", :only_path => true).should == "/something/other"
21
28
  end
22
-
23
- it "should should force the full url, even with :only_path" do
24
- url_for(:controller => "something", :action => "other", :subdomain => "awesome", :only_path => true).should == "http://awesome.testapp.com/something/other"
29
+
30
+ it "should should force the full url, even with :only_path if override_only_path is true" do
31
+ SubdomainFu.override_only_path = true
32
+ url_for(:controller => "something", :action => "other", :subdomain => "awesome", :only_path => true).should == "http://awesome.example.com/something/other"
25
33
  end
26
34
  end
27
-
35
+
28
36
  describe "Standard Routes" do
29
37
  it "should be able to add a subdomain" do
30
- needs_subdomain_url(:subdomain => "awesome").should == "http://awesome.testapp.com/needs_subdomain"
38
+ needs_subdomain_url(:subdomain => "awesome").should == "http://awesome.example.com/needs_subdomain"
31
39
  end
32
40
 
33
41
  it "should be able to remove a subdomain" do
34
- default_url_options[:host] = "awesome.testapp.com"
35
- needs_subdomain_url(:subdomain => false).should == "http://testapp.com/needs_subdomain"
42
+ default_url_options[:host] = "awesome.example.com"
43
+ needs_subdomain_url(:subdomain => false).should == "http://example.com/needs_subdomain"
36
44
  end
37
45
 
38
46
  it "should not change a mirrored subdomain" do
39
- default_url_options[:host] = "www.testapp.com"
40
- needs_subdomain_url(:subdomain => false).should == "http://www.testapp.com/needs_subdomain"
47
+ default_url_options[:host] = "www.example.com"
48
+ needs_subdomain_url(:subdomain => false).should == "http://www.example.com/needs_subdomain"
41
49
  end
42
50
 
43
51
  it "should should force the full url, even with _path" do
44
52
  needs_subdomain_path(:subdomain => "awesome").should == needs_subdomain_url(:subdomain => "awesome")
45
53
  end
46
-
54
+
47
55
  it "should not force the full url if it's the same as the current subdomain" do
48
- default_url_options[:host] = "awesome.testapp.com"
56
+ default_url_options[:host] = "awesome.example.com"
49
57
  needs_subdomain_path(:subdomain => "awesome").should == "/needs_subdomain"
50
58
  end
51
59
 
52
60
  it "should force the full url if it's a different subdomain" do
53
- default_url_options[:host] = "awesome.testapp.com"
54
- needs_subdomain_path(:subdomain => "crazy").should == "http://crazy.testapp.com/needs_subdomain"
61
+ default_url_options[:host] = "awesome.example.com"
62
+ needs_subdomain_path(:subdomain => "crazy").should == "http://crazy.example.com/needs_subdomain"
55
63
  end
56
64
 
57
65
  it "should not force the full url if the current subdomain is nil and so is the target" do
58
66
  needs_subdomain_path(:subdomain => nil).should == "/needs_subdomain"
59
67
  end
60
-
68
+
61
69
  it "should not force the full url if no :subdomain option is given" do
62
70
  needs_subdomain_path.should == "/needs_subdomain"
63
- default_url_options[:host] = "awesome.testapp.com"
71
+ default_url_options[:host] = "awesome.example.com"
64
72
  needs_subdomain_path.should == "/needs_subdomain"
65
73
  end
66
74
  end
67
-
75
+
68
76
  describe "Resourced Routes" do
69
77
  it "should be able to add a subdomain" do
70
- foo_path(:id => "something", :subdomain => "awesome").should == "http://awesome.testapp.com/foos/something"
78
+ foo_path(:id => "something", :subdomain => "awesome").should == "http://awesome.example.com/foos/something"
71
79
  end
72
-
80
+
73
81
  it "should be able to remove a subdomain" do
74
- default_url_options[:host] = "awesome.testapp.com"
75
- foo_path(:id => "something", :subdomain => false).should == "http://testapp.com/foos/something"
82
+ default_url_options[:host] = "awesome.example.com"
83
+ foo_path(:id => "something", :subdomain => false).should == "http://example.com/foos/something"
76
84
  end
77
85
 
78
86
  it "should work when passed in a paramable object" do
79
- foo_path(Paramed.new("something"), :subdomain => "awesome").should == "http://awesome.testapp.com/foos/something"
87
+ foo_path(Paramed.new("something"), :subdomain => "awesome").should == "http://awesome.example.com/foos/something"
80
88
  end
81
89
 
82
90
  it "should work when passed in a paramable object" do
83
- foo_path(Paramed.new("something"), :subdomain => "awesome").should == "http://awesome.testapp.com/foos/something"
91
+ foo_path(Paramed.new("something"), :subdomain => "awesome").should == "http://awesome.example.com/foos/something"
84
92
  end
85
-
93
+
86
94
  it "should work when passed in a paramable object and no subdomain to a _path" do
87
- default_url_options[:host] = "awesome.testapp.com"
95
+ default_url_options[:host] = "awesome.example.com"
88
96
  foo_path(Paramed.new("something")).should == "/foos/something"
89
97
  end
90
98
 
91
99
  it "should work when passed in a paramable object and no subdomain to a _url" do
92
- default_url_options[:host] = "awesome.testapp.com"
93
- foo_url(Paramed.new("something")).should == "http://awesome.testapp.com/foos/something"
100
+ default_url_options[:host] = "awesome.example.com"
101
+ foo_url(Paramed.new("something")).should == "http://awesome.example.com/foos/something"
94
102
  end
95
103
 
96
104
  it "should work on nested resource collections" do
97
- foo_bars_path(Paramed.new("something"), :subdomain => "awesome").should == "http://awesome.testapp.com/foos/something/bars"
105
+ foo_bars_path(Paramed.new("something"), :subdomain => "awesome").should == "http://awesome.example.com/foos/something/bars"
98
106
  end
99
107
 
100
108
  it "should work on nested resource members" do
101
- foo_bar_path(Paramed.new("something"),Paramed.new("else"), :subdomain => "awesome").should == "http://awesome.testapp.com/foos/something/bars/else"
109
+ foo_bar_path(Paramed.new("something"),Paramed.new("else"), :subdomain => "awesome").should == "http://awesome.example.com/foos/something/bars/else"
102
110
  end
103
111
  end
104
-
112
+
105
113
  describe "Preferred Mirror" do
106
114
  before do
107
115
  SubdomainFu.preferred_mirror = "www"
116
+ SubdomainFu.override_only_path = true
108
117
  end
109
-
118
+
110
119
  it "should switch to the preferred mirror instead of no subdomain" do
111
- default_url_options[:host] = "awesome.testapp.com"
112
- needs_subdomain_url(:subdomain => false).should == "http://www.testapp.com/needs_subdomain"
120
+ default_url_options[:host] = "awesome.example.com"
121
+ needs_subdomain_url(:subdomain => false).should == "http://www.example.com/needs_subdomain"
113
122
  end
114
123
 
115
124
  it "should switch to the preferred mirror automatically" do
116
- default_url_options[:host] = "testapp.com"
117
- needs_subdomain_url.should == "http://www.testapp.com/needs_subdomain"
125
+ default_url_options[:host] = "example.com"
126
+ needs_subdomain_url.should == "http://www.example.com/needs_subdomain"
118
127
  end
119
128
 
120
129
  it "should work when passed in a paramable object and no subdomain to a _url" do
121
- default_url_options[:host] = "awesome.testapp.com"
122
- foo_url(Paramed.new("something")).should == "http://awesome.testapp.com/foos/something"
130
+ default_url_options[:host] = "awesome.example.com"
131
+ foo_url(Paramed.new("something")).should == "http://awesome.example.com/foos/something"
123
132
  end
124
133
 
125
134
  it "should force a switch to no subdomain on a mirror if preferred_mirror is false" do
126
135
  SubdomainFu.preferred_mirror = false
127
- default_url_options[:host] = "www.testapp.com"
128
- needs_subdomain_url(:subdomain => false).should == "http://testapp.com/needs_subdomain"
136
+ default_url_options[:host] = "www.example.com"
137
+ needs_subdomain_url(:subdomain => false).should == "http://example.com/needs_subdomain"
129
138
  end
130
139
 
131
140
  after do
132
141
  SubdomainFu.preferred_mirror = nil
133
142
  end
134
143
  end
135
-
144
+
136
145
  after do
137
146
  SubdomainFu.tld_size = 0
138
147
  default_url_options[:host] = "localhost"
139
148
  end
140
- end
149
+ end
metadata CHANGED
@@ -1,32 +1,33 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mbleigh-subdomain-fu
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Bleigh
8
+ - Peter Boling
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
12
 
12
- date: 2009-05-26 00:00:00 -07:00
13
+ date: 2009-08-24 00:00:00 -07:00
13
14
  default_executable:
14
15
  dependencies: []
15
16
 
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).
17
- email: michael@intridea.com
18
+ email: michael@intridea.com peter.boling@gmail.com
18
19
  executables: []
19
20
 
20
21
  extensions: []
21
22
 
22
- extra_rdoc_files:
23
- - README
23
+ extra_rdoc_files: []
24
+
24
25
  files:
25
26
  - CHANGELOG
26
27
  - MIT-LICENSE
27
- - README
28
+ - README.rdoc
28
29
  - Rakefile
29
- - VERSION
30
+ - VERSION.yml
30
31
  - lib/subdomain-fu.rb
31
32
  - lib/subdomain_fu/routing_extensions.rb
32
33
  - lib/subdomain_fu/url_rewriter.rb
@@ -35,8 +36,9 @@ files:
35
36
  - spec/spec_helper.rb
36
37
  - spec/subdomain_fu_spec.rb
37
38
  - spec/url_rewriter_spec.rb
38
- has_rdoc: false
39
+ has_rdoc: true
39
40
  homepage: http://github.com/mbleigh/subdomain-fu
41
+ licenses:
40
42
  post_install_message:
41
43
  rdoc_options:
42
44
  - --charset=UTF-8
@@ -57,7 +59,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
57
59
  requirements: []
58
60
 
59
61
  rubyforge_project:
60
- rubygems_version: 1.2.0
62
+ rubygems_version: 1.3.5
61
63
  signing_key:
62
64
  specification_version: 3
63
65
  summary: SubdomainFu is a Rails plugin that provides subdomain routing and URL writing helpers.
data/VERSION DELETED
@@ -1 +0,0 @@
1
- 0.1.0