ref: master
app/models/doc_item.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 |
class DocItem class NotFound < Exception; end attr_accessor :id, :title, :text, :language, :order def initialize(attrs = {}) attrs.each do |name,value| self.send("#{name}=", value) end self.language ||= 'en' end def html(theme = nil) text.gsub(/<img src="([^"]+\.en\.png)"/) do |match| path = $1 translation = find_image_replacement(path, theme) if translation "<img src=\"#{translation}\"" else match end end.html_safe end private def find_image_replacement(image, theme) translation = image.sub(/\.en\.png$/, '.' + language + '.png') search_path = [ translation ] if theme search_path.unshift("/designs/themes/#{theme}#{translation}") # higher priority search_path.push("/designs/themes/#{theme}#{image}") # lower priority end search_path.find {|file| File.exist?(Rails.root.join('public', file[1..-1])) } end end |