• Stars
    star
    590
  • Rank 75,794 (Top 2 %)
  • Language
    Ruby
  • License
    Other
  • Created over 16 years ago
  • Updated about 9 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

SubdomainFu <img src=“https://codeclimate.com/badge.png” />

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.

Note: SubdomainFu has been rewritten to be compatible with Rails 3. If you are looking to use it on Rails 2.x, please install version v0.5.x instead.

Installation

SubdomainFu is available both as a traditional plugin and a GemPlugin. To install it as a traditional plugin:

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

To use it as a gem, add it to your Gemfile. Until Rails 3 is officially released, the Rails 3 version of the plugin is only available as a prerelease gem:

gem 'subdomain-fu', '1.0.0.beta2'

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

You should exercise caution using the :only_path option when you want to change subdomains. If you want the plugin to ensure that you switch subdomains when you mean to, you should set SubdomainFu.override_only_path to true in your application configuration. This will disregard :only_path in routing helpers. With this option, 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.

Note that in this was originally the default behavior of the plugin, but has not always been so, and so may constitute a change for you when upgrading to the latest and greatest.

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 the domain excluding 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. To configure SubdomainFu simply call a block in an initializer like so:

SubdomainFu.configure do |config|
  config.option_name = value
end

Some available options are enumerated below.

tld_size

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

config.tld_size = 1 # sets for current environment
config.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.

config.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:

config.preferred_mirror = "www"

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

Routing (Deprecated)

Subdomain constraint routing has been removed from the scope of this plugin as Rails 3 provides the functionality by default. For more info, see this blog post: yehudakatz.com/2009/12/26/the-rails-3-router-rack-it-up/

Resources

Versioning

This library aims to adhere to Semantic Versioning 2.0.0. Violations of this scheme should be reported as bugs. Specifically, if a minor or patch version is released that breaks backward compatibility, a new version should be immediately released that restores compatibility. Breaking changes to the public API will only be introduced with new major versions.

As a result of this policy, you can (and should) specify a dependency on this gem using the Pessimistic Version Constraint with two digits of precision.

For example:

spec.add_dependency 'subdomain-fu', '~> 4.0'

Thanks

Copyright © 2012 Michael Bleigh (www.mbleigh.com/), Peter Boling (www.railsbling.com/), and Intridea, Inc. (www.intridea.com/) Copyright © 2008-2011 Michael Bleigh (www.mbleigh.com/) and Intridea, Inc. (www.intridea.com/).

Released under the MIT license

More Repositories

1

acts-as-taggable-on

A tagging plugin for Rails applications that allows for custom tagging along dynamic contexts.
Ruby
4,972
star
2

seed-fu

Advanced seed data handling for Rails, combining the best practices of several methods together.
Ruby
1,221
star
3

twitter-auth

Standard authentication stack for Rails using Twitter to log in.
Ruby
358
star
4

princely

A simple Rails wrapper for the PrinceXML PDF generation library.
Ruby
233
star
5

uberkit

The UberKit is a Rails plugin with a set of UI tools to ease common development.
Ruby
103
star
6

omniauth-jwt

An OmniAuth strategy that uses JSON Web Token for Single Sign-On
Ruby
99
star
7

mbleigh.github.com

My blog and website.
Ruby
72
star
8

mash

Mash is a Hash with the ability to read, write, and test for the presence of arbitrary attributes using method calls.
Ruby
61
star
9

ruby-github

A Ruby library for getting information from the GitHub API.
Ruby
58
star
10

canonical-url

Rails plugin to take advantage of the new Canonical URL support of search engines.
Ruby
50
star
11

pwas-on-firebase

Demos and related material for Google I/O Progressive Web Apps on Firebase talk.
HTML
50
star
12

colorist

A Ruby library built to handle the easy conversion and manipulation of colors with a special emphasis on W3C standards and CSS-style hex color notation.
Ruby
48
star
13

acts-as-readable

A simple plugin that allows a user to mark anything as 'read.' Common usage would include forum posts, news items, etc.
Ruby
44
star
14

escapable-amp

TypeScript
30
star
15

conf_ask

A simple demonstration app built to show off Grape
JavaScript
26
star
16

fetches

A Rails plugin to simplify the fetching and memoization of records for parameter-based finds.
Ruby
25
star
17

persistence-smoothie

The source code (and slides in the Downloads section) for my talk "Persistence Smoothie: Blending SQL and NoSQL"
Ruby
21
star
18

marky

A Markdown rendering wrapper for the EtherPad collaborative editor.
JavaScript
17
star
19

twisteners

Who's listening to you? Find out with this Twitter app coded live at RailsConf 2009.
JavaScript
17
star
20

relates-to

Rails Plugin to provide simple polymorphic relationships between models.
Ruby
16
star
21

github-unfuddle

A web hook that takes GitHub commits and turns them into Unfuddle changesets posting using the Unfuddle API.
Ruby
15
star
22

from_param

Rails plugin that adds a from_param class method to ActiveRecord::Base for simple URL-based fetching.
Ruby
14
star
23

web-components-in-action

Examples and exercises for Web Components in Action workshop from Fluent 2014
CSS
13
star
24

cors-talk-example

Ruby
12
star
25

needy-controllers

Include stylesheets and scripts in a before_filter-esque fashion, and set up simple memoized methods for record fetches.
Ruby
12
star
26

browserized-styles

Automatic inclusion of browser and OS-specific stylesheets with a simple naming convention.
Ruby
11
star
27

bootstrap-polymer

An implementation of Bootstrap 3 components in Polymer.
JavaScript
10
star
28

polishing-rubies

Polishing Rubies: A Guide to Ruby Open Source Development
9
star
29

omniauth-box

Box.net strategy for OmniAuth
Ruby
9
star
30

railsconf-tweetups

Twitter app to see who's going to what at RailsConf!
Ruby
9
star
31

twitterdispatch

A simple Twitter API wrapper that gets out of your way.
6
star
32

commitbit

A web-based tool to help abandoned GitHub repos transfer to new maintainers.
CSS
6
star
33

present-future-of-oauth

Slides and code for "The Present Future of OAuth" given at RailsConf 2010.
Ruby
5
star
34

pictle

Drawing with Wordles
TypeScript
5
star
35

hooktastic

Merb-based application to make webhooks super-easy.
Ruby
5
star
36

jquery-pageselect

A jQuery plugin to handle selection of text on the page (not in a textarea).
4
star
37

jquery.navigable

A jQuery plugin for simple keyboard navigation.
JavaScript
4
star
38

css3-for-sass

A set of mixins to apply CSS3 rules to SASS.
4
star
39

cors-talk

JavaScript
4
star
40

bleightiful

The WordPress theme I hastily crafted for my personal site.
PHP
3
star
41

wedding-website

My wedding website!
JavaScript
2
star
42

rails-is-the-new-rails

Slides for a talk given at Ruby Midwest 2011
JavaScript
2
star
43

open-source-marketing

JavaScript
2
star
44

partay

Source for Partay games (for now, just Phraseology).
TypeScript
2
star
45

nodelin

Noodlin' around with Node.js to prep for the KO
JavaScript
2
star
46

callable-run-demo

HTML
2
star
47

schemer

Create JSON Schemas from Go data structures
Go
2
star
48

tinypage

A simple demo of the Firebase Hosting REST API.
JavaScript
2
star
49

omniauth-from-the-ground-up

OmniAuth: From the Ground Up Talk Content
Ruby
2
star
50

codemirror-element

A drop-in web component for source code editing. Utilizes the CodeMirror library.
HTML
2
star
51

dotfiles

Vim Script
1
star
52

puppetry

Ruby
1
star
53

i-am-open-source

I Am Open Source And So Can You (Talk)
1
star
54

let-us-lunch

Ruby
1
star
55

fiberthecrossroads.org

JavaScript
1
star