mbleigh-subdomain-fu 0.0.2 → 0.0.3

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/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
@@ -74,8 +74,9 @@ module SubdomainFu
74
74
  (!SubdomainFu.has_subdomain?(subdomain) && !SubdomainFu.has_subdomain?(SubdomainFu.subdomain_from(host)))
75
75
  end
76
76
 
77
- def self.needs_rewrite?(subdomain, host)
78
- subdomain = nil if subdomain.blank?
77
+ def self.needs_rewrite?(subdomain, host)
78
+ return false if subdomain.nil?
79
+ subdomain = nil if subdomain.blank?
79
80
  (!has_subdomain?(subdomain) && subdomain != SubdomainFu.preferred_mirror && SubdomainFu.preferred_mirror != nil) ||
80
81
  !same_subdomain?(subdomain, host)
81
82
  end
@@ -30,4 +30,5 @@ module SubdomainFu
30
30
  end
31
31
 
32
32
  ActionController::Routing::RouteSet.send :include, SubdomainFu::RouteSetExtensions
33
- ActionController::Routing::Route.send :include, SubdomainFu::RouteExtensions
33
+ ActionController::Routing::Route.send :include, SubdomainFu::RouteExtensions
34
+ ActionController::UrlRewriter::RESERVED_OPTIONS << :subdomain
@@ -26,4 +26,20 @@ module ActionController
26
26
  end
27
27
  alias_method_chain :rewrite_url, :subdomains
28
28
  end
29
+
30
+ # hack for http://www.portallabs.com/blog/?p=8
31
+ module Routing
32
+ module Optimisation
33
+ class PositionalArgumentsWithAdditionalParams
34
+ def guard_condition_with_subdomains
35
+ # don't allow optimisation if a subdomain is present - fixes a problem
36
+ # with the subdomain appearing in the query instead of being rewritten
37
+ # see http://mbleigh.lighthouseapp.com/projects/13148/tickets/8-improper-generated-urls-with-named-routes-for-a-singular-resource
38
+ guard_condition_without_subdomains + " && !args.last.has_key?(:subdomain)"
39
+ end
40
+
41
+ alias_method_chain :guard_condition, :subdomains
42
+ end
43
+ end
44
+ end
29
45
  end
data/spec/spec_helper.rb CHANGED
@@ -17,6 +17,15 @@ ActionController::Routing::Routes.draw do |map|
17
17
  fu.resources :bars
18
18
  end
19
19
 
20
+ map.connect '/', :controller => "site", :action => "home", :conditions => {:subdomain => false}
21
+ map.connect '/', :controller => "app", :action => "home", :conditions => {:subdomain => true}
22
+ map.connect '/', :controller => "mobile", :action => "home", :conditions => {:subdomain => "m"}
23
+
24
+ map.connect '/subdomain_here', :controller => "app", :action => "success", :conditions => {:subdomain => true}
25
+ map.connect '/no_subdomain_here', :controller => "site", :action => "success", :conditions => {:subdomain => false}
26
+ map.connect '/m_subdomain_here', :controller => "mobile", :action => "success", :conditions => {:subdomain => "m"}
27
+ map.connect '/numbers_only_here', :controller => "numbers", :action => "success", :conditions => {:subdomain => /[0-9]+/}
28
+
20
29
  map.connect '/:controller/:action/:id'
21
30
  end
22
31
 
@@ -43,6 +43,8 @@ describe "SubdomainFu" do
43
43
  describe "#subdomain_from" do
44
44
  it "should return the subdomain based on the TLD of the current environment" do
45
45
  SubdomainFu.subdomain_from("awesome.localhost").should == "awesome"
46
+ SubdomainFu.tld_size = 2
47
+ SubdomainFu.subdomain_from("awesome.localhost.co.uk").should == "awesome"
46
48
  SubdomainFu.tld_size = 1
47
49
  SubdomainFu.subdomain_from("awesome.localhost.com").should == "awesome"
48
50
  SubdomainFu.tld_size = 0
@@ -75,12 +77,12 @@ describe "SubdomainFu" do
75
77
  SubdomainFu.rewrite_host_for_subdomains("cool","www.localhost").should == "cool.localhost"
76
78
  end
77
79
 
78
- it "should remove the subdomain if passed nil when it's not a mirror" do
79
- SubdomainFu.rewrite_host_for_subdomains(nil,"cool.localhost").should == "localhost"
80
+ it "should remove the subdomain if passed false when it's not a mirror" do
81
+ SubdomainFu.rewrite_host_for_subdomains(false,"cool.localhost").should == "localhost"
80
82
  end
81
83
 
82
- it "should not remove the subdomain if passed nil when it is a mirror" do
83
- SubdomainFu.rewrite_host_for_subdomains(nil,"www.localhost").should == "www.localhost"
84
+ it "should not remove the subdomain if passed false when it is a mirror" do
85
+ SubdomainFu.rewrite_host_for_subdomains(false,"www.localhost").should == "www.localhost"
84
86
  end
85
87
  end
86
88
 
@@ -42,6 +42,21 @@ describe "SubdomainFu URL Writing" do
42
42
  it "should should force the full url, even with _path" do
43
43
  needs_subdomain_path(:subdomain => "awesome").should == needs_subdomain_url(:subdomain => "awesome")
44
44
  end
45
+
46
+ it "should not force the full url if it's the same as the current subdomain" do
47
+ default_url_options[:host] = "awesome.testapp.com"
48
+ needs_subdomain_path(:subdomain => "awesome").should == "/needs_subdomain"
49
+ end
50
+
51
+ it "should not force the full url if the current subdomain is nil and so is the target" do
52
+ needs_subdomain_path(:subdomain => nil).should == "/needs_subdomain"
53
+ end
54
+
55
+ it "should not force the full url if no :subdomain option is given" do
56
+ needs_subdomain_path.should == "/needs_subdomain"
57
+ default_url_options[:host] = "awesome.testapp.com"
58
+ needs_subdomain_path.should == "/needs_subdomain"
59
+ end
45
60
  end
46
61
 
47
62
  describe "Resourced Routes" do
data/subdomain-fu.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "subdomain-fu"
3
- s.version = "0.0.2"
3
+ s.version = "0.0.3"
4
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"
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.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Bleigh