Class: Platform::SiteManager

Inherits:
Object
  • Object
show all
Defined in:
app/models/platform/site_manager.rb

Constant Summary collapse

"© 2009-#{Date.today.year} Websitebakery"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(admin_group = "website_bakery", sites_location = "sites/") ⇒ SiteManager

Returns a new instance of SiteManager.



3
4
5
6
7
8
9
# File 'app/models/platform/site_manager.rb', line 3

def initialize(admin_group = "website_bakery", sites_location = "sites/")
  @auto_reloading = %w(development).include?(Rails.env)
  Rails.logger.info "Auto reloading configuration is #{@auto_reloading ? "on" : "off"}."
  @configurations = {}
  @admin_group    = admin_group
  @sites_location = sites_location
end

Instance Attribute Details

#admin_groupObject (readonly)

Returns the value of attribute admin_group.



11
12
13
# File 'app/models/platform/site_manager.rb', line 11

def admin_group
  @admin_group
end

#sites_locationObject (readonly)

Returns the value of attribute sites_location.



11
12
13
# File 'app/models/platform/site_manager.rb', line 11

def sites_location
  @sites_location
end

Class Method Details

.changelogObject



72
73
74
# File 'app/models/platform/site_manager.rb', line 72

def self.changelog
  @changelog ||= YAML::load_file(File.join([Rails.root, "doc", "changes.yml"]))["changes"]
end

.versionObject



76
77
78
# File 'app/models/platform/site_manager.rb', line 76

def self.version
  changelog.keys.sort.last
end

Instance Method Details

#auto_reload_site(*sites) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/models/platform/site_manager.rb', line 29

def auto_reload_site(*sites)
  sites.each do |site|
    initialize_site site, :location => sites_location unless c.has_key? site
    file_name = (c[site] || {})[:config_file]

    if File.exist?(file_name)
      read_time = File.new(file_name).mtime
      if c[site][:config_time].nil? or read_time > c[site][:config_time]
        c[site][:config]      = YAML::load_file(file_name)
        c[site][:config_time] = read_time
        load_site site

        Rails.logger.info "Reloading #{site}"
      end
    else
      Rails.logger.error "Configuration for #{site} (#{file_name}) can not be found"
      raise "Configuration for #{site} (#{file_name}) can not be found"
    end
  end
end

#collect_and_load_sites!Object



80
81
82
83
84
85
86
87
88
89
90
# File 'app/models/platform/site_manager.rb', line 80

def collect_and_load_sites!
  sites = []
  Dir.foreach(Rails.root + sites_location) do |site|
    site_root = Rails.root + sites_location + site
    if File.directory?(site_root) and !%w(. ..).include?(site)
      sites << site
      initialize_site(site, :location => sites_location)
    end
  end
  auto_reload_site *sites
end

#configuration_for(options) ⇒ Object

Raises:

  • (ArgumentError)


50
51
52
53
54
55
56
# File 'app/models/platform/site_manager.rb', line 50

def configuration_for options
  options.assert_valid_keys(:site, :host)
  raise ArgumentError.new("You must provide either :site or :host option") if !!options.has_key?(:site) == !!options.has_key?(:host)
  site = determine_site_by_hostname options[:host] if options[:host]
  site ||= options[:site]
  Platform::SiteConfiguration.new(self, site)
end

#determine_site_by_hostname(hostname) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
# File 'app/models/platform/site_manager.rb', line 58

def determine_site_by_hostname(hostname)
  @domain_cache ||= {}
  unless @domain_cache.has_key? hostname
    c.each do |site, settings|
      (get(site, "virtual_hosts") || []).each do |domain|
        @domain_cache[domain] = site
      end
    end
  end
  @domain_cache[hostname] or raise "No configuration found for hostname: #{hostname}"
end

#get(site, path) ⇒ Object



13
14
15
16
17
# File 'app/models/platform/site_manager.rb', line 13

def get(site, path)
  auto_reload_site site if @auto_reloading
  parts = "#{site}.#{path}".split "." # if path is string
  get_part site, parts, c[site][:config]
end

#get_part(site, parts, root) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'app/models/platform/site_manager.rb', line 19

def get_part(site, parts, root)
  element = parts.shift
  item    = root[element.to_s]
  return get_part site, parts, item unless parts.length.zero?
  Rails.logger.info "reading #{item.inspect} from config"
  item
rescue
  Rails.logger.error "can't read #{parts * "."}"
end