Class: Site

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

Overview

The site class includes - in find_for_host - some key retrieval and creation logic that is called from ApplicationController to set the current site context. Otherwise it’s just another Trusty data model.

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.severalObject

Returns the value of attribute several.



13
14
15
# File 'app/models/site.rb', line 13

def several
  @several
end

Class Method Details

.catchallObject

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



33
34
35
36
37
38
39
40
# File 'app/models/site.rb', line 33

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.



27
28
29
# File 'app/models/site.rb', line 27

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



17
18
19
20
21
22
23
# File 'app/models/site.rb', line 17

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)


44
45
46
# File 'app/models/site.rb', line 44

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

Instance Method Details

#create_homepageObject



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'app/models/site.rb', line 70

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

#dev_url(path = '/') ⇒ Object

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



65
66
67
68
# File 'app/models/site.rb', line 65

def dev_url(path = '/')
  uri = URI.join("http://#{TrustyCms::Config['dev.host'] || 'dev'}.#{base_domain}", path)
  uri.to_s
end

#reload_routesObject



84
85
86
# File 'app/models/site.rb', line 84

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.



58
59
60
61
# File 'app/models/site.rb', line 58

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