github
Advanced Search
  • Home
  • Pricing and Signup
  • Explore GitHub
  • Blog
  • Login

mbleigh / subdomain-fu

  • Admin
  • Watch Unwatch
  • Fork
  • Your Fork
  • Pull Request
  • Download Source
    • 463
    • 32
  • Source
  • Commits
  • Network (32)
  • Issues (6)
  • Downloads (5)
  • Wiki (1)
  • Graphs
  • Branch: master

click here to add a description

click here to add a homepage

  • Branches (1)
    • master ✓
  • Tags (5)
    • v0.5.4
    • v0.5.3
    • v0.4.1
    • v0.4.0
    • v0.1.0
Sending Request…
Enable Donations

Pledgie Donations

Once activated, we'll place the following badge in your repository's detail box:
Pledgie_example
This service is courtesy of Pledgie.

A new plugin approach to attempting to solve the usage of subdomains in linking and routing in Rails projects. — Read more

  cancel

http://rdoc.info/projects/mbleigh/subdomain-fu

  cancel
  • Private
  • Read-Only
  • HTTP Read-Only

This URL has Read+Write access

Regenerated gemspec for version 0.5.4 
mbleigh (author)
Mon Jan 18 05:30:40 -0800 2010
commit  18511eb922f0577788d787bbc986eee6c50a6570
tree    c223dbb3a8703f7453ee55a3259921a56acec9c6
parent  b27fcc1743c585020f17d991ca5ba9ac006297e7
subdomain-fu /
name age
history
message
file .gitignore Tue Aug 25 13:36:58 -0700 2009 installs correctly, only 9 failing tests to fix. [pboling]
file CHANGELOG Sun Sep 13 10:02:41 -0700 2009 fixing load path [pboling]
file MIT-LICENSE Fri Jun 13 10:03:08 -0700 2008 Added gemspec for GemPlugin, updated README. [mbleigh]
file README.rdoc Mon Jan 18 05:30:09 -0800 2010 Fixed formating, and updated to not include git... [technicalpickles]
file Rakefile Mon Jan 18 05:30:39 -0800 2010 Version bump to 0.5.4 [mbleigh]
file VERSION.yml Mon Jan 18 05:30:39 -0800 2010 Version bump to 0.5.4 [mbleigh]
file init.rb Sun Sep 13 10:02:41 -0700 2009 fixing load path [pboling]
file install.sh Sun Sep 13 10:04:24 -0700 2009 bumping version to 0.5.2 [pboling]
directory lib/ Mon Jan 18 05:30:39 -0800 2010 Version bump to 0.5.4 [mbleigh]
directory rails/ Sun Sep 13 06:21:46 -0700 2009 fixing load path [pboling]
directory spec/ Sun Sep 13 05:20:35 -0700 2009 added current_domain method [pboling]
file subdomain-fu.gemspec Mon Jan 18 05:30:40 -0800 2010 Regenerated gemspec for version 0.5.4 [mbleigh]
README.rdoc

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 and url management.

Installation

SubdomainFu is available both as a traditional plugin and a GemPlugin. To install it as a traditional plugin (Rails 2.1 or later):

  script/plugin install git://github.com/mbleigh/subdomain-fu.git

To use it as a gem, add it to your config/environment.rb:

  config.gem 'subdomain-fu'

Examples

SubdomainFu works inside of Rails’s URL Writing mechanisms to provide an easy and seamless way to link and otherwise understand cross-subdomain routing. You can use the :subdomain option both in named and non-named routes as well as in generated resources routes.

Let’s say my domain is ‘intridea.com’. Here are some examples of the use of the :subdomain option:

  url_for(:controller => "my_controller",
          :action => "my_action",
          :subdomain => "awesome") # => http://awesome.intridea.com/my_controller/my_action

Now let’s say I’m at awesome.intridea.com/ and I want back to the root. Specifying "false" will remove any current subdomain:

  users_url(:subdomain => false)  # => http://intridea.com/users

Note that this plugin does not honor the :only_path notion of routing when doing so would go against the intent of the command. For example, if I were at intridea.com again:

  users_path(:subdomain => "fun") # => http://fun.intridea.com/users
  users_path(:subdomain => false) # => /users

In this way you can rest assured that you will never misdirect your links to the same subdomain when you meant to change it.

Use in controllers and views

You have access to current_subdomain and current_domain methods.

current_subdomain
returns all subdomains. For the URL awesome.website.stuff.example.com, it will return "awesome.website.stuff"
current_domain
returns all subdomains except for the subdomain, including the TLD. For the URL awesome.website.stuff.example.com, it will return "website.stuff.example.com"

If what you really want is the entire domain, then use request.domain in your controllers. The purpose of current_domain is to only strip off the first subdomain, if any, and return what’s left.

Configuration

You may need to configure SubdomainFu based on your development setup. The configuration required is:

TLD Size

A hash for each environment of the size of the top-level domain name. (something.com = 1, localhost = 0, etc.)

  SubdomainFu.tld_size = 1 # sets for current environment
  SubdomainFu.tld_sizes = {:development => 0,
                           :test => 0,
                           :production => 1} # set all at once (also the defaults)

Mirrors

Mirrors are the subdomains that are equivalent to no subdomain (i.e. they ‘mirror’) the usage of the root domain.

  SubdomainFu.mirrors = %w(www site we) # Defaults to %w(www)

Preferred Mirror

SubdomainFu also understands the notion of a ‘preferred mirror’, that is, if you always want your links going to ‘www.yourdomain.com’ instead of ‘yourdomain.com’, you can set the preferred mirror like so:

  SubdomainFu.preferred_mirror = "www"

Now when you create a link with :subdomain => false in the options the subdomain will default to the preferred mirror.

Routing

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 config/routes.rb file:

  map.with_options :conditions => {:subdomain => 'admin'} do |admin|
    admin.resources :posts
    admin.resources :users
  end

In addition to specifying a string, you could also specify false 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.

Resources

  • GitHub Repository: github.com/mbleigh/subdomain-fu
  • RDocs: rdoc.info/projects/mbleigh/subdomain-fu

Copyright © 2008 Michael Bleigh (www.mbleigh.com/) and Intridea, Inc. (www.intridea.com/). Released under the MIT license

Blog | Support | Training | Contact | API | Status | Twitter | Help | Security
© 2010 GitHub Inc. All rights reserved. | Terms of Service | Privacy Policy
Powered by the Dedicated Servers and
Cloud Computing of Rackspace Hosting®
Dedicated Server