Permalink
Browse files

Initial import. Not even remotely complete yet.

  • Loading branch information...
0 parents commit 5fd32298cb608ae1663b6bf50c5d0941bafbdb50 Michael Bleigh committed Jun 5, 2008
Showing with 175 additions and 0 deletions.
  1. +20 −0 MIT-LICENSE
  2. +27 −0 README
  3. +22 −0 Rakefile
  4. +4 −0 init.rb
  5. +75 −0 lib/subdomain_fu.rb
  6. +1 −0 lib/subdomain_fu/routing_extensions.rb
  7. +26 −0 lib/subdomain_fu/url_rewriter.rb
@@ -0,0 +1,20 @@
+Copyright (c) 2008 [name of plugin creator]
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 README
@@ -0,0 +1,27 @@
+*******************************************************************
+*** THIS PLUGIN IS NOT COMPLETE, PLEASE DON'T TRY TO USE IT YET ***
+*******************************************************************
+
+SubdomainFu
+=========
+
+SubdomainFu provides a modern implementation of subdomain handling in Rails.
+It takes aspects from account_location, request_routing, and other snippets
+found around the web and combines them to provide a single, simple solution
+for subdomain-based route management.
+
+Example
+=======
+
+Feature Wishlist
+
+* Link between subdomains via :subdomain option (with :subdomain => false corresponding to no subdomain)
+* Route between subdomains, raise error on subdomain-requiring subdomains that don't have an subdomain
+* Get current subdomain from subdomain
+* Must use _url instead of _path to switch between subdomains or switch to no subdomain
+
+link_to "Some Subdomain Page", some_resources_path()
+
+
+Copyright (c) 2008 Michael Bleigh (http://www.mbleigh.com/) and
+Intridea, Inc. (http://www.intridea.com/). Released under the MIT license
@@ -0,0 +1,22 @@
+require 'rake'
+require 'rake/testtask'
+require 'rake/rdoctask'
+
+desc 'Default: run unit tests.'
+task :default => :test
+
+desc 'Test the account_fu plugin.'
+Rake::TestTask.new(:test) do |t|
+ t.libs << 'lib'
+ t.pattern = 'test/**/*_test.rb'
+ t.verbose = true
+end
+
+desc 'Generate documentation for the account_fu plugin.'
+Rake::RDocTask.new(:rdoc) do |rdoc|
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = 'AccountFu'
+ rdoc.options << '--line-numbers' << '--inline-source'
+ rdoc.rdoc_files.include('README')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
@@ -0,0 +1,4 @@
+require 'subdomain_fu'
+require 'subdomain_fu/url_rewriter'
+
+ActionController::Base.send :include, SubdomainFu::Controller
@@ -0,0 +1,75 @@
+require 'subdomain_fu/routing_extensions'
+
+module SubdomainFu
+ # The length of the period-split top-level domain for each environment.
+ # For example, "localhost" has a tld_size of zero, and "something.co.uk"
+ # has a tld_size of two.
+ #
+ # To set a tld size for a given environment, just call SubdomainFu.tld_sizes[:environment] = value
+ mattr_accessor :tld_sizes
+ @@tld_sizes = {:development => 0, :test => 0, :production => 1}
+
+ # Subdomains that are equivalent to going to the website with no subdomain at all.
+ # Defaults to "www" as the only member.
+ mattr_accessor :mirrors
+ @@mirrors = %w(www)
+
+ mattr_accessor :preferred_mirror
+ @@preferred_mirror = nil
+
+ # Returns the TLD Size of the current environment.
+ def self.tld_size
+ tld_sizes[RAILS_ENV.to_sym]
+ end
+
+ def self.tld_size=(value)
+ tld_sizes[RAILS_ENV.to_sym] = value
+ end
+
+ def self.has_subdomain?(subdomain)
+ subdomain && !SubdomainFu.mirrors.include?(subdomain)
+ end
+
+ def self.subdomain_from(host)
+ return nil unless host
+ parts = host.split('.')
+ parts[0..-(SubdomainFu.tld_size+2)].join(".")
+ end
+
+ def self.host_without_subdomain(host)
+ parts = host.split('.')
+ parts[-(SubdomainFu.tld_size+1)..-1].join(".")
+ end
+
+ def self.rewrite_host_for_subdomains(subdomain, host)
+ if same_subdomain?(subdomain, host)
+ host
+ else
+ change_subdomain_of_host(subdomain, host)
+ end
+ end
+
+ def self.change_subdomain_of_host(subdomain, host)
+ host = SubdomainFu.host_without_subdomain(host)
+ host = "#{subdomain}.#{host}" if subdomain
+ host
+ end
+
+ def self.same_subdomain?(subdomain, host)
+ result = subdomain == SubdomainFu.subdomain_from(host) ||
+ (!SubdomainFu.has_subdomain?(subdomain) && !SubdomainFu.has_subdomain?(SubdomainFu.subdomain_from(host)))
+ result
+ end
+
+ module Controller
+ def self.included(controller)
+ controller.helper_method(:current_subdomain, :current_subdomain)
+ end
+
+ protected
+
+ def current_subdomain
+ request.subdomains(SubdomainFu.tld_size).join(".")
+ end
+ end
+end
@@ -0,0 +1 @@
+# Do stuff to allow for route conditions for subdomains
@@ -0,0 +1,26 @@
+module ActionController
+ module UrlWriter
+ def url_for_with_subdomains(options)
+ unless SubdomainFu.same_subdomain?(options[:subdomain], options[:host])
+ options[:only_path] = false
+ options[:host] = SubdomainFu.rewrite_host_for_subdomains(options.delete(:subdomain), options[:host])
+ end
+ url_for_without_subdomains(options)
+ end
+ end
+
+ class UrlRewriter #:nodoc:
+ private
+
+ def rewrite_url_with_subdomains(options)
+ puts "Comparing: #{options[:subdomain]}, #{options[:host] || @request.host_with_port}"
+ unless SubdomainFu.same_subdomain?(options[:subdomain], (options[:host] || @request.host_with_port))
+ puts "Different: #{options[:subdomain]}, #{options[:host] || @request.host_with_port}"
+ options[:only_path] = false
+ options[:host] = SubdomainFu.rewrite_host_for_subdomains(options.delete(:subdomain), options[:host] || @request.host_with_port)
+ end
+ rewrite_url_without_subdomains(options)
+ end
+ alias_method_chain :rewrite_url, :subdomains
+ end
+end

0 comments on commit 5fd3229

Please sign in to comment.