mholling-subdomain_routes 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,4 +1,4 @@
1
1
  ---
2
- :patch: 0
2
+ :patch: 1
3
3
  :major: 0
4
4
  :minor: 3
@@ -9,7 +9,7 @@ module SubdomainRoutes
9
9
  ActionController::Routing::Routes.subdomain_procs
10
10
  end
11
11
 
12
- def rewrite_subdomain_options(options, host)
12
+ def rewrite_subdomain_options(options, host, port = nil, protocol = nil)
13
13
  if subdomains = options[:subdomains]
14
14
  old_subdomain, domain = split_host(host)
15
15
  options_subdomain = options.has_key?(:subdomain) ? options[:subdomain].to_param.to_s.downcase : nil
@@ -36,6 +36,8 @@ module SubdomainRoutes
36
36
  unless new_subdomain == old_subdomain
37
37
  options[:only_path] = false
38
38
  options[:host] = [ new_subdomain, domain ].reject(&:blank?).join('.')
39
+ options[:port] ||= port if port
40
+ options[:protocol] ||= protocol if protocol
39
41
  end
40
42
  options.delete(:subdomain)
41
43
  end
@@ -50,11 +52,13 @@ module SubdomainRoutes
50
52
  end
51
53
 
52
54
  def url_for_with_subdomains(options)
53
- host = options[:host] || default_url_options[:host]
55
+ host = options[:host] || default_url_options[:host]
56
+ port = options[:port] || default_url_options[:port]
57
+ protocol = options[:protocol] || default_url_options[:protocol]
54
58
  if options[:subdomains] && host.blank?
55
59
  raise HostNotSupplied, "Missing host to link to! Please provide :host parameter or set default_url_options[:host]"
56
60
  end
57
- rewrite_subdomain_options(options, host)
61
+ rewrite_subdomain_options(options, host, port, protocol)
58
62
  url_for_without_subdomains(options)
59
63
  end
60
64
  end
@@ -68,11 +72,13 @@ module SubdomainRoutes
68
72
  end
69
73
 
70
74
  def rewrite_with_subdomains(options)
71
- host = options[:host] || @request.host
75
+ host = options[:host] || @request.host
76
+ protocol = options[:protocol] || (@request.ssl? ? @request.protocol : nil)
77
+ port = options[:port] || (@request.port_string =~ /:(\d+)$/ ? $1.to_i : nil)
72
78
  if options[:subdomains] && host.blank?
73
79
  raise HostNotSupplied, "Missing host to link to!"
74
80
  end
75
- rewrite_subdomain_options(options, host)
81
+ rewrite_subdomain_options(options, host, port, protocol)
76
82
  rewrite_without_subdomains(options)
77
83
  end
78
84
  end
@@ -34,32 +34,37 @@ def recognize_path(request)
34
34
  ActionController::Routing::Routes.recognize_path(request.path, ActionController::Routing::Routes.extract_request_environment(request))
35
35
  end
36
36
 
37
- def in_controller_with_host(host, &block)
37
+ def in_controller_with_host(host, options = {}, &block)
38
38
  spec = self
39
39
  Class.new(ActionView::TestCase::TestController) do
40
40
  include Spec::Matchers
41
41
  end.new.instance_eval do
42
42
  request.host = host
43
+ request.instance_eval do
44
+ @env.merge! 'HTTPS' => 'on','SERVER_PORT' => 443 if options[:protocol] =~ /^https(:\/\/)?$/
45
+ @env.merge! 'SERVER_PORT'=> options[:port] if options[:port]
46
+ end
43
47
  copy_instance_variables_from(spec)
44
48
  instance_eval(&block)
45
49
  end
46
50
  end
47
51
 
48
- def in_object_with_host(host, &block)
52
+ def in_object_with_host(host, options = {}, &block)
49
53
  spec = self
50
54
  Class.new do
51
55
  include Spec::Matchers
52
56
  include ActionController::UrlWriter
53
57
  end.new.instance_eval do
54
58
  self.class.default_url_options = { :host => host }
59
+ self.class.default_url_options.merge! options.slice(:port, :protocol)
55
60
  copy_instance_variables_from(spec)
56
61
  instance_eval(&block)
57
62
  end
58
63
  end
59
64
 
60
- def with_host(host, &block)
61
- in_controller_with_host(host, &block)
62
- in_object_with_host(host, &block)
65
+ def with_host(host, options = {}, &block)
66
+ in_controller_with_host(host, options.dup, &block)
67
+ in_object_with_host(host, options.dup, &block)
63
68
  end
64
69
 
65
70
  ActiveRecord::Base.class_eval do
@@ -62,6 +62,16 @@ describe "URL writing" do
62
62
  url_for(@path_options).should == "http://#{host}/users"
63
63
  end
64
64
  end
65
+
66
+ [ [ "port", { :port => 8080 }, "http://#{host}:8080/users" ],
67
+ [ "protocol", { :protocol => "https" }, "https://#{host}/users" ] ].each do |variant, options, url|
68
+ it "should preserve the #{variant} when forcing the host" do
69
+ with_host "other.example.com", options do
70
+ users_path.should == url
71
+ url_for(@path_options).should == url
72
+ end
73
+ end
74
+ end
65
75
 
66
76
  context "and a subdomain different from the host subdomain is explicitly requested" do
67
77
  it "should change the host if the requested subdomain matches" do
@@ -137,6 +147,26 @@ describe "URL writing" do
137
147
  end
138
148
  end
139
149
  end
150
+
151
+ it "should preserve the port when changing the host" do
152
+ [ [ subdomains.first, hosts.first, hosts.last ],
153
+ [ subdomains.last, hosts.last, hosts.first ] ].each do |subdomain, new_host, old_host|
154
+ with_host(old_host, :port => 8080) do
155
+ items_path(:subdomain => subdomain).should == "http://#{new_host}:8080/items"
156
+ url_for(@path_options.merge(:subdomain => subdomain)).should == "http://#{new_host}:8080/items"
157
+ end
158
+ end
159
+ end
160
+
161
+ it "should preserve the protocol when changing the host" do
162
+ [ [ subdomains.first, hosts.first, hosts.last ],
163
+ [ subdomains.last, hosts.last, hosts.first ] ].each do |subdomain, new_host, old_host|
164
+ with_host(old_host, :protocol => "https") do
165
+ items_path(:subdomain => subdomain).should == "https://#{new_host}/items"
166
+ url_for(@path_options.merge(:subdomain => subdomain)).should == "https://#{new_host}/items"
167
+ end
168
+ end
169
+ end
140
170
 
141
171
  it "should raise a routing error if the requested subdomain doesn't match" do
142
172
  [ [ hosts.first, hosts.last ],
@@ -180,7 +210,7 @@ describe "URL writing" do
180
210
  url_for(@path_options).should == "/events"
181
211
  end
182
212
  end
183
-
213
+
184
214
  it "should force the host if the object has a different to_param from the current subdomain" do
185
215
  with_host "example.com" do
186
216
  city_events_url(@boston).should == "http://boston.example.com/events"
@@ -189,6 +219,18 @@ describe "URL writing" do
189
219
  url_for(@path_options).should == "http://boston.example.com/events"
190
220
  end
191
221
  end
222
+
223
+ [ [ "port", { :port => 8080 }, "http://boston.example.com:8080/events" ],
224
+ [ "protocol", { :protocol => "https" }, "https://boston.example.com/events" ] ].each do |variant, options, url|
225
+ it "should preserve the #{variant} when forcing the host" do
226
+ with_host "example.com", options do
227
+ city_events_url(@boston).should == url
228
+ city_events_path(@boston).should == url
229
+ url_for(@url_options).should == url
230
+ url_for(@path_options).should == url
231
+ end
232
+ end
233
+ end
192
234
 
193
235
  it "should raise an error if the object to_param is an invalid subdomain" do
194
236
  @newyork = City.new
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mholling-subdomain_routes
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matthew Hollingworth
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-08-25 00:00:00 -07:00
12
+ date: 2009-09-05 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -59,7 +59,6 @@ files:
59
59
  - spec/validations_spec.rb
60
60
  has_rdoc: false
61
61
  homepage: http://github.com/mholling/subdomain_routes
62
- licenses:
63
62
  post_install_message:
64
63
  rdoc_options:
65
64
  - --charset=UTF-8
@@ -80,7 +79,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
80
79
  requirements: []
81
80
 
82
81
  rubyforge_project:
83
- rubygems_version: 1.3.5
82
+ rubygems_version: 1.2.0
84
83
  signing_key:
85
84
  specification_version: 2
86
85
  summary: A Rails library for incorporating subdomains into route generation and recognition.