Class: Publinator::DomainName

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/publinator/domain_name.rb

Overview

Ideas:

* after_create adds link in ~/.pow in development

Class Method Summary collapse

Class Method Details

.get_by_domain_name(request) ⇒ Domain

lookup site based on the request

Parameters:

  • (Request)

Returns:

  • (Domain)


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
# File 'app/models/publinator/domain_name.rb', line 12

def self.get_by_domain_name(request)
  if request.env['x-forwarded-for']
    origin = request.env['x-forwarded-for'].to_s
    domain_array = origin.split('.')
    domain_array_length = origin.length
    if domain_array.length == 2
      request_subdomain = ""
      request_domain = origin
    elsif domain_array.length > 3
      request_subdomain = domain_array[domain_array_length - 3]
      request_domain = "#{domain_array[domain_array_length - 2]}.#{domain_array[domain_array_length - 1]}"
    end
  else
    request_subdomain = request.subdomain
    request_domain = request.domain
  end
  if request_domain.present?
    if request_subdomain.blank? || request_subdomain == 'www'
      @domain = self.find(:first, :conditions => ["name = ? AND (subdomain is null or subdomain = '')", request_domain.downcase])
    elsif request_subdomain.present? && request_subdomain != 'www'
      @domain = self.find(:first, :conditions => ["name = ? AND subdomain = ?", request_domain.downcase, request_subdomain.downcase])
    else
      @domain = self.find(:first, :conditions => ["name = ? AND subdomain = 'www' AND anchor = ?", request_domain.downcase, true])
    end
  else
    @domain = Site.default.domain_names.detect { |d| d.default }
  end
  return @domain
end