aboutsummaryrefslogtreecommitdiffstats
path: root/app/models/help_page.rb
blob: 1207c5198e8e3ee239ce0509b155f5cecccd6baa (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
class HelpPage

  attr_accessor :slug, :content, :data

  def initialize(slug)
    @slug = slug
    @data = {}.with_indifferent_access
  end

  def filename
    "#{Rails.root}/app/views/help/#{slug}.textile"
  end

  def exists?
    File.exists? filename
  end

  def load
    self.content = File.read(filename)
    self.data ||= {}

    if self.content =~ /^(---\s*\n.*?\n?)^(---\s*$\n?)/m
      self.content = $POSTMATCH
      self.data.merge! YAML.load($1)
    end
  end

  def method_missing(method, *arguments)
    if arguments.empty? and data.has_key?(method)
      data[method]
    else
      super
    end
  end

  def self.find(slug)
    new(slug).tap do |page|
      if page.exists?
        page.load
      else
        raise ActiveRecord::RecordNotFound 
      end
    end
  end

end