public
Description: A new plugin approach to attempting to solve the usage of subdomains in linking and routing in Rails projects.
Homepage: http://rdoc.info/projects/mbleigh/subdomain-fu
Clone URL: git://github.com/mbleigh/subdomain-fu.git
subdomain-fu / lib / subdomain_fu / routing_extensions.rb
100644 62 lines (54 sloc) 2.635 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# Thanks to Jamis Buck for ideas on this stuff
# http://weblog.jamisbuck.org/2006/10/26/monkey-patching-rails-extending-routes-2
# This is not yet a working part of SubdomainFu.
 
module SubdomainFu
  module RouteExtensions
    def self.included(base)
      base.alias_method_chain :recognition_conditions, :subdomain
    end
 
    def recognition_conditions_with_subdomain
      result = recognition_conditions_without_subdomain
      result << "conditions[:subdomain] === env[:subdomain]" if conditions[:subdomain] && conditions[:subdomain] != true && conditions[:subdomain] != false
      result << "SubdomainFu.has_subdomain?(env[:subdomain])" if conditions[:subdomain] == true
      result << "!SubdomainFu.has_subdomain?(env[:subdomain])" if conditions[:subdomain] == false
      result
    end
  end
 
  module RouteSetExtensions
    def self.included(base)
      base.alias_method_chain :extract_request_environment, :subdomain
    end
 
    def extract_request_environment_with_subdomain(request)
      env = extract_request_environment_without_subdomain(request)
      env.merge(:host => request.host, :domain => request.domain, :subdomain => SubdomainFu.subdomain_from(request.host))
    end
  end
 
  module MapperExtensions
    def quick_map(has_unless, *args, &block)
      options = args.find{|a| a.is_a?(Hash)}
      namespace_str = options ? options.delete(:namespace).to_s : args.join('_or_')
      namespace_str += '_' unless namespace_str.blank?
      mapped_exp = args.map(&:to_s).join('|')
      conditions_hash = { :subdomain => ( has_unless ? /[^(#{mapped_exp})]/ : /(#{mapped_exp})/) }
      with_options(:conditions => conditions_hash, :name_prefix => namespace_str, &block)
    end
    # Adds methods to Mapper to apply an options with a method. Example
    # map.subdomain :blog { |blog| blog.resources :pages }
    # or
    # map.unless_subdomain :blog { |not_blog| not_blog.resources :people }
    def subdomain(*args, &block)
      quick_map(false, *args, &block)
    end
    def unless_subdomain(*args, &block)
      quick_map(true, *args, &block)
    end
  end
end
 
ActionController::Routing::RouteSet::Mapper.send :include, SubdomainFu::MapperExtensions
ActionController::Routing::RouteSet.send :include, SubdomainFu::RouteSetExtensions
ActionController::Routing::Route.send :include, SubdomainFu::RouteExtensions
 
# UrlRewriter::RESERVED_OPTIONS is only available in Rails >= 2.2
# http://www.portallabs.com/blog/2008/12/02/fixing-subdomain_fu-with-named-routes-rails-22/
if Rails::VERSION::MAJOR >= 2 and Rails::VERSION::MINOR >= 2
  ActionController::UrlRewriter::RESERVED_OPTIONS << :subdomain
end