Sha256: 9c64add2b56396f8206d1125f4f7c7a0eb99adc932c98344a85ff89be49cad24

Contents?: true

Size: 1.19 KB

Versions: 26

Compression:

Stored size: 1.19 KB

Contents

module Locomotive
  module Routing
    class DefaultConstraint

      def self.matches?(request)
        if Locomotive.config.multi_sites?
          domain, subdomain = domain_and_subdomain(request)
          subdomain = 'www' if subdomain.blank?

          domain == Locomotive.config.domain && Locomotive.config.reserved_subdomains.include?(subdomain)
        else
          false
        end
      end

      # see actionpack/lib/action_dispatch/http/url.rb for more information
      def self.domain_and_subdomain(request)
        [extract_domain(request), extract_subdomain(request)]
      end

      def self.extract_domain(request, tld_length = 1)
        return nil unless named_host?(request.host)
        request.host.split('.').last(1 + tld_length).join('.')
      end

      def self.extract_subdomain(request, tld_length = 1)
        subdomains(request, tld_length).join('.')
      end

      def self.named_host?(host)
        !(host.nil? || /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.match(host))
      end

      def self.subdomains(request, tld_length = 1)
        return [] unless named_host?(request.host)
        parts = request.host.split('.')
        parts[0..-(tld_length+2)]
      end

    end
  end
end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
locomotive_cms-2.5.7 lib/locomotive/routing/default_constraint.rb
locomotive_cms-2.5.6 lib/locomotive/routing/default_constraint.rb
locomotive_cms-2.5.6.rc2 lib/locomotive/routing/default_constraint.rb
locomotive_cms-2.5.6.rc1 lib/locomotive/routing/default_constraint.rb
locomotive_cms-2.5.5 lib/locomotive/routing/default_constraint.rb
locomotive_cms-2.5.4 lib/locomotive/routing/default_constraint.rb
locomotive_cms-2.5.3 lib/locomotive/routing/default_constraint.rb
locomotive_cms-2.5.2 lib/locomotive/routing/default_constraint.rb
locomotive_cms-2.5.1 lib/locomotive/routing/default_constraint.rb
locomotive_cms-2.5.0 lib/locomotive/routing/default_constraint.rb
locomotive_cms-2.5.0.rc3 lib/locomotive/routing/default_constraint.rb
locomotive_cms-2.5.0.rc2 lib/locomotive/routing/default_constraint.rb
locomotive_cms-2.5.0.rc1 lib/locomotive/routing/default_constraint.rb
locomotive_cms-2.4.1 lib/locomotive/routing/default_constraint.rb
locomotive_cms-2.4.0 lib/locomotive/routing/default_constraint.rb
locomotive_cms-2.3.1 lib/locomotive/routing/default_constraint.rb
locomotive_cms-2.3.0 lib/locomotive/routing/default_constraint.rb
locomotive_cms-2.2.3 lib/locomotive/routing/default_constraint.rb
locomotive_cms-2.2.2 lib/locomotive/routing/default_constraint.rb
locomotive_cms-2.2.1 lib/locomotive/routing/default_constraint.rb