mbleigh-subdomain-fu 0.0.1 → 0.0.2
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/README +50 -12
- data/lib/subdomain-fu.rb +26 -7
- data/lib/subdomain_fu/routing_extensions.rb +8 -3
- data/lib/subdomain_fu/url_rewriter.rb +2 -2
- data/rails/init.rb +0 -3
- data/spec/spec_helper.rb +18 -4
- data/spec/subdomain_fu_spec.rb +22 -0
- data/spec/url_rewriter_spec.rb +46 -1
- data/subdomain-fu.gemspec +2 -2
- metadata +3 -3
data/README
CHANGED
|
@@ -18,6 +18,36 @@ To use it as a GemPlugin, add it to your environment.rb:
|
|
|
18
18
|
|
|
19
19
|
config.gem 'mbleigh-subdomain-fu', :source => "http://gems.github.com", :lib => "subdomain-fu"
|
|
20
20
|
|
|
21
|
+
|
|
22
|
+
Examples
|
|
23
|
+
========
|
|
24
|
+
|
|
25
|
+
SubdomainFu works inside of Rails's URL Writing mechanisms to provide an easy and seamless
|
|
26
|
+
way to link and otherwise understand cross-subdomain routing. You can use the :subdomain
|
|
27
|
+
option both in named and non-named routes as well as in generated resources routes.
|
|
28
|
+
|
|
29
|
+
Let's say my domain is 'intridea.com'. Here are some examples of the use of the :subdomain
|
|
30
|
+
option:
|
|
31
|
+
|
|
32
|
+
url_for(:controller => "my_controller",
|
|
33
|
+
:action => "my_action",
|
|
34
|
+
:subdomain => "awesome") # => http://awesome.intridea.com/my_controller/my_action
|
|
35
|
+
|
|
36
|
+
Now let's say I'm at http://awesome.intridea.com/ and I want back to the root.
|
|
37
|
+
Specifying "false" will remove any current subdomain:
|
|
38
|
+
|
|
39
|
+
users_url(:subdomain => false) # => http://intridea.com/users
|
|
40
|
+
|
|
41
|
+
Note that this plugin does not honor the :only_path notion of routing when doing
|
|
42
|
+
so would go against the intent of the command. For example, if I were at http://intridea.com
|
|
43
|
+
again:
|
|
44
|
+
|
|
45
|
+
users_path(:subdomain => "fun") # => http://fun.intridea.com/users
|
|
46
|
+
users_path(:subdomain => false) # => /users
|
|
47
|
+
|
|
48
|
+
In this way you can rest assured that you will never misdirect your links to the
|
|
49
|
+
same subdomain when you meant to change it.
|
|
50
|
+
|
|
21
51
|
Configuration
|
|
22
52
|
=============
|
|
23
53
|
|
|
@@ -43,22 +73,30 @@ the usage of the root domain.
|
|
|
43
73
|
|
|
44
74
|
SubdomainFu.mirrors = %w(www site we) # Defaults to %w(www)
|
|
45
75
|
|
|
46
|
-
|
|
47
|
-
|
|
76
|
+
preferred_mirror
|
|
77
|
+
----------------
|
|
78
|
+
|
|
79
|
+
SubdomainFu also understands the notion of a 'preferred mirror', that is, if you
|
|
80
|
+
always want your links going to 'www.yourdomain.com' instead of 'yourdomain.com',
|
|
81
|
+
you can set the preferred mirror like so:
|
|
82
|
+
|
|
83
|
+
SubdomainFu.preferred_mirror = "www"
|
|
84
|
+
|
|
85
|
+
Now when you create a link to a false subdomain
|
|
48
86
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
call to use (or lose) a subdomain:
|
|
87
|
+
Known Issues / Future Work
|
|
88
|
+
==========================
|
|
52
89
|
|
|
53
|
-
|
|
54
|
-
|
|
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).
|
|
55
93
|
|
|
56
|
-
|
|
57
|
-
|
|
94
|
+
Resources
|
|
95
|
+
=========
|
|
58
96
|
|
|
59
|
-
*
|
|
60
|
-
*
|
|
61
|
-
*
|
|
97
|
+
* Acts As Community Project: http://actsascommunity.com/projects/subdomain-fu
|
|
98
|
+
* GitHub Repository: http://github.com/mbleigh/subdomain-fu
|
|
99
|
+
* Lighthouse: http://mbleigh.lighthouseapp.com/projects/13148-subdomain-fu
|
|
62
100
|
|
|
63
101
|
Copyright (c) 2008 Michael Bleigh (http://www.mbleigh.com/) and
|
|
64
102
|
Intridea, Inc. (http://www.intridea.com/). Released under the MIT license
|
data/lib/subdomain-fu.rb
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
require 'subdomain_fu/routing_extensions'
|
|
1
|
+
#require 'subdomain_fu/routing_extensions'
|
|
2
2
|
require 'subdomain_fu/url_rewriter'
|
|
3
3
|
|
|
4
4
|
module SubdomainFu
|
|
@@ -49,10 +49,14 @@ module SubdomainFu
|
|
|
49
49
|
|
|
50
50
|
# Rewrites the subdomain of the host unless they are equivalent (i.e. mirrors of each other)
|
|
51
51
|
def self.rewrite_host_for_subdomains(subdomain, host)
|
|
52
|
-
|
|
53
|
-
host
|
|
52
|
+
unless needs_rewrite?(subdomain, host)
|
|
53
|
+
if has_subdomain?(subdomain) || (subdomain_from(host) == SubdomainFu.preferred_mirror) || (!has_subdomain?(subdomain) && SubdomainFu.preferred_mirror == nil)
|
|
54
|
+
host
|
|
55
|
+
else
|
|
56
|
+
change_subdomain_of_host(SubdomainFu.preferred_mirror, host)
|
|
57
|
+
end
|
|
54
58
|
else
|
|
55
|
-
change_subdomain_of_host(subdomain, host)
|
|
59
|
+
change_subdomain_of_host(subdomain || SubdomainFu.preferred_mirror, host)
|
|
56
60
|
end
|
|
57
61
|
end
|
|
58
62
|
|
|
@@ -65,9 +69,24 @@ module SubdomainFu
|
|
|
65
69
|
|
|
66
70
|
# Is this subdomain equivalent to the subdomain found in this host string?
|
|
67
71
|
def self.same_subdomain?(subdomain, host)
|
|
68
|
-
|
|
72
|
+
subdomain = nil unless subdomain
|
|
73
|
+
(subdomain == SubdomainFu.subdomain_from(host)) ||
|
|
69
74
|
(!SubdomainFu.has_subdomain?(subdomain) && !SubdomainFu.has_subdomain?(SubdomainFu.subdomain_from(host)))
|
|
70
|
-
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def self.needs_rewrite?(subdomain, host)
|
|
78
|
+
subdomain = nil if subdomain.blank?
|
|
79
|
+
(!has_subdomain?(subdomain) && subdomain != SubdomainFu.preferred_mirror && SubdomainFu.preferred_mirror != nil) ||
|
|
80
|
+
!same_subdomain?(subdomain, host)
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def self.current_subdomain(request)
|
|
84
|
+
subdomain = request.subdomains(SubdomainFu.tld_size).join(".")
|
|
85
|
+
if has_subdomain?(subdomain)
|
|
86
|
+
subdomain
|
|
87
|
+
else
|
|
88
|
+
nil
|
|
89
|
+
end
|
|
71
90
|
end
|
|
72
91
|
|
|
73
92
|
module Controller
|
|
@@ -78,7 +97,7 @@ module SubdomainFu
|
|
|
78
97
|
protected
|
|
79
98
|
|
|
80
99
|
def current_subdomain
|
|
81
|
-
|
|
100
|
+
SubdomainFu.current_subdomain(request)
|
|
82
101
|
end
|
|
83
102
|
end
|
|
84
103
|
end
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
# Thanks to Jamis Buck for ideas on this stuff
|
|
2
2
|
# http://weblog.jamisbuck.org/2006/10/26/monkey-patching-rails-extending-routes-2
|
|
3
|
+
# This is not yet a working part of SubdomainFu.
|
|
3
4
|
|
|
4
5
|
module SubdomainFu
|
|
5
6
|
module RouteExtensions
|
|
@@ -9,15 +10,16 @@ module SubdomainFu
|
|
|
9
10
|
|
|
10
11
|
def recognition_conditions_with_subdomain
|
|
11
12
|
result = recognition_conditions_without_subdomain
|
|
12
|
-
result << "conditions[:subdomain] === env[:subdomain]" if conditions[:subdomain] && conditions[:subdomain] != true
|
|
13
|
+
result << "conditions[:subdomain] === env[:subdomain]" if conditions[:subdomain] && conditions[:subdomain] != true && conditions[:subdomain] != false
|
|
13
14
|
result << "SubdomainFu.has_subdomain?(env[:subdomain])" if conditions[:subdomain] == true
|
|
15
|
+
result << "!SubdomainFu.has_subdomain?(env[:subdomain])" if conditions[:subdomain] == false
|
|
14
16
|
result
|
|
15
17
|
end
|
|
16
18
|
end
|
|
17
19
|
|
|
18
20
|
module RouteSetExtensions
|
|
19
21
|
def self.included(base)
|
|
20
|
-
base.alias_method_chain :extract_request_environment, :subdomain
|
|
22
|
+
base.alias_method_chain :extract_request_environment, :subdomain
|
|
21
23
|
end
|
|
22
24
|
|
|
23
25
|
def extract_request_environment_with_subdomain(request)
|
|
@@ -25,4 +27,7 @@ module SubdomainFu
|
|
|
25
27
|
env.merge(:host => request.host, :domain => request.domain, :subdomain => SubdomainFu.subdomain_from(request.host))
|
|
26
28
|
end
|
|
27
29
|
end
|
|
28
|
-
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
ActionController::Routing::RouteSet.send :include, SubdomainFu::RouteSetExtensions
|
|
33
|
+
ActionController::Routing::Route.send :include, SubdomainFu::RouteExtensions
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
module ActionController
|
|
2
2
|
module UrlWriter
|
|
3
3
|
def url_for_with_subdomains(options)
|
|
4
|
-
|
|
4
|
+
unless SubdomainFu.needs_rewrite?(options[:subdomain], options[:host] || default_url_options[:host])
|
|
5
5
|
options.delete(:subdomain)
|
|
6
6
|
else
|
|
7
7
|
options[:only_path] = false
|
|
@@ -16,7 +16,7 @@ module ActionController
|
|
|
16
16
|
private
|
|
17
17
|
|
|
18
18
|
def rewrite_url_with_subdomains(options)
|
|
19
|
-
|
|
19
|
+
unless SubdomainFu.needs_rewrite?(options[:subdomain], (options[:host] || @request.host_with_port))
|
|
20
20
|
options.delete(:subdomain)
|
|
21
21
|
else
|
|
22
22
|
options[:only_path] = false
|
data/rails/init.rb
CHANGED
|
@@ -2,7 +2,4 @@ require 'subdomain-fu'
|
|
|
2
2
|
|
|
3
3
|
ActionController::Base.send :include, SubdomainFu::Controller
|
|
4
4
|
|
|
5
|
-
ActionController::Routing::RouteSet.send :include, SubdomainFu::RouteSetExtensions
|
|
6
|
-
ActionController::Routing::Route.send :include, SubdomainFu::RouteExtensions
|
|
7
|
-
|
|
8
5
|
RAILS_DEFAULT_LOGGER.info("** SubdomainFu: initialized properly")
|
data/spec/spec_helper.rb
CHANGED
|
@@ -9,11 +9,25 @@ plugin_spec_dir = File.dirname(__FILE__)
|
|
|
9
9
|
ActiveRecord::Base.logger = Logger.new(plugin_spec_dir + "/debug.log")
|
|
10
10
|
|
|
11
11
|
ActionController::Routing::Routes.draw do |map|
|
|
12
|
-
map.needs_subdomain '/needs_subdomain', :controller => "fu", :action => "awesome"
|
|
13
|
-
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
|
+
|
|
14
20
|
map.connect '/:controller/:action/:id'
|
|
15
21
|
end
|
|
16
22
|
|
|
23
|
+
class Paramed
|
|
24
|
+
def initialize(param)
|
|
25
|
+
@param = param
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def to_param
|
|
29
|
+
@param || "param"
|
|
30
|
+
end
|
|
31
|
+
end
|
|
17
32
|
|
|
18
|
-
include ActionController::UrlWriter
|
|
19
|
-
default_url_options[:host] = "testapp.com"
|
|
33
|
+
include ActionController::UrlWriter
|
data/spec/subdomain_fu_spec.rb
CHANGED
|
@@ -98,6 +98,28 @@ describe "SubdomainFu" do
|
|
|
98
98
|
end
|
|
99
99
|
end
|
|
100
100
|
|
|
101
|
+
describe "#current_subdomain" do
|
|
102
|
+
it "should return the current subdomain if there is one" do
|
|
103
|
+
request = mock("request", :subdomains => ["awesome"])
|
|
104
|
+
SubdomainFu.current_subdomain(request).should == "awesome"
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
it "should return nil if there's no subdomain" do
|
|
108
|
+
request = mock("request", :subdomains => [])
|
|
109
|
+
SubdomainFu.current_subdomain(request).should be_nil
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
it "should return nil if the current subdomain is a mirror" do
|
|
113
|
+
request = mock("request", :subdomains => ["www"])
|
|
114
|
+
SubdomainFu.current_subdomain(request).should be_nil
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
it "should return the whole thing (including a .) if there's multiple subdomains" do
|
|
118
|
+
request = mock("request", :subdomains => ["awesome","rad"])
|
|
119
|
+
SubdomainFu.current_subdomain(request).should == "awesome.rad"
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
|
|
101
123
|
describe "#same_subdomain?" do
|
|
102
124
|
it { SubdomainFu.same_subdomain?("www","www.localhost").should be_true }
|
|
103
125
|
it { SubdomainFu.same_subdomain?("www","localhost").should be_true }
|
data/spec/url_rewriter_spec.rb
CHANGED
|
@@ -3,6 +3,7 @@ require File.dirname(__FILE__) + '/spec_helper'
|
|
|
3
3
|
describe "SubdomainFu URL Writing" do
|
|
4
4
|
before do
|
|
5
5
|
SubdomainFu.tld_size = 1
|
|
6
|
+
default_url_options[:host] = "testapp.com"
|
|
6
7
|
end
|
|
7
8
|
|
|
8
9
|
describe "#url_for" do
|
|
@@ -43,8 +44,52 @@ describe "SubdomainFu URL Writing" do
|
|
|
43
44
|
end
|
|
44
45
|
end
|
|
45
46
|
|
|
47
|
+
describe "Resourced Routes" do
|
|
48
|
+
it "should be able to add a subdomain" do
|
|
49
|
+
foo_path(:id => "something", :subdomain => "awesome").should == "http://awesome.testapp.com/foos/something"
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
it "should be able to remove a subdomain" do
|
|
53
|
+
default_url_options[:host] = "awesome.testapp.com"
|
|
54
|
+
foo_path(:id => "something", :subdomain => false).should == "http://testapp.com/foos/something"
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
it "should work when passed in a paramable object" do
|
|
58
|
+
foo_path(Paramed.new("something"), :subdomain => "awesome").should == "http://awesome.testapp.com/foos/something"
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
it "should work on nested resource collections" do
|
|
62
|
+
foo_bars_path(Paramed.new("something"), :subdomain => "awesome").should == "http://awesome.testapp.com/foos/something/bars"
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
it "should work on nested resource members" do
|
|
66
|
+
foo_bar_path(Paramed.new("something"),Paramed.new("else"), :subdomain => "awesome").should == "http://awesome.testapp.com/foos/something/bars/else"
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
describe "Preferred Mirror" do
|
|
71
|
+
before do
|
|
72
|
+
SubdomainFu.preferred_mirror = "www"
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
it "should switch to the preferred mirror instead of no subdomain" do
|
|
76
|
+
default_url_options[:host] = "awesome.testapp.com"
|
|
77
|
+
needs_subdomain_url(:subdomain => false).should == "http://www.testapp.com/needs_subdomain"
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
it "should force a switch to no subdomain on a mirror if preferred_mirror is false" do
|
|
81
|
+
SubdomainFu.preferred_mirror = false
|
|
82
|
+
default_url_options[:host] = "www.testapp.com"
|
|
83
|
+
needs_subdomain_url(:subdomain => false).should == "http://testapp.com/needs_subdomain"
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
after do
|
|
87
|
+
SubdomainFu.preferred_mirror = nil
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
|
|
46
91
|
after do
|
|
47
92
|
SubdomainFu.tld_size = 0
|
|
48
|
-
default_url_options[:host] = "
|
|
93
|
+
default_url_options[:host] = "localhost"
|
|
49
94
|
end
|
|
50
95
|
end
|
data/subdomain-fu.gemspec
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
Gem::Specification.new do |s|
|
|
2
2
|
s.name = "subdomain-fu"
|
|
3
|
-
s.version = "0.0.
|
|
4
|
-
s.date = "2008-06-
|
|
3
|
+
s.version = "0.0.2"
|
|
4
|
+
s.date = "2008-06-25"
|
|
5
5
|
s.summary = "Provides a simple solution for route handling and linking between subdomains in a Rails application."
|
|
6
6
|
s.email = "michael@intridea.com"
|
|
7
7
|
s.homepage = "http://www.actsascommunity.com/projects/subdomain-fu"
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: mbleigh-subdomain-fu
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.0.
|
|
4
|
+
version: 0.0.2
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Michael Bleigh
|
|
@@ -9,7 +9,7 @@ autorequire:
|
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
11
|
|
|
12
|
-
date: 2008-06-
|
|
12
|
+
date: 2008-06-25 00:00:00 -07:00
|
|
13
13
|
default_executable:
|
|
14
14
|
dependencies: []
|
|
15
15
|
|
|
@@ -57,7 +57,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
57
57
|
requirements: []
|
|
58
58
|
|
|
59
59
|
rubyforge_project:
|
|
60
|
-
rubygems_version: 1.0
|
|
60
|
+
rubygems_version: 1.2.0
|
|
61
61
|
signing_key:
|
|
62
62
|
specification_version: 2
|
|
63
63
|
summary: Provides a simple solution for route handling and linking between subdomains in a Rails application.
|