summaryrefslogtreecommitdiff
path: root/lib/site_mount_point.rb
blob: 75661bae114018fb03cba97d40fea6011909e5c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#
# A site can have many 'mount points' -- places in the site tree where different directories are inserted.
#
# At a minimum, every site needs a mount point for '/'
#
class SiteMountPoint

  attr_accessor :directory
  attr_accessor :relative_directory
  attr_accessor :path
  attr_accessor :options
  attr_accessor :menu_file
  attr_accessor :locales_dir
  attr_accessor :timestamp

  def initialize(site_config, directory_source, options={})
    @directory          = find_directory(site_config, directory_source)
    @path               = options[:path]
    @relative_directory = relative_dir_path(@directory)
    @menu_file          = file_path('menu.txt')
    @locales_dir        = file_path('locales')
    @options            = options
    reset_timestamp
  end

  def changed?
    File.mtime(@directory) > @timestamp
  end

  def reset_timestamp
    @timestamp = File.mtime(@directory)
  end

  private

  def file_path(file)
    path = File.join(@directory, file)
    if File.exists?(path)
      path
    else
      nil
    end
  end

  #
  # returns path relative to app/views
  #
  def relative_dir_path(directory)
    if Rails.root
      Pathname.new(directory).relative_path_from(Pathname.new(Rails.root + 'app/views')).to_s
    end
  end

  def find_directory(site_config, directory_source)
    directory = nil
    [directory_source].flatten.each do |dir_source|
      if dir_source.starts_with?('/')
        directory = dir_source
      else
        directory = File.expand_path(dir_source, File.dirname(site_config.file_path))
      end
      if File.directory?(directory)
        break
      end
    end
    if File.directory?(directory)
      return directory
    else
      raise 'FATAL ERROR: could find any of %s' % directory_source.join(', ')
    end
  end

end