Class: Comfy::Cms::Site

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/comfy/cms/site.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.find_site(host, path = nil) ⇒ Object

– Class Methods ———————————————————– returning the Comfy::Cms::Site instance based on host and path


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/models/comfy/cms/site.rb', line 36

def self.find_site(host, path = nil)
  return Comfy::Cms::Site.first if Comfy::Cms::Site.count == 1
  cms_site = nil

  public_cms_path = ComfortableMexicanSofa.configuration.public_cms_path
  if path && public_cms_path != "/"
    path = path.sub(%r{\A#{public_cms_path}}, "")
  end

  Comfy::Cms::Site.where(hostname: real_host_from_aliases(host)).each do |site|
    if site.path.blank?
      cms_site = site
    elsif "#{path.to_s.split('?')[0]}/" =~ %r{^/#{Regexp.escape(site.path.to_s)}/}
      cms_site = site
      break
    end
  end
  cms_site
end

.real_host_from_aliases(host) ⇒ Object


56
57
58
59
60
61
62
63
# File 'app/models/comfy/cms/site.rb', line 56

def self.real_host_from_aliases(host)
  if (aliases = ComfortableMexicanSofa.config.hostname_aliases)
    aliases.each do |alias_host, hosts|
      return alias_host if hosts.include?(host)
    end
  end
  host
end

Instance Method Details

#url(relative: false) ⇒ Object

– Instance Methods ——————————————————–


66
67
68
69
70
71
# File 'app/models/comfy/cms/site.rb', line 66

def url(relative: false)
  public_cms_path = ComfortableMexicanSofa.config.public_cms_path || "/"
  host = "//#{hostname}"
  path = ["/", public_cms_path, self.path].compact.join("/").squeeze("/").chomp("/")
  relative ? path.presence : [host, path].join
end