ref: master
app/models/doc_section.rb
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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
class DocSection < DocItem def self.root_dir @root_dir ||= Rails.root.join('doc', 'noosfero') end def items @items ||= load_items end def find(id) topic = items.find {|item| item.id == id } if topic topic else raise DocItem::NotFound end end def self.all(language = 'en', force = false) if force @all = nil end @all ||= {} @all[language] ||= load_dirs(language) end def self.find(id, language = 'en', force = false) if id.blank? root(language) else section = all(language, force).find {|item| item.id == id } if section section else raise DocItem::NotFound end end end def self.root(language = 'en') @root ||= {} @root[language] ||= load(root_dir, language) end private attr_accessor :directory def self.load_dirs(language) Dir.glob(File.join(root_dir, '*')).select {|item| File.directory?(item) }.map do |dir| load(dir, language) end end def self.load(dir, language) index = DocTopic.loadfile(self._find_topic(dir, 'index', language)) toc = DocTopic.loadfile(self._find_topic(dir, 'toc', language)) new(:id => File.basename(dir), :title => index.title, :text => index.text + toc.text, :language => language, :directory => dir) end def self._find_topic(dir, id, language) language_suffix = _language_suffix(language) [ File.join(dir, "#{id}#{language_suffix}.xhtml"), File.join(dir, "#{id}.en.xhtml") ].find {|file| File.exist?(file) } || raise(DocItem::NotFound) end def load_items if directory language_suffix = self.class._language_suffix(language) Dir.glob(File.join(directory, "*.en.xhtml")).map do |file| # extract the available id's from the English versions File.basename(file).sub(/\.en.xhtml$/, '') end.map do |id| # load a translation, if available, or the original English topic DocTopic.loadfile(self.class._find_topic(directory, id, language)) end else [] end end def self._language_suffix(language) (!language || language == 'en') ? '' : ('.' + language) end end |