theme.rb
上传用户:netsea168
上传日期:2022-07-22
资源大小:4652k
文件大小:1k
源码类别:

Ajax

开发平台:

Others

  1. class Theme
  2.   cattr_accessor :cache_theme_lookup
  3.   @@cache_theme_lookup = false
  4.   attr_accessor :name, :path, :description_html
  5.   def initialize(name, path)
  6.     @name, @path = name, path
  7.   end
  8.   def layout(action=:default)
  9.     if action.to_s == 'view_page'
  10.       if File.exists? "#{RAILS_ROOT}/themes/#{name}/layouts/pages.html.erb"
  11.         return "#{RAILS_ROOT}/themes/#{name}/layouts/pages.html.erb"
  12.       end
  13.     end
  14.     "#{RAILS_ROOT}/themes/#{name}/layouts/default.html.erb"
  15.   end
  16.   def description
  17.     File.read("#{path}/about.markdown") rescue "### #{name}"
  18.   end
  19.   # Find a theme, given the theme name
  20.   def self.find(name)
  21.     self.new(name,theme_path(name))
  22.   end
  23.   def self.themes_root
  24.     RAILS_ROOT + "/themes"
  25.   end
  26.   def self.theme_path(name)
  27.     themes_root + "/" + name
  28.   end
  29.   def self.theme_from_path(path)
  30.     name = path.scan(/[-w]+$/i).flatten.first
  31.     self.new(name, path)
  32.   end
  33.   def self.find_all
  34.     installed_themes.inject([]) do |array, path|
  35.       array << theme_from_path(path)
  36.     end
  37.   end
  38.   def self.installed_themes
  39.     cache_theme_lookup ? @theme_cache ||= search_theme_directory : search_theme_directory
  40.   end
  41.   def self.search_theme_directory
  42.     glob = "#{themes_root}/[a-zA-Z0-9]*"
  43.     Dir.glob(glob).select do |file|
  44.       File.readable?("#{file}/about.markdown")
  45.     end.compact
  46.   end
  47. end