mbleigh-subdomain-fu 0.0.1

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/MIT-LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ Copyright (c) 2008 Michael Bleigh (http://www.mbleigh.com) and
2
+ Intridea, Inc (http://www.intridea.com)
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining
5
+ a copy of this software and associated documentation files (the
6
+ "Software"), to deal in the Software without restriction, including
7
+ without limitation the rights to use, copy, modify, merge, publish,
8
+ distribute, sublicense, and/or sell copies of the Software, and to
9
+ permit persons to whom the Software is furnished to do so, subject to
10
+ the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,64 @@
1
+ SubdomainFu
2
+ ===========
3
+
4
+ SubdomainFu provides a modern implementation of subdomain handling in Rails.
5
+ It takes aspects from account_location, request_routing, and other snippets
6
+ found around the web and combines them to provide a single, simple solution
7
+ for subdomain-based route and url management.
8
+
9
+ Installation
10
+ ============
11
+
12
+ SubdomainFu is available both as a traditional plugin and a GemPlugin. To
13
+ install it as a traditional plugin (Rails 2.1 or later):
14
+
15
+ script/plugin install git://github.com/mbleigh/subdomain-fu.git
16
+
17
+ To use it as a GemPlugin, add it to your environment.rb:
18
+
19
+ config.gem 'mbleigh-subdomain-fu', :source => "http://gems.github.com", :lib => "subdomain-fu"
20
+
21
+ Configuration
22
+ =============
23
+
24
+ You may need to configure SubdomainFu based on your development setup. The
25
+ configuration required is:
26
+
27
+ tld_size
28
+ --------
29
+
30
+ A hash for each environment of the size of the top-level domain name.
31
+ (something.com = 1, localhost = 0, etc.)
32
+
33
+ SubdomainFu.tld_size = 1 # sets for current environment
34
+ SubdomainFu.tld_sizes = {:development => 0,
35
+ :test => 0,
36
+ :production => 1} # set all at once (also the defaults)
37
+
38
+ mirrors
39
+ -------
40
+
41
+ Mirrors are the subdomains that are equivalent to no subdomain (i.e. they 'mirror')
42
+ the usage of the root domain.
43
+
44
+ SubdomainFu.mirrors = %w(www site we) # Defaults to %w(www)
45
+
46
+ Examples
47
+ ========
48
+
49
+ SubdomainFu works in both the URL writing parts of Rails and the routes. For
50
+ URL writing, it provides you an option you can pass into any URL-generating
51
+ call to use (or lose) a subdomain:
52
+
53
+ url_for(:controller => "my_controller", :action => "my_action", :subdomain => "awesome")
54
+ users_url(:subdomain => false) # specifying "false" will remove any current subdomain
55
+
56
+ Known Issues / TODO
57
+ ===================
58
+
59
+ * TODO: Implement route conditions that require either a subdomain or no subdomain
60
+ * TODO: Spec out URL Rewriting more completely, test with different domain levels etc.
61
+ * TODO: Implement "preferred_mirror" to automatically use "www" for example if that's desired.
62
+
63
+ Copyright (c) 2008 Michael Bleigh (http://www.mbleigh.com/) and
64
+ Intridea, Inc. (http://www.intridea.com/). Released under the MIT license
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require File.dirname(__FILE__) + "/rails/init"
@@ -0,0 +1,84 @@
1
+ require 'subdomain_fu/routing_extensions'
2
+ require 'subdomain_fu/url_rewriter'
3
+
4
+ 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
+ mattr_accessor :mirrors
17
+ @@mirrors = %w(www)
18
+
19
+ mattr_accessor :preferred_mirror
20
+ @@preferred_mirror = nil
21
+
22
+ # Returns the TLD Size of the current environment.
23
+ def self.tld_size
24
+ tld_sizes[RAILS_ENV.to_sym]
25
+ end
26
+
27
+ # Sets the TLD Size of the current environment
28
+ def self.tld_size=(value)
29
+ tld_sizes[RAILS_ENV.to_sym] = value
30
+ end
31
+
32
+ # Is the current subdomain either nil or a mirror?
33
+ def self.has_subdomain?(subdomain)
34
+ !subdomain.blank? && !SubdomainFu.mirrors.include?(subdomain)
35
+ end
36
+
37
+ # Gets the subdomain from the host based on the TLD size
38
+ def self.subdomain_from(host)
39
+ return nil unless host
40
+ parts = host.split('.')
41
+ sub = parts[0..-(SubdomainFu.tld_size+2)].join(".")
42
+ sub.blank? ? nil : sub
43
+ end
44
+
45
+ def self.host_without_subdomain(host)
46
+ parts = host.split('.')
47
+ parts[-(SubdomainFu.tld_size+1)..-1].join(".")
48
+ end
49
+
50
+ # Rewrites the subdomain of the host unless they are equivalent (i.e. mirrors of each other)
51
+ def self.rewrite_host_for_subdomains(subdomain, host)
52
+ if same_subdomain?(subdomain, host)
53
+ host
54
+ else
55
+ change_subdomain_of_host(subdomain, host)
56
+ end
57
+ end
58
+
59
+ # Changes the subdomain of the host to whatever is passed in.
60
+ def self.change_subdomain_of_host(subdomain, host)
61
+ host = SubdomainFu.host_without_subdomain(host)
62
+ host = "#{subdomain}.#{host}" if subdomain
63
+ host
64
+ end
65
+
66
+ # Is this subdomain equivalent to the subdomain found in this host string?
67
+ def self.same_subdomain?(subdomain, host)
68
+ result = subdomain == SubdomainFu.subdomain_from(host) ||
69
+ (!SubdomainFu.has_subdomain?(subdomain) && !SubdomainFu.has_subdomain?(SubdomainFu.subdomain_from(host)))
70
+ result
71
+ end
72
+
73
+ module Controller
74
+ def self.included(controller)
75
+ controller.helper_method(:current_subdomain)
76
+ end
77
+
78
+ protected
79
+
80
+ def current_subdomain
81
+ request.subdomains(SubdomainFu.tld_size).join(".")
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,28 @@
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
+
4
+ module SubdomainFu
5
+ module RouteExtensions
6
+ def self.included(base)
7
+ base.alias_method_chain :recognition_conditions, :subdomain
8
+ end
9
+
10
+ def recognition_conditions_with_subdomain
11
+ result = recognition_conditions_without_subdomain
12
+ result << "conditions[:subdomain] === env[:subdomain]" if conditions[:subdomain] && conditions[:subdomain] != true
13
+ result << "SubdomainFu.has_subdomain?(env[:subdomain])" if conditions[:subdomain] == true
14
+ result
15
+ end
16
+ end
17
+
18
+ module RouteSetExtensions
19
+ def self.included(base)
20
+ base.alias_method_chain :extract_request_environment, :subdomain
21
+ end
22
+
23
+ def extract_request_environment_with_subdomain(request)
24
+ env = extract_request_environment_without_subdomain(request)
25
+ env.merge(:host => request.host, :domain => request.domain, :subdomain => SubdomainFu.subdomain_from(request.host))
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,29 @@
1
+ module ActionController
2
+ module UrlWriter
3
+ def url_for_with_subdomains(options)
4
+ if SubdomainFu.same_subdomain?(options[:subdomain], options[:host] || default_url_options[:host])
5
+ options.delete(:subdomain)
6
+ else
7
+ options[:only_path] = false
8
+ options[:host] = SubdomainFu.rewrite_host_for_subdomains(options.delete(:subdomain), options[:host] || default_url_options[:host])
9
+ end
10
+ url_for_without_subdomains(options)
11
+ end
12
+ alias_method_chain :url_for, :subdomains
13
+ end
14
+
15
+ class UrlRewriter #:nodoc:
16
+ private
17
+
18
+ def rewrite_url_with_subdomains(options)
19
+ if SubdomainFu.same_subdomain?(options[:subdomain], (options[:host] || @request.host_with_port))
20
+ options.delete(:subdomain)
21
+ else
22
+ options[:only_path] = false
23
+ options[:host] = SubdomainFu.rewrite_host_for_subdomains(options.delete(:subdomain), options[:host] || @request.host_with_port)
24
+ end
25
+ rewrite_url_without_subdomains(options)
26
+ end
27
+ alias_method_chain :rewrite_url, :subdomains
28
+ end
29
+ end
data/rails/init.rb ADDED
@@ -0,0 +1,8 @@
1
+ require 'subdomain-fu'
2
+
3
+ ActionController::Base.send :include, SubdomainFu::Controller
4
+
5
+ ActionController::Routing::RouteSet.send :include, SubdomainFu::RouteSetExtensions
6
+ ActionController::Routing::Route.send :include, SubdomainFu::RouteExtensions
7
+
8
+ RAILS_DEFAULT_LOGGER.info("** SubdomainFu: initialized properly")
@@ -0,0 +1,19 @@
1
+ begin
2
+ require File.dirname(__FILE__) + '/../../../../spec/spec_helper'
3
+ rescue LoadError
4
+ puts "You need to install rspec in your base app"
5
+ exit
6
+ end
7
+
8
+ plugin_spec_dir = File.dirname(__FILE__)
9
+ ActiveRecord::Base.logger = Logger.new(plugin_spec_dir + "/debug.log")
10
+
11
+ ActionController::Routing::Routes.draw do |map|
12
+ map.needs_subdomain '/needs_subdomain', :controller => "fu", :action => "awesome", :conditions => {:subdomain => true}
13
+ map.resources :fu_somethings, :conditions => {:subdomain => true}
14
+ map.connect '/:controller/:action/:id'
15
+ end
16
+
17
+
18
+ include ActionController::UrlWriter
19
+ default_url_options[:host] = "testapp.com"
@@ -0,0 +1,109 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe "SubdomainFu" do
4
+ describe "TLD Sizes" do
5
+ before do
6
+ SubdomainFu.tld_sizes = SubdomainFu::DEFAULT_TLD_SIZES.dup
7
+ end
8
+
9
+ it { SubdomainFu.tld_sizes.should be_kind_of(Hash) }
10
+
11
+ it "should have default values for development, test, and production" do
12
+ SubdomainFu.tld_sizes[:development].should == 0
13
+ SubdomainFu.tld_sizes[:test].should == 0
14
+ SubdomainFu.tld_sizes[:production].should == 1
15
+ end
16
+
17
+ it "#tld_size should be for the current environment" do
18
+ SubdomainFu.tld_size.should == SubdomainFu.tld_sizes[RAILS_ENV.to_sym]
19
+ end
20
+
21
+ it "should be able to be set for the current environment" do
22
+ SubdomainFu.tld_size = 5
23
+ SubdomainFu.tld_size.should == 5
24
+ SubdomainFu.tld_sizes[:test].should == 5
25
+ end
26
+ end
27
+
28
+ describe "#has_subdomain?" do
29
+ it "should be true for non-mirrored subdomains" do
30
+ SubdomainFu.has_subdomain?("awesome").should be_true
31
+ end
32
+
33
+ it "should be false for mirrored subdomains" do
34
+ SubdomainFu.has_subdomain?(SubdomainFu.mirrors.first).should be_false
35
+ end
36
+
37
+ it "shoud be false for a nil or blank subdomain" do
38
+ SubdomainFu.has_subdomain?("").should be_false
39
+ SubdomainFu.has_subdomain?(nil).should be_false
40
+ end
41
+ end
42
+
43
+ describe "#subdomain_from" do
44
+ it "should return the subdomain based on the TLD of the current environment" do
45
+ SubdomainFu.subdomain_from("awesome.localhost").should == "awesome"
46
+ SubdomainFu.tld_size = 1
47
+ SubdomainFu.subdomain_from("awesome.localhost.com").should == "awesome"
48
+ SubdomainFu.tld_size = 0
49
+ end
50
+
51
+ it "should join deep subdomains with a period" do
52
+ SubdomainFu.subdomain_from("awesome.coolguy.localhost").should == "awesome.coolguy"
53
+ end
54
+
55
+ it "should return nil for no subdomain" do
56
+ SubdomainFu.subdomain_from("localhost").should be_nil
57
+ end
58
+ end
59
+
60
+ it "#host_without_subdomain should chop of the subdomain and return the rest" do
61
+ SubdomainFu.host_without_subdomain("awesome.localhost:3000").should == "localhost:3000"
62
+ SubdomainFu.host_without_subdomain("something.awful.localhost:3000").should == "localhost:3000"
63
+ end
64
+
65
+ describe "#rewrite_host_for_subdomains" do
66
+ it "should not change the same subdomain" do
67
+ SubdomainFu.rewrite_host_for_subdomains("awesome","awesome.localhost").should == "awesome.localhost"
68
+ end
69
+
70
+ it "should not change an equivalent (mirrored) subdomain" do
71
+ SubdomainFu.rewrite_host_for_subdomains("www","localhost").should == "localhost"
72
+ end
73
+
74
+ it "should change the subdomain if it's different" do
75
+ SubdomainFu.rewrite_host_for_subdomains("cool","www.localhost").should == "cool.localhost"
76
+ end
77
+
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
+ end
81
+
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
+ end
85
+ end
86
+
87
+ describe "#change_subdomain_of_host" do
88
+ it "should change it if passed a different one" do
89
+ SubdomainFu.change_subdomain_of_host("awesome","cool.localhost").should == "awesome.localhost"
90
+ end
91
+
92
+ it "should remove it if passed nil" do
93
+ SubdomainFu.change_subdomain_of_host(nil,"cool.localhost").should == "localhost"
94
+ end
95
+
96
+ it "should add it if there isn't one" do
97
+ SubdomainFu.change_subdomain_of_host("awesome","localhost").should == "awesome.localhost"
98
+ end
99
+ end
100
+
101
+ describe "#same_subdomain?" do
102
+ it { SubdomainFu.same_subdomain?("www","www.localhost").should be_true }
103
+ it { SubdomainFu.same_subdomain?("www","localhost").should be_true }
104
+ it { SubdomainFu.same_subdomain?("awesome","www.localhost").should be_false }
105
+ it { SubdomainFu.same_subdomain?("cool","awesome.localhost").should be_false }
106
+ it { SubdomainFu.same_subdomain?(nil,"www.localhost").should be_true }
107
+ it { SubdomainFu.same_subdomain?("www","awesome.localhost").should be_false }
108
+ end
109
+ end
@@ -0,0 +1,50 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe "SubdomainFu URL Writing" do
4
+ before do
5
+ SubdomainFu.tld_size = 1
6
+ end
7
+
8
+ describe "#url_for" do
9
+ it "should be able to add a subdomain" do
10
+ url_for(:controller => "something", :action => "other", :subdomain => "awesome").should == "http://awesome.testapp.com/something/other"
11
+ end
12
+
13
+ it "should be able to remove a subdomain" do
14
+ url_for(:controller => "something", :action => "other", :subdomain => false, :host => "awesome.testapp.com").should == "http://testapp.com/something/other"
15
+ end
16
+
17
+ it "should not change a mirrored subdomain" do
18
+ url_for(:controller => "something", :action => "other", :subdomain => false, :host => "www.testapp.com").should == "http://www.testapp.com/something/other"
19
+ end
20
+
21
+ it "should should force the full url, even with :only_path" do
22
+ url_for(:controller => "something", :action => "other", :subdomain => "awesome", :only_path => true).should == "http://awesome.testapp.com/something/other"
23
+ end
24
+ end
25
+
26
+ describe "Standard Routes" do
27
+ it "should be able to add a subdomain" do
28
+ needs_subdomain_url(:subdomain => "awesome").should == "http://awesome.testapp.com/needs_subdomain"
29
+ end
30
+
31
+ it "should be able to remove a subdomain" do
32
+ default_url_options[:host] = "awesome.testapp.com"
33
+ needs_subdomain_url(:subdomain => false).should == "http://testapp.com/needs_subdomain"
34
+ end
35
+
36
+ it "should not change a mirrored subdomain" do
37
+ default_url_options[:host] = "www.testapp.com"
38
+ needs_subdomain_url(:subdomain => false).should == "http://www.testapp.com/needs_subdomain"
39
+ end
40
+
41
+ it "should should force the full url, even with _path" do
42
+ needs_subdomain_path(:subdomain => "awesome").should == needs_subdomain_url(:subdomain => "awesome")
43
+ end
44
+ end
45
+
46
+ after do
47
+ SubdomainFu.tld_size = 0
48
+ default_url_options[:host] = "testapp.com"
49
+ end
50
+ end
@@ -0,0 +1,26 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "subdomain-fu"
3
+ s.version = "0.0.1"
4
+ s.date = "2008-06-13"
5
+ s.summary = "Provides a simple solution for route handling and linking between subdomains in a Rails application."
6
+ s.email = "michael@intridea.com"
7
+ s.homepage = "http://www.actsascommunity.com/projects/subdomain-fu"
8
+ s.description = "SubdomainFu aims to solve the problem of subdomain-based routing and in a unified way, establishing simple conventions for linking between subdomains of a Rails app."
9
+ s.has_rdoc = true
10
+ s.authors = ["Michael Bleigh"]
11
+ s.files = [ "MIT-LICENSE",
12
+ "README",
13
+ "init.rb",
14
+ "lib/subdomain_fu",
15
+ "lib/subdomain_fu/routing_extensions.rb",
16
+ "lib/subdomain_fu/url_rewriter.rb",
17
+ "lib/subdomain-fu.rb",
18
+ "rails/init.rb",
19
+ "spec/spec_helper.rb",
20
+ "spec/subdomain_fu_spec.rb",
21
+ "spec/url_rewriter_spec.rb",
22
+ "subdomain-fu.gemspec" ]
23
+ s.rdoc_options = ["--main", "README"]
24
+ #s.extra_rdoc_files = ["History.txt", "Manifest.txt", "README.txt"]
25
+ #s.add_dependency("mbleigh-mash", [">= 0.0.5"])
26
+ end
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mbleigh-subdomain-fu
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Michael Bleigh
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-06-13 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: SubdomainFu aims to solve the problem of subdomain-based routing and in a unified way, establishing simple conventions for linking between subdomains of a Rails app.
17
+ email: michael@intridea.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - MIT-LICENSE
26
+ - README
27
+ - init.rb
28
+ - lib/subdomain_fu
29
+ - lib/subdomain_fu/routing_extensions.rb
30
+ - lib/subdomain_fu/url_rewriter.rb
31
+ - lib/subdomain-fu.rb
32
+ - rails/init.rb
33
+ - spec/spec_helper.rb
34
+ - spec/subdomain_fu_spec.rb
35
+ - spec/url_rewriter_spec.rb
36
+ - subdomain-fu.gemspec
37
+ has_rdoc: true
38
+ homepage: http://www.actsascommunity.com/projects/subdomain-fu
39
+ post_install_message:
40
+ rdoc_options:
41
+ - --main
42
+ - README
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: "0"
50
+ version:
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: "0"
56
+ version:
57
+ requirements: []
58
+
59
+ rubyforge_project:
60
+ rubygems_version: 1.0.1
61
+ signing_key:
62
+ specification_version: 2
63
+ summary: Provides a simple solution for route handling and linking between subdomains in a Rails application.
64
+ test_files: []
65
+