Module: Vhost::ApplicationControllerExtensions

Defined in:
lib/vhost/application_controller_extensions.rb

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



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
# File 'lib/vhost/application_controller_extensions.rb', line 2

def self.included(base)
  base.class_eval {
    prepend_before_filter :redirect_to_primary_site

    helper_method :primary_site_url
    
    def redirect_to_primary_site
      if VhostExtension.REDIRECT_TO_PRIMARY_SITE
        site = current_site
        return if site.nil? || site.hostname.include?("*")
        primary_host = site.hostnames.first.domain
        redirect_to(primary_site_url + request.request_uri) if request.host != primary_host
      end
    end

    def primary_site_url
        site = current_site
        return nil if site.nil? || site.hostnames.map(&:domain).include?("*")

        # Rebuild the current URL. Check if it matches the URL of the
        # primary site and redirect if it does not.
        prefix = request.ssl? ? "https://" : "http://"
        host = request.host
        port = request.port_string

        # Primary site is the first site
        primary_host = site.hostnames.first.domain
        
        # Return the concatenation
        prefix+primary_host+port
    end
  }
end