info.rb 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. require "cgi"
  2. module Rails
  3. module Info
  4. mattr_accessor :properties
  5. class << (@@properties = [])
  6. def names
  7. map {|val| val.first }
  8. end
  9. def value_for(property_name)
  10. if property = assoc(property_name)
  11. property.last
  12. end
  13. end
  14. end
  15. class << self #:nodoc:
  16. def property(name, value = nil)
  17. value ||= yield
  18. properties << [name, value] if value
  19. rescue Exception
  20. end
  21. def frameworks
  22. %w( active_record action_pack active_resource action_mailer active_support )
  23. end
  24. def framework_version(framework)
  25. if Object.const_defined?(framework.classify)
  26. require "#{framework}/version"
  27. "#{framework.classify}::VERSION::STRING".constantize
  28. end
  29. end
  30. def to_s
  31. column_width = properties.names.map {|name| name.length}.max
  32. info = properties.map do |name, value|
  33. value = value.join(", ") if value.is_a?(Array)
  34. "%-#{column_width}s %s" % [name, value]
  35. end
  36. info.unshift "About your application's environment"
  37. info * "\n"
  38. end
  39. alias inspect to_s
  40. def to_html
  41. (table = '<table>').tap do
  42. properties.each do |(name, value)|
  43. table << %(<tr><td class="name">#{CGI.escapeHTML(name.to_s)}</td>)
  44. formatted_value = if value.kind_of?(Array)
  45. "<ul>" + value.map { |v| "<li>#{CGI.escapeHTML(v.to_s)}</li>" }.join + "</ul>"
  46. else
  47. CGI.escapeHTML(value.to_s)
  48. end
  49. table << %(<td class="value">#{formatted_value}</td></tr>)
  50. end
  51. table << '</table>'
  52. end
  53. end
  54. end
  55. # The Ruby version and platform, e.g. "1.8.2 (powerpc-darwin8.2.0)".
  56. property 'Ruby version', "#{RUBY_VERSION} (#{RUBY_PLATFORM})"
  57. # The RubyGems version, if it's installed.
  58. property 'RubyGems version' do
  59. Gem::RubyGemsVersion
  60. end
  61. property 'Rack version' do
  62. ::Rack.release
  63. end
  64. # The Rails version.
  65. property 'Rails version' do
  66. Rails::VERSION::STRING
  67. end
  68. property 'JavaScript Runtime' do
  69. ExecJS.runtime.name
  70. end
  71. # Versions of each Rails framework (Active Record, Action Pack,
  72. # Active Resource, Action Mailer, and Active Support).
  73. frameworks.each do |framework|
  74. property "#{framework.titlecase} version" do
  75. framework_version(framework)
  76. end
  77. end
  78. property 'Middleware' do
  79. Rails.configuration.middleware.map(&:inspect)
  80. end
  81. # The application's location on the filesystem.
  82. property 'Application root' do
  83. File.expand_path(Rails.root)
  84. end
  85. # The current Rails environment (development, test, or production).
  86. property 'Environment' do
  87. Rails.env
  88. end
  89. # The name of the database adapter for the current environment.
  90. property 'Database adapter' do
  91. ActiveRecord::Base.configurations[Rails.env]['adapter']
  92. end
  93. property 'Database schema version' do
  94. ActiveRecord::Migrator.current_version rescue nil
  95. end
  96. end
  97. end