subdomainitis 0.9.22 → 0.9.30
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/subdomainitis.rb +57 -6
- metadata +3 -3
data/lib/subdomainitis.rb
CHANGED
@@ -28,7 +28,7 @@ module Subdomainitis
|
|
28
28
|
|
29
29
|
|
30
30
|
class SubdomainRouteSet < ActionDispatch::Routing::RouteSet
|
31
|
-
|
31
|
+
def initialize(parent_route_set, subdomain_key)
|
32
32
|
@parent_route_set, @subdomain_key = parent_route_set, subdomain_key
|
33
33
|
super *[]
|
34
34
|
end
|
@@ -45,7 +45,31 @@ module Subdomainitis
|
|
45
45
|
[IsSubdomain.new(parent_route_set)],
|
46
46
|
request_class
|
47
47
|
)
|
48
|
+
|
48
49
|
end
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
class MainDomainRouteSet < ActionDispatch::Routing::RouteSet
|
54
|
+
|
55
|
+
def initialize(parent_route_set)
|
56
|
+
@parent_route_set = parent_route_set
|
57
|
+
super *[]
|
58
|
+
end
|
59
|
+
attr_reader :parent_route_set
|
60
|
+
|
61
|
+
def add_route(app, conditions = {}, requirements = {}, defaults = {}, name = nil, anchor = true)
|
62
|
+
parent_route_set.add_maindomain_route name
|
63
|
+
parent_route_set.add_route wrap(app), conditions, requirements, defaults, name, anchor
|
64
|
+
end
|
65
|
+
def wrap(app)
|
66
|
+
ActionDispatch::Routing::Mapper::Constraints.new(
|
67
|
+
app,
|
68
|
+
[IsMaindomain.new(parent_route_set)],
|
69
|
+
request_class
|
70
|
+
)
|
71
|
+
end
|
72
|
+
|
49
73
|
end
|
50
74
|
|
51
75
|
def subdomain_as(subdomain_key, &block)
|
@@ -55,7 +79,9 @@ module Subdomainitis
|
|
55
79
|
end
|
56
80
|
|
57
81
|
def main_domain(&block)
|
58
|
-
|
82
|
+
@set.maindomain_routes ||= {}
|
83
|
+
maindomain_routeset = MainDomainRouteSet.new @set
|
84
|
+
maindomain_routeset.draw &block
|
59
85
|
end
|
60
86
|
|
61
87
|
def use_fake_subdomains!
|
@@ -92,8 +118,11 @@ module Subdomainitis
|
|
92
118
|
|
93
119
|
module RouteSetMethods
|
94
120
|
def url_for_with_subdomains(args)
|
95
|
-
|
121
|
+
route_name = args[:use_route]
|
122
|
+
if subdomain_key = subdomain_routes[route_name]
|
96
123
|
subdomain_url_for(subdomain_key, args.dup)
|
124
|
+
elsif maindomain_routes[route_name]
|
125
|
+
maindomain_url_for(args.dup)
|
97
126
|
else
|
98
127
|
url_for_without_subdomains args
|
99
128
|
end
|
@@ -111,20 +140,42 @@ module Subdomainitis
|
|
111
140
|
end)
|
112
141
|
end
|
113
142
|
|
143
|
+
def maindomain_url_for(args)
|
144
|
+
raise HostRequired.new if args[:only_path]
|
145
|
+
|
146
|
+
url_for_without_subdomains(if use_fake_subdomains
|
147
|
+
args
|
148
|
+
else
|
149
|
+
args.merge :host => main_domain_host(args[:host])
|
150
|
+
end)
|
151
|
+
end
|
152
|
+
|
114
153
|
def add_subdomain_route(name, subdomain_key)
|
115
154
|
subdomain_routes[name] = subdomain_key
|
116
155
|
end
|
117
156
|
|
157
|
+
def add_maindomain_route(name)
|
158
|
+
maindomain_routes[name] = true
|
159
|
+
end
|
160
|
+
|
118
161
|
def host_name(subdomain_parameter, host)
|
119
162
|
raise HostRequired.new unless host
|
120
|
-
|
163
|
+
|
121
164
|
subdomain_parameter = if subdomain_parameter.respond_to?(:to_param)
|
122
165
|
subdomain_parameter.to_param
|
123
166
|
else
|
124
167
|
subdomain_parameter
|
125
168
|
end
|
126
169
|
|
127
|
-
([subdomain_parameter] + host.split(".")[
|
170
|
+
([subdomain_parameter] + host.split(".")[subdomain_index..-1]).join(".")
|
171
|
+
end
|
172
|
+
|
173
|
+
def main_domain_host(host)
|
174
|
+
host.split(".")[subdomain_index..-1].join(".")
|
175
|
+
end
|
176
|
+
|
177
|
+
def subdomain_index
|
178
|
+
-1 - tld_length
|
128
179
|
end
|
129
180
|
|
130
181
|
end
|
@@ -133,7 +184,7 @@ module Subdomainitis
|
|
133
184
|
mapper.instance_variable_get(:@set).class_eval do
|
134
185
|
include RouteSetMethods
|
135
186
|
alias_method_chain :url_for, :subdomains
|
136
|
-
attr_accessor :subdomain_routes, :use_fake_subdomains, :tld_length
|
187
|
+
attr_accessor :subdomain_routes, :maindomain_routes, :use_fake_subdomains, :tld_length
|
137
188
|
end
|
138
189
|
|
139
190
|
delegate :tld_length=, :to => :@set
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 9
|
8
|
-
-
|
9
|
-
version: 0.9.
|
8
|
+
- 30
|
9
|
+
version: 0.9.30
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Stephen Judkins
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date:
|
17
|
+
date: 2011-02-02 00:00:00 -08:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|