subdomain-fu 0.5.4 → 1.0.0.beta2

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -5,17 +5,19 @@ It takes aspects from account_location, request_routing, and other snippets
5
5
  found around the web and combines them to provide a single, simple solution
6
6
  for subdomain-based route and url management.
7
7
 
8
+ **Note**: SubdomainFu has been rewritten to be compatible with Rails 3. If
9
+ you are looking to use it on Rails 2.x, please install version v0.5.x instead.
10
+
8
11
  == Installation
9
12
 
10
13
  SubdomainFu is available both as a traditional plugin and a GemPlugin. To
11
- install it as a traditional plugin (Rails 2.1 or later):
14
+ install it as a traditional plugin:
12
15
 
13
- script/plugin install git://github.com/mbleigh/subdomain-fu.git
16
+ script/plugin install git://github.com/intridea/subdomain-fu.git
14
17
 
15
- To use it as a gem, add it to your config/environment.rb:
16
-
17
- config.gem 'subdomain-fu'
18
+ To use it as a gem, add it to your Gemfile:
18
19
 
20
+ gem 'subdomain-fu'
19
21
 
20
22
  == Examples
21
23
 
@@ -51,7 +53,7 @@ You have access to current_subdomain and current_domain methods.
51
53
 
52
54
  [current_subdomain] returns all subdomains. For the URL http://awesome.website.stuff.example.com, it will return "awesome.website.stuff"
53
55
 
54
- [current_domain] returns all subdomains except for the subdomain, including the TLD. For the URL http://awesome.website.stuff.example.com, it will return "website.stuff.example.com"
56
+ [current_domain] returns the domain excluding for the subdomain, including the TLD. For the URL http://awesome.website.stuff.example.com, it will return "website.stuff.example.com"
55
57
 
56
58
  If what you really want is the entire domain, then use <tt>request.domain</tt> in
57
59
  your controllers. The purpose of current_domain is to only strip off the first
@@ -59,52 +61,53 @@ subdomain, if any, and return what's left.
59
61
 
60
62
  == Configuration
61
63
 
62
- You may need to configure SubdomainFu based on your development setup. The
63
- configuration required is:
64
+ You may need to configure SubdomainFu based on your development setup. To configure
65
+ SubdomainFu simply call a block in an initializer like so:
66
+
67
+ SubdomainFu.configure do |config|
68
+ config.option_name = value
69
+ end
64
70
 
65
- === TLD Size
71
+ Some available options are enumerated below.
72
+
73
+ === tld_size
66
74
 
67
75
  A hash for each environment of the size of the top-level domain name.
68
76
  (something.com = 1, localhost = 0, etc.)
69
77
 
70
- SubdomainFu.tld_size = 1 # sets for current environment
71
- SubdomainFu.tld_sizes = {:development => 0,
78
+ config.tld_size = 1 # sets for current environment
79
+ config.tld_sizes = {:development => 0,
72
80
  :test => 0,
73
81
  :production => 1} # set all at once (also the defaults)
74
82
 
75
- === Mirrors
83
+ === mirrors
76
84
 
77
85
  Mirrors are the subdomains that are equivalent to no subdomain (i.e. they 'mirror')
78
86
  the usage of the root domain.
79
87
 
80
- SubdomainFu.mirrors = %w(www site we) # Defaults to %w(www)
88
+ config.mirrors = %w(www site we) # Defaults to %w(www)
81
89
 
82
- === Preferred Mirror
90
+ === preferred_mirror
83
91
 
84
92
  SubdomainFu also understands the notion of a 'preferred mirror', that is, if you
85
93
  always want your links going to 'www.yourdomain.com' instead of 'yourdomain.com',
86
94
  you can set the preferred mirror like so:
87
95
 
88
- SubdomainFu.preferred_mirror = "www"
96
+ config.preferred_mirror = "www"
89
97
 
90
98
  Now when you create a link with <tt>:subdomain => false</tt> in the options the subdomain
91
99
  will default to the preferred mirror.
92
100
 
93
- == Routing
94
-
95
- SubdomainFu can also work within Rails' routing for subdomain-specific routes. For instance, if you only wanted your administrative tools available in the "admin" subdomain you could add this to your <tt>config/routes.rb</tt> file:
101
+ == Routing (Deprecated)
96
102
 
97
- map.with_options :conditions => {:subdomain => 'admin'} do |admin|
98
- admin.resources :posts
99
- admin.resources :users
100
- end
101
-
102
- In addition to specifying a string, you could also specify <tt>false</tt> to require no subdomain (this includes mirrors that you've set up such as www) or a regular expression to match a range of subdomains.
103
+ Subdomain constraint routing has been removed from the scope of this plugin as Rails 3
104
+ provides the functionality by default. For more info, see this blog post:
105
+ http://yehudakatz.com/2009/12/26/the-rails-3-router-rack-it-up/
103
106
 
104
107
  == Resources
105
108
 
106
- * GitHub Repository: http://github.com/mbleigh/subdomain-fu
107
- * RDocs: http://rdoc.info/projects/mbleigh/subdomain-fu
109
+ * GitHub Repository: http://github.com/intridea/subdomain-fu
110
+ * RDocs: http://rdoc.info/projects/intridea/subdomain-fu
108
111
 
109
- Copyright (c) 2008 Michael Bleigh (http://www.mbleigh.com/) and
112
+ Copyright (c) 2008-2010 Michael Bleigh (http://www.mbleigh.com/) and
110
113
  Intridea, Inc. (http://www.intridea.com/). Released under the MIT license
data/VERSION.yml CHANGED
@@ -1,5 +1,5 @@
1
1
  ---
2
- :major: 0
3
- :build:
4
- :minor: 5
5
- :patch: 4
2
+ :major: 1
3
+ :build: beta2
4
+ :minor: 0
5
+ :patch: 0
data/lib/subdomain-fu.rb CHANGED
@@ -1,57 +1,77 @@
1
- require 'subdomain_fu/routing_extensions'
2
1
  require 'subdomain_fu/url_rewriter'
3
2
 
4
3
  module SubdomainFu
5
- # The length of the period-split top-level domain for each environment.
6
- # For example, "localhost" has a tld_size of zero, and "something.co.uk"
7
- # has a tld_size of two.
8
- #
9
- # To set a tld size for a given environment, just call SubdomainFu.tld_sizes[:environment] = value
10
- DEFAULT_TLD_SIZES = {:development => 0, :test => 0, :production => 1}
11
- mattr_accessor :tld_sizes
12
- @@tld_sizes = DEFAULT_TLD_SIZES.dup
13
-
14
- # Subdomains that are equivalent to going to the website with no subdomain at all.
15
- # Defaults to "www" as the only member.
16
- DEFAULT_MIRRORS = %w(www)
17
- mattr_accessor :mirrors
18
- @@mirrors = DEFAULT_MIRRORS.dup
19
-
20
- mattr_accessor :preferred_mirror
21
- @@preferred_mirror = nil
22
-
23
- mattr_accessor :override_only_path
24
- @@override_only_path = false
25
-
26
- # Returns the TLD Size of the current environment.
27
- def self.tld_size
28
- tld_sizes[RAILS_ENV.to_sym]
4
+ class << self
5
+ attr_accessor :config
6
+
7
+ def config
8
+ self.config = Configuration.new unless @config
9
+ @config
10
+ end
29
11
  end
30
12
 
31
- # Sets the TLD Size of the current environment
32
- def self.tld_size=(value)
33
- tld_sizes[RAILS_ENV.to_sym] = value
13
+ # The configurable options of Subdomain Fu. Use like so:
14
+ #
15
+ # SubdomainFu.configure do |config|
16
+ # config.tld_size = 2
17
+ # config.preferred_mirror = 'www'
18
+ # end
19
+ #
20
+ # Available configurations are:
21
+ #
22
+ # <tt>tld_size</tt>: :: The size of the top-level domain. For example, 'localhost' is 0, 'example.com' is 1, and 'example.co.uk' is 2.
23
+ # <tt>mirrors</tt>: :: An array of subdomains that should be equivalent to no subdomain. Defaults to <tt>['www']</tt>.
24
+ # <tt>preferred_mirror</tt>: The preferred mirror subdomain to which to rewrite URLs. No subdomain is used by default.
25
+ # <tt>override_only_path</tt>: :: If <tt>true</tt>, changing the subdomain will emit a full URL in url_for options, even if it wouldn't have otherwise.
26
+ def self.configure
27
+ self.config ||= Configuration.new
28
+ yield(self.config)
29
+ end
30
+
31
+ class Configuration
32
+ attr_accessor :tld_sizes, :mirrors, :preferred_mirror, :override_only_path
33
+
34
+ @@defaults = {
35
+ :tld_sizes => {:development => 1, :test => 1, :production => 1},
36
+ :mirrors => %w(www),
37
+ :preferred_mirror => nil,
38
+ :override_only_path => false
39
+ }
40
+
41
+ def initialize
42
+ @@defaults.each_pair do |k, v|
43
+ self.send("#{k}=", v)
44
+ end
45
+ end
46
+
47
+ def tld_size=(size)
48
+ tld_sizes[Rails.env.to_sym] = size
49
+ end
50
+
51
+ def tld_size
52
+ tld_sizes[Rails.env.to_sym]
53
+ end
34
54
  end
35
55
 
36
56
  # Is the current subdomain either nil or not a mirror?
37
57
  def self.has_subdomain?(subdomain)
38
- subdomain != false && !subdomain.blank? && !SubdomainFu.mirrors.include?(subdomain)
58
+ subdomain != false && !subdomain.blank? && !SubdomainFu.config.mirrors.include?(subdomain)
39
59
  end
40
60
 
41
61
  def self.is_mirror?(subdomain)
42
- subdomain != false && !subdomain.blank? && SubdomainFu.mirrors.include?(subdomain)
62
+ subdomain != false && !subdomain.blank? && SubdomainFu.config.mirrors.include?(subdomain)
43
63
  end
44
64
 
45
65
  # Is the subdomain a preferred mirror
46
66
  def self.preferred_mirror?(subdomain)
47
- subdomain == SubdomainFu.preferred_mirror || SubdomainFu.preferred_mirror.nil?
67
+ subdomain == SubdomainFu.config.preferred_mirror || SubdomainFu.config.preferred_mirror.nil?
48
68
  end
49
69
 
50
70
  # Gets the subdomain from the host based on the TLD size
51
71
  def self.subdomain_from(host)
52
72
  return nil if host.nil? || /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.match(host)
53
73
  parts = host.split('.')
54
- sub = parts[0..-(SubdomainFu.tld_size+2)].join(".")
74
+ sub = parts[0..-(SubdomainFu.config.tld_size+2)].join(".")
55
75
  sub.blank? ? nil : sub
56
76
  end
57
77
 
@@ -63,19 +83,19 @@ module SubdomainFu
63
83
 
64
84
  def self.host_without_subdomain(host)
65
85
  parts = host.split('.')
66
- parts[-(SubdomainFu.tld_size+1)..-1].join(".")
86
+ parts[-(SubdomainFu.config.tld_size+1)..-1].join(".")
67
87
  end
68
88
 
69
89
  # Rewrites the subdomain of the host unless they are equivalent (i.e. mirrors of each other)
70
90
  def self.rewrite_host_for_subdomains(subdomain, host)
71
91
  if needs_rewrite?(subdomain, host)
72
- change_subdomain_of_host(subdomain || SubdomainFu.preferred_mirror, host)
92
+ change_subdomain_of_host(subdomain || SubdomainFu.config.preferred_mirror, host)
73
93
  else
74
94
  if has_subdomain?(subdomain) || preferred_mirror?(subdomain_from(host)) ||
75
95
  (subdomain.nil? && has_subdomain?(subdomain_from(host)))
76
96
  host
77
97
  else
78
- change_subdomain_of_host(SubdomainFu.preferred_mirror, host)
98
+ change_subdomain_of_host(SubdomainFu.config.preferred_mirror, host)
79
99
  end
80
100
  end
81
101
  end
@@ -95,14 +115,14 @@ module SubdomainFu
95
115
  end
96
116
 
97
117
  def self.override_only_path?
98
- self.override_only_path
118
+ config.override_only_path
99
119
  end
100
120
 
101
121
  def self.needs_rewrite?(subdomain, host)
102
122
  case subdomain
103
123
  when nil
104
124
  #rewrite when there is a preferred mirror set and there is no subdomain on the host
105
- return true if self.preferred_mirror && subdomain_from(host).nil?
125
+ return true if config.preferred_mirror && subdomain_from(host).nil?
106
126
  return false
107
127
  when false
108
128
  h = subdomain_from(host)
@@ -111,7 +131,7 @@ module SubdomainFu
111
131
  #rewrite when there is a subdomain in the host, and it is not a preferred mirror
112
132
  return true if !preferred_mirror?(h)
113
133
  #rewrite when there is a preferred mirror set and the subdomain of the host is not a mirror
114
- return true if self.preferred_mirror && !is_mirror?(h)
134
+ return true if config.preferred_mirror && !is_mirror?(h)
115
135
  #no rewrite if host already has mirror subdomain
116
136
  #it { SubdomainFu.needs_rewrite?(false,"www.localhost").should be_false }
117
137
  return false if is_mirror?(h)
@@ -129,7 +149,7 @@ module SubdomainFu
129
149
  end
130
150
 
131
151
  def self.current_subdomain(request)
132
- subdomain = request.subdomains(SubdomainFu.tld_size).join(".")
152
+ subdomain = request.subdomains(SubdomainFu.config.tld_size).join(".")
133
153
  if has_subdomain?(subdomain)
134
154
  subdomain
135
155
  else
@@ -1,48 +1,33 @@
1
+ require 'action_controller/metal/url_for'
2
+ require 'action_controller/url_rewriter'
3
+
1
4
  module ActionController
2
- module UrlWriter
3
- def url_for_with_subdomains(options)
4
- if SubdomainFu.needs_rewrite?(options[:subdomain], options[:host] || default_url_options[:host]) || options[:only_path] == false
5
- options[:only_path] = false if SubdomainFu.override_only_path?
6
- options[:host] = SubdomainFu.rewrite_host_for_subdomains(options.delete(:subdomain), options[:host] || default_url_options[:host])
7
- else
8
- options.delete(:subdomain)
9
- end
10
- url_for_without_subdomains(options)
11
- end
12
- alias_method_chain :url_for, :subdomains
13
- end
5
+ # module UrlFor
6
+ # def url_for_with_subdomains(options)
7
+ # if SubdomainFu.needs_rewrite?(options[:subdomain], options[:host] || default_url_options[:host]) || options[:only_path] == false
8
+ # options[:only_path] = false if SubdomainFu.override_only_path?
9
+ # options[:host] = SubdomainFu.rewrite_host_for_subdomains(options.delete(:subdomain), options[:host] || default_url_options[:host])
10
+ # else
11
+ # options.delete(:subdomain)
12
+ # end
13
+ # url_for_without_subdomains(options)
14
+ # end
15
+ # alias_method_chain :url_for, :subdomains
16
+ # end
14
17
 
15
18
  class UrlRewriter #:nodoc:
16
- private
17
-
18
- def rewrite_url_with_subdomains(options)
19
- if SubdomainFu.needs_rewrite?(options[:subdomain], (options[:host] || @request.host_with_port)) || options[:only_path] == false
20
- options[:only_path] = false if SubdomainFu.override_only_path?
21
- options[:host] = SubdomainFu.rewrite_host_for_subdomains(options.delete(:subdomain), options[:host] || @request.host_with_port)
22
- # puts "options[:host]: #{options[:host].inspect}"
23
- else
24
- options.delete(:subdomain)
25
- end
26
- rewrite_url_without_subdomains(options)
27
- end
28
- alias_method_chain :rewrite_url, :subdomains
29
- end
30
-
31
- if Rails::VERSION::MAJOR >= 2 and Rails::VERSION::MINOR <= 1
32
- # hack for http://www.portallabs.com/blog/2008/10/22/fixing-subdomain_fu-with-named-routes/
33
- module Routing
34
- module Optimisation
35
- class PositionalArgumentsWithAdditionalParams
36
- def guard_condition_with_subdomains
37
- # don't allow optimisation if a subdomain is present - fixes a problem
38
- # with the subdomain appearing in the query instead of being rewritten
39
- # see http://mbleigh.lighthouseapp.com/projects/13148/tickets/8-improper-generated-urls-with-named-routes-for-a-singular-resource
40
- guard_condition_without_subdomains + " && !args.last.has_key?(:subdomain)"
41
- end
42
-
43
- alias_method_chain :guard_condition, :subdomains
19
+ class << self
20
+ def rewrite_with_subdomains(options, path_segments=nil)
21
+ if SubdomainFu.needs_rewrite?(options[:subdomain], (options[:host] || @request.host_with_port)) || options[:only_path] == false
22
+ options[:only_path] = false if SubdomainFu.override_only_path?
23
+ options[:host] = SubdomainFu.rewrite_host_for_subdomains(options.delete(:subdomain), options[:host] || @request.host_with_port)
24
+ # puts "options[:host]: #{options[:host].inspect}"
25
+ else
26
+ options.delete(:subdomain)
44
27
  end
28
+ rewrite_without_subdomains(options, path_segments)
45
29
  end
30
+ alias_method_chain :rewrite, :subdomains
46
31
  end
47
32
  end
48
33
  end
data/rails/init.rb CHANGED
@@ -1,6 +1,6 @@
1
+ require 'action_controller/base'
2
+
1
3
  #Allow whatever Ruby Package tool is being used ot manage load paths. gem auto adds the gem's lib dir to load path.
2
4
  require 'subdomain-fu' unless defined?(SubdomainFu)
3
5
 
4
- ActionController::Base.send :include, SubdomainFu::Controller
5
-
6
- RAILS_DEFAULT_LOGGER.info("** SubdomainFu: initialized properly")
6
+ ActionController::Base.send :include, SubdomainFu::Controller
data/spec/spec_helper.rb CHANGED
@@ -1,14 +1,14 @@
1
1
  begin
2
2
  require File.dirname(__FILE__) + '/../../../../spec/spec_helper'
3
- rescue LoadError
4
- puts "You need to install rspec in your base app"
5
- exit
3
+ #rescue LoadError
4
+ # puts "You need to install RSpec in your base app. Add 'rspec-rails' to your Gemfile."
5
+ # exit
6
6
  end
7
7
 
8
8
  plugin_spec_dir = File.dirname(__FILE__)
9
9
  ActiveRecord::Base.logger = Logger.new(plugin_spec_dir + "/debug.log")
10
10
 
11
- ActionController::Routing::Routes.draw do |map|
11
+ Rails3::Application.routes.draw do |map|
12
12
  map.needs_subdomain '/needs_subdomain', :controller => "fu", :action => "awesome"
13
13
  map.no_subdomain '/no_subdomain', :controller => "fu", :action => "lame"
14
14
  map.needs_awesome '/needs_awesome', :controller => "fu", :action => "lame"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: subdomain-fu
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.4
4
+ version: 1.0.0.beta2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Bleigh
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-01-18 00:00:00 -05:00
12
+ date: 2010-02-09 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -28,7 +28,6 @@ files:
28
28
  - Rakefile
29
29
  - VERSION.yml
30
30
  - lib/subdomain-fu.rb
31
- - lib/subdomain_fu/routing_extensions.rb
32
31
  - lib/subdomain_fu/url_rewriter.rb
33
32
  - rails/init.rb
34
33
  - spec/spec.opts
@@ -52,9 +51,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
52
51
  version:
53
52
  required_rubygems_version: !ruby/object:Gem::Requirement
54
53
  requirements:
55
- - - ">="
54
+ - - ">"
56
55
  - !ruby/object:Gem::Version
57
- version: "0"
56
+ version: 1.3.1
58
57
  version:
59
58
  requirements: []
60
59
 
@@ -1,61 +0,0 @@
1
- # Thanks to Jamis Buck for ideas on this stuff
2
- # http://weblog.jamisbuck.org/2006/10/26/monkey-patching-rails-extending-routes-2
3
- # This is not yet a working part of SubdomainFu.
4
-
5
- module SubdomainFu
6
- module RouteExtensions
7
- def self.included(base)
8
- base.alias_method_chain :recognition_conditions, :subdomain
9
- end
10
-
11
- def recognition_conditions_with_subdomain
12
- result = recognition_conditions_without_subdomain
13
- result << "conditions[:subdomain] === env[:subdomain]" if conditions[:subdomain] && conditions[:subdomain] != true && conditions[:subdomain] != false
14
- result << "SubdomainFu.has_subdomain?(env[:subdomain])" if conditions[:subdomain] == true
15
- result << "!SubdomainFu.has_subdomain?(env[:subdomain])" if conditions[:subdomain] == false
16
- result
17
- end
18
- end
19
-
20
- module RouteSetExtensions
21
- def self.included(base)
22
- base.alias_method_chain :extract_request_environment, :subdomain
23
- end
24
-
25
- def extract_request_environment_with_subdomain(request)
26
- env = extract_request_environment_without_subdomain(request)
27
- env.merge(:host => request.host, :domain => request.domain, :subdomain => SubdomainFu.current_subdomain(request))
28
- end
29
- end
30
-
31
- module MapperExtensions
32
- def quick_map(has_unless, *args, &block)
33
- options = args.find{|a| a.is_a?(Hash)}
34
- namespace_str = options ? options.delete(:namespace).to_s : args.join('_or_')
35
- namespace_str += '_' unless namespace_str.blank?
36
- mapped_exp = args.map(&:to_s).join('|')
37
- conditions_hash = { :subdomain => ( has_unless ? /[^(#{mapped_exp})]/ : /(#{mapped_exp})/) }
38
- with_options(:conditions => conditions_hash, :name_prefix => namespace_str, &block)
39
- end
40
- # Adds methods to Mapper to apply an options with a method. Example
41
- # map.subdomain :blog { |blog| blog.resources :pages }
42
- # or
43
- # map.unless_subdomain :blog { |not_blog| not_blog.resources :people }
44
- def subdomain(*args, &block)
45
- quick_map(false, *args, &block)
46
- end
47
- def unless_subdomain(*args, &block)
48
- quick_map(true, *args, &block)
49
- end
50
- end
51
- end
52
-
53
- ActionController::Routing::RouteSet::Mapper.send :include, SubdomainFu::MapperExtensions
54
- ActionController::Routing::RouteSet.send :include, SubdomainFu::RouteSetExtensions
55
- ActionController::Routing::Route.send :include, SubdomainFu::RouteExtensions
56
-
57
- # UrlRewriter::RESERVED_OPTIONS is only available in Rails >= 2.2
58
- # http://www.portallabs.com/blog/2008/12/02/fixing-subdomain_fu-with-named-routes-rails-22/
59
- if Rails::VERSION::MAJOR >= 2 and Rails::VERSION::MINOR >= 2
60
- ActionController::UrlRewriter::RESERVED_OPTIONS << :subdomain
61
- end