mholling-subdomain_routes 0.1.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/LICENSE +20 -0
- data/README.textile +323 -0
- data/Rakefile +57 -0
- data/VERSION.yml +4 -0
- data/lib/subdomain_routes/config.rb +6 -0
- data/lib/subdomain_routes/mailer.rb +17 -0
- data/lib/subdomain_routes/mapper.rb +40 -0
- data/lib/subdomain_routes/proc_set.rb +33 -0
- data/lib/subdomain_routes/request.rb +12 -0
- data/lib/subdomain_routes/resources.rb +49 -0
- data/lib/subdomain_routes/routes.rb +66 -0
- data/lib/subdomain_routes/split_host.rb +35 -0
- data/lib/subdomain_routes/url_writer.rb +86 -0
- data/lib/subdomain_routes/validations.rb +28 -0
- data/lib/subdomain_routes.rb +14 -0
- data/spec/extraction_spec.rb +83 -0
- data/spec/mailer_spec.rb +15 -0
- data/spec/proc_set_spec.rb +61 -0
- data/spec/recognition_spec.rb +89 -0
- data/spec/resources_spec.rb +38 -0
- data/spec/routes_spec.rb +248 -0
- data/spec/spec_helper.rb +68 -0
- data/spec/url_writing_spec.rb +177 -0
- data/spec/validations_spec.rb +12 -0
- metadata +94 -0
data/spec/routes_spec.rb
ADDED
@@ -0,0 +1,248 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "subdomain routes" do
|
4
|
+
before(:each) do
|
5
|
+
ActionController::Routing::Routes.clear!
|
6
|
+
SubdomainRoutes::Config.stub!(:domain_length).and_return(2)
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should check the validity of each subdomain" do
|
10
|
+
SubdomainRoutes.should_receive(:valid_subdomain?).twice.and_return(true, true)
|
11
|
+
lambda { map_subdomain(:www, :www1) { } }.should_not raise_error
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should check the validity of each subdomain and raise an error if any are invalid" do
|
15
|
+
SubdomainRoutes.should_receive(:valid_subdomain?).twice.and_return(true, false)
|
16
|
+
lambda { map_subdomain(:www, :www!) { } }.should raise_error(ArgumentError)
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should check not the validity of a nil subdomain" do
|
20
|
+
SubdomainRoutes.should_not_receive(:valid_subdomain?)
|
21
|
+
map_subdomain(nil) { }
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should accept a nil subdomain" do
|
25
|
+
map_subdomain(nil) { |map| map.options[:subdomains].should == [ "" ] }
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should accept a blank subdomain" do
|
29
|
+
map_subdomain("") { |map| map.options[:subdomains].should == [ "" ] }
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should accept a single specified subdomain" do
|
33
|
+
map_subdomain(:admin) { |admin| admin.options[:subdomains].should == [ "admin" ] }
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should accept strings or symbols as subdomains" do
|
37
|
+
map_subdomain(:admin) { |admin| admin.options[:subdomains].should == [ "admin" ] }
|
38
|
+
map_subdomain("admin") { |admin| admin.options[:subdomains].should == [ "admin" ] }
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should accept multiple subdomains" do
|
42
|
+
map_subdomain(:admin, :support) { |map| map.options[:subdomains].should == [ "admin", "support" ] }
|
43
|
+
end
|
44
|
+
|
45
|
+
it "should downcase the subdomains" do
|
46
|
+
map_subdomain(:Admin, "SUPPORT") { |map| map.options[:subdomains].should == [ "admin", "support" ] }
|
47
|
+
end
|
48
|
+
|
49
|
+
it "should accept a :proc option as the subdomain" do
|
50
|
+
map_subdomain(:proc => :name) { |name| name.options[:subdomains].should == { :proc => :name } }
|
51
|
+
end
|
52
|
+
|
53
|
+
it "should raise ArgumentError if no subdomain is specified" do
|
54
|
+
lambda { map_subdomain }.should raise_error(ArgumentError)
|
55
|
+
end
|
56
|
+
|
57
|
+
it "should not include repeated subdomains in the options" do
|
58
|
+
map_subdomain(:admin, :support, :admin) { |map| map.options[:subdomains].should == [ "admin", "support" ] }
|
59
|
+
end
|
60
|
+
|
61
|
+
it "should be invoked by map.subdomains as well as map.subdomain" do
|
62
|
+
ActionController::Routing::Routes.draw do |map|
|
63
|
+
map.subdomains(:admin, :support) { |sub| sub.options[:subdomains].should == [ "admin", "support" ] }
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
[ [ :admin ], [ :support, :admin ] ].each do |subdomains|
|
68
|
+
context "mapping #{subdomains.size} subdomains" do
|
69
|
+
it "should set the first subdomain as a namespace" do
|
70
|
+
map_subdomain(*subdomains) { |map| map.options[:namespace].should == "#{subdomains.first}/" }
|
71
|
+
end
|
72
|
+
|
73
|
+
it "should prefix the first subdomain to named routes" do
|
74
|
+
map_subdomain(*subdomains) { |map| map.options[:name_prefix].should == "#{subdomains.first}_" }
|
75
|
+
end
|
76
|
+
|
77
|
+
it "should instead set a namespace to the name if specified" do
|
78
|
+
args = subdomains + [ :name => :something ]
|
79
|
+
map_subdomain(*args) { |map| map.options[:namespace].should == "something/" }
|
80
|
+
end
|
81
|
+
|
82
|
+
it "should instead prefix the name to named routes if specified" do
|
83
|
+
args = subdomains + [ :name => :something ]
|
84
|
+
map_subdomain(*args) { |map| map.options[:name_prefix].should == "something_" }
|
85
|
+
end
|
86
|
+
|
87
|
+
it "should not set a namespace if name is specified as nil" do
|
88
|
+
args = subdomains + [ :name => nil ]
|
89
|
+
map_subdomain(*args) { |map| map.options[:namespace].should be_nil }
|
90
|
+
end
|
91
|
+
|
92
|
+
it "should not set a named route prefix if name is specified as nil" do
|
93
|
+
args = subdomains + [ :name => nil ]
|
94
|
+
map_subdomain(*args) { |map| map.options[:name_prefix].should be_nil }
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
it "should strip bad characters from the namespace and name prefix" do
|
100
|
+
map_subdomain("just-do-it") { |map| map.options[:namespace].should == "just_do_it/" }
|
101
|
+
map_subdomain("just-do-it") { |map| map.options[:name_prefix].should == "just_do_it_" }
|
102
|
+
map_subdomain(nil, :name => "just-do-it") { |map| map.options[:namespace].should == "just_do_it/" }
|
103
|
+
map_subdomain(nil, :name => "just-do-it") { |map| map.options[:name_prefix].should == "just_do_it_" }
|
104
|
+
map_subdomain(nil, :name => "Just do it!") { |map| map.options[:namespace].should == "just_do_it/" }
|
105
|
+
map_subdomain(nil, :name => "Just do it!") { |map| map.options[:name_prefix].should == "just_do_it_" }
|
106
|
+
end
|
107
|
+
|
108
|
+
context "mapping the nil subdomain" do
|
109
|
+
it "should not set a namespace" do
|
110
|
+
[ nil, "" ].each do |none|
|
111
|
+
map_subdomain(none) { |map| map.options[:namespace].should be_nil }
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
it "should not set a named route prefix" do
|
116
|
+
[ nil, "" ].each do |none|
|
117
|
+
map_subdomain(none) { |map| map.options[:name_prefix].should be_nil }
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
context "mapping nil and other subdomains" do
|
123
|
+
it "should set the first non-nil subdomain as a namespace" do
|
124
|
+
[ nil, "" ].each do |none|
|
125
|
+
map_subdomain(none, :www) { |map| map.options[:namespace].should == "www/" }
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
it "should prefix the first non-nil subdomain to named routes" do
|
130
|
+
[ nil, "" ].each do |none|
|
131
|
+
map_subdomain(none, :www) { |map| map.options[:name_prefix].should == "www_" }
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
context "for a single specified subdomain" do
|
137
|
+
before(:each) do
|
138
|
+
map_subdomain(:admin) do |map|
|
139
|
+
map.resources :articles, :has_many => :comments
|
140
|
+
map.foobar "foobar", :controller => "foo", :action => "bar"
|
141
|
+
map.named_route "foobaz", "foobaz", :controller => "foo", :action => "baz"
|
142
|
+
map.connect "/:controller/:action/:id"
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
it "should add the specified subdomain to the route recognition conditions" do
|
147
|
+
ActionController::Routing::Routes.routes.each do |route|
|
148
|
+
route.conditions[:subdomains].should == [ "admin" ]
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
it "should add the subdomain to the route generation requirements" do
|
153
|
+
ActionController::Routing::Routes.routes.each do |route|
|
154
|
+
route.requirements[:subdomains].should == [ "admin" ]
|
155
|
+
end
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
159
|
+
context "for multiple specified subdomains" do
|
160
|
+
before(:each) do
|
161
|
+
map_subdomain(:support, :admin) do |map|
|
162
|
+
map.resources :articles, :has_many => :comments
|
163
|
+
map.foobar "foobar", :controller => "foo", :action => "bar"
|
164
|
+
map.named_route "foobaz", "foobaz", :controller => "foo", :action => "baz"
|
165
|
+
map.connect "/:controller/:action/:id"
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
it "should add the specified subdomain to the route recognition conditions" do
|
170
|
+
ActionController::Routing::Routes.routes.each do |route|
|
171
|
+
route.conditions[:subdomains].should == [ "support", "admin" ]
|
172
|
+
end
|
173
|
+
end
|
174
|
+
|
175
|
+
it "should not add a subdomain to the route generation requirements" do
|
176
|
+
ActionController::Routing::Routes.routes.each do |route|
|
177
|
+
route.requirements[:subdomains].should == [ "support", "admin" ]
|
178
|
+
end
|
179
|
+
end
|
180
|
+
end
|
181
|
+
|
182
|
+
context "for a :proc subdomain" do
|
183
|
+
it "should set the value a namespace" do
|
184
|
+
map_subdomain(:proc => :city) { |city| city.options[:namespace].should == "city/" }
|
185
|
+
end
|
186
|
+
|
187
|
+
it "should prefix the value to named routes" do
|
188
|
+
map_subdomain(:proc => :city) { |city| city.options[:name_prefix].should == "city_" }
|
189
|
+
end
|
190
|
+
|
191
|
+
it "should set a namespace to the name if specified" do
|
192
|
+
map_subdomain(:proc => :city, :name => :something) { |city| city.options[:namespace].should == "something/" }
|
193
|
+
end
|
194
|
+
|
195
|
+
it "should prefix the name to named routes if specified" do
|
196
|
+
map_subdomain(:proc => :city, :name => :something) { |city| city.options[:name_prefix].should == "something_" }
|
197
|
+
end
|
198
|
+
|
199
|
+
it "should add the specified proc to the route recognition conditions" do
|
200
|
+
map_subdomain(:proc => :city) { |city| city.resources :events }
|
201
|
+
ActionController::Routing::Routes.routes.each do |route|
|
202
|
+
route.conditions[:subdomains].should == { :proc => :city }
|
203
|
+
end
|
204
|
+
end
|
205
|
+
|
206
|
+
it "should add the specified proc to the route generation requirements" do
|
207
|
+
map_subdomain(:proc => :city) { |city| city.resources :events }
|
208
|
+
ActionController::Routing::Routes.routes.each do |route|
|
209
|
+
route.requirements[:subdomains].should == { :proc => :city }
|
210
|
+
end
|
211
|
+
end
|
212
|
+
end
|
213
|
+
end
|
214
|
+
|
215
|
+
describe "ActionController::Routing::Routes" do
|
216
|
+
before(:each) do
|
217
|
+
ActionController::Routing::Routes.clear!
|
218
|
+
end
|
219
|
+
|
220
|
+
it "should allow a subdomain recognition method to be added" do
|
221
|
+
ActionController::Routing::Routes.subdomain_procs.recognizes?(:city).should be_false
|
222
|
+
ActionController::Routing::Routes.recognize_subdomain(:city) { |city| city == "perth" }
|
223
|
+
ActionController::Routing::Routes.subdomain_procs.recognizes?(:city).should be_true
|
224
|
+
end
|
225
|
+
|
226
|
+
it "should allow subdomain recognition methods to be cleared" do
|
227
|
+
ActionController::Routing::Routes.recognize_subdomain(:city) { |city| city == "perth" }
|
228
|
+
ActionController::Routing::Routes.clear!
|
229
|
+
ActionController::Routing::Routes.subdomain_procs.recognizes?(:city).should be_false
|
230
|
+
end
|
231
|
+
|
232
|
+
it "should flush the cache when called" do
|
233
|
+
ActionController::Routing::Routes.subdomain_procs.should_receive(:flush!)
|
234
|
+
begin
|
235
|
+
ActionController::Routing::Routes.call(ActionController::TestRequest.new.env)
|
236
|
+
rescue ActionController::RoutingError # no routes defined so the call will raise a routing error
|
237
|
+
end
|
238
|
+
end
|
239
|
+
|
240
|
+
it "should not flush the cache when called if SubdomainRoutes::Config.manual_flush is set" do
|
241
|
+
SubdomainRoutes::Config.stub!(:manual_flush).and_return(true)
|
242
|
+
ActionController::Routing::Routes.subdomain_procs.should_not_receive(:flush!)
|
243
|
+
begin
|
244
|
+
ActionController::Routing::Routes.call(ActionController::TestRequest.new.env)
|
245
|
+
rescue ActionController::RoutingError # no routes defined so the call will raise a routing error
|
246
|
+
end
|
247
|
+
end
|
248
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
require 'spec'
|
2
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
3
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
4
|
+
require 'rubygems'
|
5
|
+
require 'active_support'
|
6
|
+
require 'action_controller'
|
7
|
+
require 'active_record' # only required for testing optional features, not required by the gem
|
8
|
+
require 'action_mailer' # only required for testing optional features, not required by the gem
|
9
|
+
|
10
|
+
require 'subdomain_routes'
|
11
|
+
|
12
|
+
Spec::Runner.configure do |config|
|
13
|
+
end
|
14
|
+
require 'action_controller/test_process'
|
15
|
+
require 'action_view/test_case'
|
16
|
+
|
17
|
+
ActiveSupport::OptionMerger.send(:define_method, :options) { @options }
|
18
|
+
|
19
|
+
def map_subdomain(*subdomains, &block)
|
20
|
+
ActionController::Routing::Routes.draw do |map|
|
21
|
+
map.subdomain(*subdomains, &block)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def recognize_path(request)
|
26
|
+
ActionController::Routing::Routes.recognize_path(request.path, ActionController::Routing::Routes.extract_request_environment(request))
|
27
|
+
end
|
28
|
+
|
29
|
+
def in_controller_with_host(host, &block)
|
30
|
+
variables = instance_variables.inject([]) do |array, name|
|
31
|
+
array << [ name, instance_variable_get(name) ]
|
32
|
+
end
|
33
|
+
Class.new(ActionView::TestCase::TestController) do
|
34
|
+
include Spec::Matchers
|
35
|
+
end.new.instance_eval do
|
36
|
+
request.host = host
|
37
|
+
variables.each { |name, value| instance_variable_set(name, value) }
|
38
|
+
instance_eval(&block)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def in_object_with_host(host, &block)
|
43
|
+
variables = instance_variables.inject([]) do |array, name|
|
44
|
+
array << [ name, instance_variable_get(name) ]
|
45
|
+
end
|
46
|
+
Class.new do
|
47
|
+
include Spec::Matchers
|
48
|
+
include ActionController::UrlWriter
|
49
|
+
end.new.instance_eval do
|
50
|
+
self.class.default_url_options = { :host => host }
|
51
|
+
variables.each { |name, value| instance_variable_set(name, value) }
|
52
|
+
instance_eval(&block)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def with_host(host, &block)
|
57
|
+
in_controller_with_host(host, &block)
|
58
|
+
in_object_with_host(host, &block)
|
59
|
+
end
|
60
|
+
|
61
|
+
ActiveRecord::Base.class_eval do
|
62
|
+
alias_method :save, :valid?
|
63
|
+
def self.columns() @columns ||= []; end
|
64
|
+
|
65
|
+
def self.column(name, sql_type = nil, default = nil, null = true)
|
66
|
+
columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type, null)
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,177 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "URL writing" do
|
4
|
+
before(:each) do
|
5
|
+
ActionController::Routing::Routes.clear!
|
6
|
+
SubdomainRoutes::Config.stub!(:domain_length).and_return(2)
|
7
|
+
end
|
8
|
+
|
9
|
+
{ "nil" => nil, "an IP address" => "207.192.69.152" }.each do |host_type, host|
|
10
|
+
context "when the host is #{host_type}" do
|
11
|
+
it "should raise an error when a subdomain route is requested" do
|
12
|
+
map_subdomain(:www) { |www| www.resources :users }
|
13
|
+
with_host(host) { lambda { www_users_path }.should raise_error(SubdomainRoutes::HostNotSupplied) }
|
14
|
+
end
|
15
|
+
|
16
|
+
context "and a non-subdomain route is requested" do
|
17
|
+
before(:each) do
|
18
|
+
ActionController::Routing::Routes.draw { |map| map.resources :users }
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should not raise an error when the route is a path" do
|
22
|
+
with_host(host) do
|
23
|
+
lambda { users_path }.should_not raise_error
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
[ [ "single", :admin, "admin.example.com" ],
|
31
|
+
[ "nil", nil, "example.com" ] ].each do |type, subdomain, host|
|
32
|
+
context "when a #{type} subdomain is specified" do
|
33
|
+
before(:each) do
|
34
|
+
map_subdomain(subdomain, :name => nil) { |map| map.resources :users }
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should not change the host for an URL if the host subdomain matches" do
|
38
|
+
with_host(host) { users_url.should == "http://#{host}/users" }
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should change the host for an URL if the host subdomain differs" do
|
42
|
+
with_host("other.example.com") { users_url.should == "http://#{host}/users" }
|
43
|
+
end
|
44
|
+
|
45
|
+
it "should not force the host for a path if the host subdomain matches" do
|
46
|
+
with_host(host) { users_path.should == "/users" }
|
47
|
+
end
|
48
|
+
|
49
|
+
it "should force the host for a path if the host subdomain differs" do
|
50
|
+
with_host("other.example.com") { users_path.should == "http://#{host}/users" }
|
51
|
+
end
|
52
|
+
|
53
|
+
context "and a subdomain different from the host subdomain is explicitly requested" do
|
54
|
+
it "should change the host if the requested subdomain matches" do
|
55
|
+
with_host("other.example.com") { users_path(:subdomain => subdomain).should == "http://#{host}/users" }
|
56
|
+
end
|
57
|
+
|
58
|
+
it "should raise a routing error if the requested subdomain doesn't match" do
|
59
|
+
with_host(host) do
|
60
|
+
lambda { users_path(:subdomain => :other) }.should raise_error(ActionController::RoutingError)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
context "and the current host's subdomain is explicitly requested" do
|
66
|
+
it "should not force the host for a path if the subdomain matches" do
|
67
|
+
with_host(host) { users_path(:subdomain => subdomain).should == "/users" }
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
[ [ "", [ :books, :dvds ], [ "books.example.com", "dvds.example.com" ] ],
|
74
|
+
[ " including nil", [ nil, :www ], [ "example.com", "www.example.com" ] ] ].each do |qualifier, subdomains, hosts|
|
75
|
+
context "when multiple subdomains#{qualifier} are specified" do
|
76
|
+
before(:each) do
|
77
|
+
args = subdomains + [ :name => nil ]
|
78
|
+
map_subdomain(*args) { |map| map.resources :items }
|
79
|
+
end
|
80
|
+
|
81
|
+
it "should not change the host for an URL if the host subdomain matches" do
|
82
|
+
hosts.each do |host|
|
83
|
+
with_host(host) { items_url.should == "http://#{host}/items" }
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
it "should not force the host for a path if the host subdomain matches" do
|
88
|
+
hosts.each do |host|
|
89
|
+
with_host(host) { items_path.should == "/items" }
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
it "should raise a routing error if the host subdomain doesn't match" do
|
94
|
+
with_host "other.example.com" do
|
95
|
+
lambda { item_url }.should raise_error(ActionController::RoutingError)
|
96
|
+
lambda { item_path }.should raise_error(ActionController::RoutingError)
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
context "and a subdomain different from the host subdomain is explicitly requested" do
|
101
|
+
it "should change the host if the requested subdomain matches" do
|
102
|
+
[ [ subdomains.first, hosts.first, hosts.last ],
|
103
|
+
[ subdomains.last, hosts.last, hosts.first ] ].each do |subdomain, new_host, old_host|
|
104
|
+
with_host(old_host) { items_path(:subdomain => subdomain).should == "http://#{new_host}/items" }
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
it "should raise a routing error if the requested subdomain doesn't match" do
|
109
|
+
[ [ hosts.first, hosts.last ],
|
110
|
+
[ hosts.last, hosts.first ] ].each do |new_host, old_host|
|
111
|
+
with_host(old_host) do
|
112
|
+
lambda { items_path(:subdomain => :other) }.should raise_error(ActionController::RoutingError)
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
it "should downcase a supplied subdomain" do
|
121
|
+
map_subdomain(:www1, :www2, :name => nil) { |map| map.resources :users }
|
122
|
+
[ [ :Www1, "www1" ], [ "Www2", "www2" ] ].each do |mixedcase, lowercase|
|
123
|
+
with_host("www.example.com") { users_url(:subdomain => mixedcase).should == "http://#{lowercase}.example.com/users" }
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
context "when a :proc subdomain is specified" do
|
128
|
+
before(:each) do
|
129
|
+
map_subdomain(:proc => :city) { |city| city.resources :events }
|
130
|
+
end
|
131
|
+
|
132
|
+
it "should raise a routing error without a recognize proc" do
|
133
|
+
with_host "boston.example.com" do
|
134
|
+
lambda { city_events_url }.should raise_error(ActionController::RoutingError)
|
135
|
+
lambda { city_events_path }.should raise_error(ActionController::RoutingError)
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
context "and a recognize proc is defined" do
|
140
|
+
before(:each) do
|
141
|
+
ActionController::Routing::Routes.recognize_subdomain(:city) { |city| } # this block will be stubbed
|
142
|
+
end
|
143
|
+
|
144
|
+
it "should not change the host if the recognize proc returns true" do
|
145
|
+
with_host "boston.example.com" do
|
146
|
+
ActionController::Routing::Routes.subdomain_procs.should_receive(:recognize).twice.with(:city, "boston").and_return(true)
|
147
|
+
city_events_url.should == "http://boston.example.com/events"
|
148
|
+
city_events_path.should == "/events"
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
it "should raise a routing error if the recognize proc returns false" do
|
153
|
+
with_host "www.example.com" do
|
154
|
+
ActionController::Routing::Routes.subdomain_procs.should_receive(:recognize).twice.with(:city, "www").and_return(false)
|
155
|
+
lambda { city_events_url }.should raise_error(ActionController::RoutingError)
|
156
|
+
lambda { city_events_path }.should raise_error(ActionController::RoutingError)
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
160
|
+
it "should force the host if the recognize proc returns false but a matching subdomain is supplied" do
|
161
|
+
with_host "www.example.com" do
|
162
|
+
ActionController::Routing::Routes.subdomain_procs.should_receive(:recognize).twice.with(:city, "boston").and_return(true)
|
163
|
+
city_events_url(:subdomain => :boston).should == "http://boston.example.com/events"
|
164
|
+
city_events_path(:subdomain => :boston).should == "http://boston.example.com/events"
|
165
|
+
end
|
166
|
+
end
|
167
|
+
|
168
|
+
it "should raise a routing error if the recognize proc returns false and a non-matching subdomain is supplied" do
|
169
|
+
with_host "www.example.com" do
|
170
|
+
ActionController::Routing::Routes.subdomain_procs.should_receive(:recognize).twice.with(:city, "hobart").and_return(false)
|
171
|
+
lambda { city_events_url(:subdomain => :hobart) }.should raise_error(ActionController::RoutingError)
|
172
|
+
lambda { city_events_path(:subdomain => :hobart) }.should raise_error(ActionController::RoutingError)
|
173
|
+
end
|
174
|
+
end
|
175
|
+
end
|
176
|
+
end
|
177
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
describe "ActiveRecord::Base" do
|
2
|
+
it "should have validates_subdomain_format_of which runs SubdomainRoutes.valid_subdomain? against the attributes" do
|
3
|
+
class User < ActiveRecord::Base
|
4
|
+
attr_accessor :subdomain
|
5
|
+
User.validates_subdomain_format_of :subdomain
|
6
|
+
end
|
7
|
+
SubdomainRoutes.should_receive(:valid_subdomain?).with("mholling").and_return(true)
|
8
|
+
User.new(:subdomain => "mholling").valid?.should be_true
|
9
|
+
SubdomainRoutes.should_receive(:valid_subdomain?).with("mholling").and_return(nil)
|
10
|
+
User.new(:subdomain => "mholling").valid?.should be_false
|
11
|
+
end
|
12
|
+
end
|
metadata
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mholling-subdomain_routes
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Matthew Hollingworth
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-05-30 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: actionpack
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 2.2.1
|
24
|
+
version:
|
25
|
+
description: SubdomainRoutes add subdomain conditions to the Rails routing system. Routes may be restricted to one or many specified subdomains. An URL will be recognised only if the host subdomain matches the subdomain specified in the route. Route generation is also enhanced, so that the subdomain of a generated URL (or path) will be changed if the requested route has a different subdomain to that of the current request. Dynamic subdomain routes can also be defined.
|
26
|
+
email: mdholling@gmail.com
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files:
|
32
|
+
- LICENSE
|
33
|
+
- README.textile
|
34
|
+
files:
|
35
|
+
- LICENSE
|
36
|
+
- README.textile
|
37
|
+
- Rakefile
|
38
|
+
- VERSION.yml
|
39
|
+
- lib/subdomain_routes.rb
|
40
|
+
- lib/subdomain_routes/config.rb
|
41
|
+
- lib/subdomain_routes/mailer.rb
|
42
|
+
- lib/subdomain_routes/mapper.rb
|
43
|
+
- lib/subdomain_routes/proc_set.rb
|
44
|
+
- lib/subdomain_routes/request.rb
|
45
|
+
- lib/subdomain_routes/resources.rb
|
46
|
+
- lib/subdomain_routes/routes.rb
|
47
|
+
- lib/subdomain_routes/split_host.rb
|
48
|
+
- lib/subdomain_routes/url_writer.rb
|
49
|
+
- lib/subdomain_routes/validations.rb
|
50
|
+
- spec/extraction_spec.rb
|
51
|
+
- spec/mailer_spec.rb
|
52
|
+
- spec/proc_set_spec.rb
|
53
|
+
- spec/recognition_spec.rb
|
54
|
+
- spec/resources_spec.rb
|
55
|
+
- spec/routes_spec.rb
|
56
|
+
- spec/spec_helper.rb
|
57
|
+
- spec/url_writing_spec.rb
|
58
|
+
- spec/validations_spec.rb
|
59
|
+
has_rdoc: false
|
60
|
+
homepage: http://github.com/mholling/subdomain_routes
|
61
|
+
post_install_message:
|
62
|
+
rdoc_options:
|
63
|
+
- --charset=UTF-8
|
64
|
+
require_paths:
|
65
|
+
- lib
|
66
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: "0"
|
71
|
+
version:
|
72
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: "0"
|
77
|
+
version:
|
78
|
+
requirements: []
|
79
|
+
|
80
|
+
rubyforge_project:
|
81
|
+
rubygems_version: 1.2.0
|
82
|
+
signing_key:
|
83
|
+
specification_version: 2
|
84
|
+
summary: A Rails library for incorporating subdomains into route generation and recognition.
|
85
|
+
test_files:
|
86
|
+
- spec/extraction_spec.rb
|
87
|
+
- spec/mailer_spec.rb
|
88
|
+
- spec/proc_set_spec.rb
|
89
|
+
- spec/recognition_spec.rb
|
90
|
+
- spec/resources_spec.rb
|
91
|
+
- spec/routes_spec.rb
|
92
|
+
- spec/spec_helper.rb
|
93
|
+
- spec/url_writing_spec.rb
|
94
|
+
- spec/validations_spec.rb
|