afimb/chouette2

View on GitHub
app/models/help_page.rb

Summary

Maintainability
A
1 hr
Test Coverage
#encoding: utf-8
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 = $' # cryptic name for $POSTMATCH, but since V2.2
      self.data.merge! YAML.load($1)
    end

    # workaround for special chars
    self.content = self.content.gsub('é','é')
    self.content = self.content.gsub('è','è')
    self.content = self.content.gsub('à','à')
    self.content = self.content.gsub('ù','ù')
    self.content = self.content.gsub('É','É')
    self.content = self.content.gsub('È','È')
    self.content = self.content.gsub('Ê','Ê')
    self.content = self.content.gsub('À','À')
    self.content = self.content.gsub('Ù','Ù')
    self.content = self.content.gsub('â','â')
    self.content = self.content.gsub('ê','ê')
    self.content = self.content.gsub('ô','ô')
    self.content = self.content.gsub('î','î')
    self.content = self.content.gsub('û','û')
    self.content = self.content.gsub('ë','&eumlc;')
    self.content = self.content.gsub('ï','ï')
    self.content = self.content.gsub('ç','ç')
    self.content = self.content.gsub('oe','œ')
    self.content = self.content.gsub('<<','&laquo;')
    self.content = self.content.gsub('«','&laquo;')
    self.content = self.content.gsub('>>','&raquo;')
    self.content = self.content.gsub('»','&raquo;')
    self.content = self.content.gsub('°','&ordm;')
    self.content = self.content.gsub('’',"'")
    self.content = self.content.gsub(' '," ")
  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