subdomainitis 0.9.2 → 0.9.3

Sign up to get free protection for your applications and to get access to all the features.
data/lib/subdomainitis.rb CHANGED
@@ -28,7 +28,7 @@ module Subdomainitis
28
28
 
29
29
 
30
30
  class SubdomainRouteSet < ActionDispatch::Routing::RouteSet
31
- def initialize(parent_route_set, subdomain_key)
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
- constraints IsMaindomain.new(@set), &block
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!
@@ -76,7 +102,7 @@ module Subdomainitis
76
102
  request = ActionDispatch::Request.new env
77
103
 
78
104
  path_parameters = env[PATH_PARAMETER_KEY].merge(subdomain_key => subdomain_from(request))
79
- env = env.merge(PATH_PARAMETER_KEY => path_parameters)
105
+ env[PATH_PARAMETER_KEY] = path_parameters
80
106
 
81
107
  dispatcher.call(env)
82
108
  end
@@ -92,8 +118,11 @@ module Subdomainitis
92
118
 
93
119
  module RouteSetMethods
94
120
  def url_for_with_subdomains(args)
95
- if subdomain_key = subdomain_routes[args[:use_route]]
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
- index = -1 - tld_length
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(".")[index..-1]).join(".")
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
@@ -17,7 +17,7 @@ module Subdomainitis
17
17
  define :resolve_to do |expected|
18
18
  match do |path|
19
19
  called = false
20
- uri = URI.parse(path)
20
+
21
21
  controller_class = "#{expected[:controller].camelize}Controller".constantize
22
22
  controller_instance = mock(controller_class)
23
23
  controller_class.should_receive(:action).any_number_of_times.with(expected[:action]).and_return(controller_instance)
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 9
8
- - 2
9
- version: 0.9.2
8
+ - 3
9
+ version: 0.9.3
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: 2010-10-25 00:00:00 -07:00
17
+ date: 2011-02-01 00:00:00 -08:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -36,13 +36,13 @@ dependencies:
36
36
  requirement: &id002 !ruby/object:Gem::Requirement
37
37
  none: false
38
38
  requirements:
39
- - - "="
39
+ - - ">="
40
40
  - !ruby/object:Gem::Version
41
41
  segments:
42
42
  - 3
43
43
  - 0
44
- - 1
45
- version: 3.0.1
44
+ - 3
45
+ version: 3.0.3
46
46
  type: :runtime
47
47
  version_requirements: *id002
48
48
  description: subdomainitis provides easy, simple support for using wildcard subdomains as controller parameters in Rails 3