Class: Site

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

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.severalObject

Returns the value of attribute several.



16
17
18
# File 'app/models/site.rb', line 16

def several
  @several
end

Class Method Details

.catchallObject

If none is found, we are probably brand new, so a workable default site is created.



36
37
38
39
40
41
42
43
# File 'app/models/site.rb', line 36

def catchall
  create({
           domain: '',
           name: 'default_site',
           base_domain: 'localhost',
           homepage: Page.find_by_parent_id(nil),
         })
end

.defaultObject

Site.default returns the the first site it can find with an empty domain pattern.



30
31
32
# File 'app/models/site.rb', line 30

def default
  find_by_domain('') || find_by_domain(nil) || catchall
end

.find_for_host(hostname = '') ⇒ Object

I’ve added one or two sql queries here for the sake of a separate default method



20
21
22
23
24
25
26
# File 'app/models/site.rb', line 20

def find_for_host(hostname = '')
  return default if hostname.blank?

  sites = includes(:homepage).where('domain IS NOT NULL')
  site = sites.find { |site| hostname == site.base_domain || hostname =~ Regexp.compile(site.domain) }
  site || default
end

.several?Boolean

Returns true if more than one site is present. This is normally only used to make interface decisions, eg whether to show the site-chooser dropdown.

Returns:

  • (Boolean)


47
48
49
# File 'app/models/site.rb', line 47

def several?
  several = (count > 1) if several.nil?
end

Instance Method Details

#create_homepageObject



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'app/models/site.rb', line 66

def create_homepage
  if homepage_id.blank?
    self.homepage = build_homepage(title: "#{name} Homepage",
                                   slug: name.to_slug.to_s, breadcrumb: 'Home')
    default_status = TrustyCms::Config['defaults.page.status']
    homepage.status = Status[default_status] if default_status
    default_parts = TrustyCms::Config['defaults.page.parts'].to_s.strip.split(/\s*,\s*/)
    default_parts.each do |name|
      homepage.parts << PagePart.new(name: name, filter_id: TrustyCms::Config['defaults.page.filter'])
    end
    save
  end
end

#reload_routesObject



80
81
82
# File 'app/models/site.rb', line 80

def reload_routes
  TrustyCms::Application.reload_routes!
end

#url(path = '/') ⇒ Object

Returns the fully specified web address for the supplied path, or the root of this site if no path is given.



61
62
63
64
# File 'app/models/site.rb', line 61

def url(path = '/')
  uri = URI.join("http://#{base_domain}", path)
  uri.to_s
end