subdomainitis 0.9.0 → 0.9.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,8 +1,10 @@
1
- # Subdomainitis: easily and transparently use subdomains as parameters in a Rails 3 application
1
+ # Subdomainitis: transparently use subdomains as parameters in Rails 3
2
2
 
3
3
  ### Installation and usage
4
4
  To install, add `gem "subdomainitis"` to your `Gemfile`. Then modify your routes.rb file; see the example below for usage.
5
5
 
6
+ require 'subdomainitis'
7
+
6
8
  MyProject::Application.routes.draw do
7
9
  extend Subdomainitis
8
10
 
@@ -23,11 +25,11 @@ In the example above, the routes for `spams` will work regardless of the presenc
23
25
 
24
26
  Accessing `bars` routes will only work when a subdomain is provided. Additionally, the specific subdomain is passed into the controller as a path parameter, as specified by the first argument to `subdomain_as`. For example, `http://subdomain.mycompany.com/bars` resolves to `{:controller => 'bars', :action => 'index', :account => 'subdomain'}`.
25
27
 
26
- URL generation should work transparently as well; make sure you're using url instead of path generation (ie, `foos_url` instead of `foos_path`). Subdomainitis tries to fail fast by raising exceptions when a working URL cannot be generated.
28
+ URL generation should work transparently as well; make sure you're using url instead of path generation (ie, `foos_url` instead of `foos_path`). Subdomainitis tries to fail fast by raising exceptions when a functional URL cannot be generated.
27
29
 
28
- Call `use_fake_subdomains!` to use the `_subdomain` GET parameter instead of an actual subdomain. This is useful for development where wildcard subdomains aren't possible, or for testing environments like Cucumber that don't support subdomains. This support has been tested with both recognition and generation.
30
+ Call `use_fake_subdomains!` to use the `_subdomain` GET parameter instead of an actual subdomain. This is useful for development where wildcard subdomains aren't possible, or for testing environments like Cucumber that don't support subdomains. Enabling this mode should be completely transparent if you're using URL generators.
29
31
 
30
- No changes are necessary in the controllers.
32
+ No changes to controllers are necessary.
31
33
 
32
34
  ### Issues
33
35
  Though this library seems to work fine for me, there are probably bugs and untested corner cases. Currently only named routes have been verified to work.
@@ -39,7 +39,7 @@ module Subdomainitis
39
39
  end
40
40
 
41
41
  def generate(args)
42
- url_helpers.url_for(args.merge(:host => 'test.host'))
42
+ url_helpers.url_for({:host => 'test.host'}.merge(args))
43
43
  end
44
44
 
45
45
  def url_helpers
data/lib/subdomainitis.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  module Subdomainitis
2
2
 
3
3
  SUBDOMAIN_KEY = "_subdomain"
4
+ DEFAULT_TLD_LENGTH = 1
4
5
 
5
6
  class IsSubdomain
6
7
  def initialize(route_set)
@@ -8,11 +9,13 @@ module Subdomainitis
8
9
  end
9
10
  attr_reader :route_set
10
11
 
12
+ delegate :use_fake_subdomains, :tld_length, :to => :route_set
13
+
11
14
  def matches?(request)
12
- if route_set.use_fake_subdomains
15
+ if use_fake_subdomains
13
16
  request.GET.has_key?(SUBDOMAIN_KEY)
14
17
  else
15
- request.subdomain.present?
18
+ request.subdomain(tld_length).present?
16
19
  end
17
20
  end
18
21
  end
@@ -65,6 +68,7 @@ module Subdomainitis
65
68
  end
66
69
 
67
70
  attr_reader :route_set, :subdomain_key, :dispatcher
71
+ delegate :use_fake_subdomains, :tld_length, :to => :route_set
68
72
 
69
73
  PATH_PARAMETER_KEY = 'action_dispatch.request.path_parameters'
70
74
 
@@ -78,10 +82,10 @@ module Subdomainitis
78
82
  end
79
83
 
80
84
  def subdomain_from(request)
81
- if route_set.use_fake_subdomains
85
+ if use_fake_subdomains
82
86
  request.GET[SUBDOMAIN_KEY]
83
87
  else
84
- request.subdomain
88
+ request.subdomain(tld_length)
85
89
  end
86
90
  end
87
91
  end
@@ -113,19 +117,25 @@ module Subdomainitis
113
117
 
114
118
  def host_name(subdomain_parameter, host)
115
119
  raise HostRequired.new unless host
116
- ([subdomain_parameter] + host.split(".")[-2..-1]).join(".")
120
+ index = -1 - tld_length
121
+ ([subdomain_parameter] + host.split(".")[index..-1]).join(".")
117
122
  end
118
123
 
119
124
  end
120
125
 
121
- def self.extended(router)
122
- router.instance_variable_get(:@set).class_eval do
126
+ def self.extended(mapper)
127
+ mapper.instance_variable_get(:@set).class_eval do
123
128
  include RouteSetMethods
124
129
  alias_method_chain :url_for, :subdomains
125
- attr_accessor :subdomain_routes, :use_fake_subdomains
130
+ attr_accessor :subdomain_routes, :use_fake_subdomains, :tld_length
126
131
  end
132
+
133
+ delegate :tld_length=, :to => :@set
134
+
135
+ mapper.tld_length = DEFAULT_TLD_LENGTH
127
136
  end
128
137
 
138
+
129
139
  class HostRequired < Exception
130
140
  def initialize
131
141
  super("A hostname must be specified to generate this URL since it depends on a subdomain")
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 9
8
- - 0
9
- version: 0.9.0
8
+ - 1
9
+ version: 0.9.1
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-21 00:00:00 -07:00
17
+ date: 2010-10-25 00:00:00 -07:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency