module Templates class TemplatesController < ApplicationController unloadable def index render :json => collect_templates end def mtime render :text => last_modify end protected def collect_templates app_templates = { :mtime => last_modify } Dir.new("#{Rails.root}/app/controllers").entries.each do |controller_file_name| if controller_file_name =~ /_controller/ controller_name = controller_file_name.camelize.gsub('.rb', '') controller = controller_name.constantize templates_hash = {} unless controller.templates_options.nil? if controller.templates_options[:layout] templates_hash[:layout] = render_to_string(:file => "layouts/#{controller.controller_name}", :layout => false) end if controller.templates_options[:partials].count > 0 templates_hash[:partials] = {} controller.templates_options[:partials].each do |partial| templates_hash[:partials][partial] = render_to_string(:file => "#{controller.controller_name}/_#{partial}", :layout => false) end end end if not controller.templates_list.nil? and controller.templates_list.count > 0 templates_hash[:templates] = {} controller.templates_list.each do |template| templates_hash[:templates][template] = render_to_string(:file => "#{controller.controller_name}/#{template}", :layout => false) end end app_templates[controller.controller_name] = templates_hash unless templates_hash.empty? end end app_templates end def last_modify # Watched files (and dirs) list watched_files = %w[ app/views app/helpers config/locales config/routes.rb ].map { |f| [Rails.root, f].join('/') } stat_flags = if RUBY_PLATFORM =~ /darwin/ # Mac OS X '-f %m' else # Linux (it may work on Windows, but in fact I'm really don't care about this crap) '--format=%Y' end `stat #{stat_flags} $(ls -t $(find #{ watched_files.join(' ') } -type f) | head -n 1)`.strip end end end