2017-08-14 13:08:09 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2009-05-16 15:28:25 -04:00
|
|
|
require "cgi"
|
|
|
|
|
2005-11-04 23:44:33 -05:00
|
|
|
module Rails
|
2016-12-20 17:16:17 -05:00
|
|
|
# This module helps build the runtime properties that are displayed in
|
|
|
|
# Rails::InfoController responses. These include the active Rails version,
|
|
|
|
# Ruby version, Rack version, and so on.
|
2005-11-04 23:44:33 -05:00
|
|
|
module Info
|
2017-05-31 05:16:20 -04:00
|
|
|
mattr_accessor :properties, default: []
|
|
|
|
|
|
|
|
class << @@properties
|
2005-11-05 09:30:47 -05:00
|
|
|
def names
|
2014-10-27 12:28:53 -04:00
|
|
|
map(&:first)
|
2005-11-05 09:30:47 -05:00
|
|
|
end
|
2007-12-18 20:18:45 -05:00
|
|
|
|
2005-11-05 09:30:47 -05:00
|
|
|
def value_for(property_name)
|
2007-12-18 23:21:02 -05:00
|
|
|
if property = assoc(property_name)
|
2007-12-18 20:18:45 -05:00
|
|
|
property.last
|
|
|
|
end
|
2005-11-05 09:30:47 -05:00
|
|
|
end
|
|
|
|
end
|
2007-12-18 20:18:45 -05:00
|
|
|
|
2005-11-04 23:44:33 -05:00
|
|
|
class << self #:nodoc:
|
|
|
|
def property(name, value = nil)
|
|
|
|
value ||= yield
|
2007-12-18 23:21:02 -05:00
|
|
|
properties << [name, value] if value
|
2005-11-04 23:44:33 -05:00
|
|
|
rescue Exception
|
|
|
|
end
|
|
|
|
|
2005-11-05 09:30:47 -05:00
|
|
|
def to_s
|
2014-10-27 12:28:53 -04:00
|
|
|
column_width = properties.names.map(&:length).max
|
2010-02-03 12:07:22 -05:00
|
|
|
info = properties.map do |name, value|
|
|
|
|
value = value.join(", ") if value.is_a?(Array)
|
|
|
|
"%-#{column_width}s %s" % [name, value]
|
|
|
|
end
|
|
|
|
info.unshift "About your application's environment"
|
|
|
|
info * "\n"
|
2005-11-05 09:30:47 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
alias inspect to_s
|
2007-12-18 23:21:02 -05:00
|
|
|
|
2005-11-07 23:26:34 -05:00
|
|
|
def to_html
|
2018-05-17 04:32:27 -04:00
|
|
|
(+"<table>").tap do |table|
|
2005-11-07 23:26:34 -05:00
|
|
|
properties.each do |(name, value)|
|
2006-03-29 10:11:47 -05:00
|
|
|
table << %(<tr><td class="name">#{CGI.escapeHTML(name.to_s)}</td>)
|
2009-04-02 12:54:52 -04:00
|
|
|
formatted_value = if value.kind_of?(Array)
|
2016-08-06 13:55:02 -04:00
|
|
|
"<ul>" + value.map { |v| "<li>#{CGI.escapeHTML(v.to_s)}</li>" }.join + "</ul>"
|
2016-08-07 17:41:00 -04:00
|
|
|
else
|
|
|
|
CGI.escapeHTML(value.to_s)
|
|
|
|
end
|
2009-04-02 12:54:52 -04:00
|
|
|
table << %(<td class="value">#{formatted_value}</td></tr>)
|
2005-11-07 23:26:34 -05:00
|
|
|
end
|
2016-08-06 13:15:47 -04:00
|
|
|
table << "</table>"
|
2005-11-07 23:26:34 -05:00
|
|
|
end
|
|
|
|
end
|
Add JSON support to rails properties route (`/rails/info/properties`).
Added json format, like this:
{
"Rails version": "6.0.0.alpha",
"Ruby version": "2.5.1-p57 (x86_64-darwin17)",
"RubyGems version": "2.7.6",
"Rack version": "2.0.6",
"JavaScript Runtime": "Node.js (V8)",
"Middleware": ["Rack::Sendfile", "ActionDispatch::Static", "ActionDispatch::Executor", "ActiveSupport::Cache::Strategy::LocalCache::Middleware", "Rack::Runtime", "Rack::MethodOverride", "ActionDispatch::RequestId", "ActionDispatch::RemoteIp", "Sprockets::Rails::QuietAssets", "Rails::Rack::Logger", "ActionDispatch::ShowExceptions", "WebConsole::Middleware", "ActionDispatch::DebugExceptions", "ActionDispatch::Reloader", "ActionDispatch::Callbacks", "ActiveRecord::Migration::CheckPending", "ActionDispatch::Cookies", "ActionDispatch::Session::CookieStore", "ActionDispatch::Flash", "ActionDispatch::ContentSecurityPolicy::Middleware", "Rack::Head", "Rack::ConditionalGet", "Rack::ETag", "Rack::TempfileReaper"],
"Application root": "/path/to/app",
"Environment": "development",
"Database adapter": "sqlite3",
"Database schema version": 0
}
2018-11-06 09:20:56 -05:00
|
|
|
|
|
|
|
def to_json
|
|
|
|
Hash[properties].to_json
|
|
|
|
end
|
2005-11-04 23:44:33 -05:00
|
|
|
end
|
|
|
|
|
2014-09-23 04:13:58 -04:00
|
|
|
# The Rails version.
|
2016-08-06 13:15:47 -04:00
|
|
|
property "Rails version" do
|
2014-09-23 04:13:58 -04:00
|
|
|
Rails.version.to_s
|
|
|
|
end
|
|
|
|
|
2013-07-03 12:56:04 -04:00
|
|
|
# The Ruby version and platform, e.g. "2.0.0-p247 (x86_64-darwin12.4.0)".
|
2016-08-06 13:15:47 -04:00
|
|
|
property "Ruby version" do
|
2013-07-03 09:50:42 -04:00
|
|
|
"#{RUBY_VERSION}-p#{RUBY_PATCHLEVEL} (#{RUBY_PLATFORM})"
|
|
|
|
end
|
2005-11-04 23:44:33 -05:00
|
|
|
|
|
|
|
# The RubyGems version, if it's installed.
|
2016-08-06 13:15:47 -04:00
|
|
|
property "RubyGems version" do
|
2005-11-04 23:44:33 -05:00
|
|
|
Gem::RubyGemsVersion
|
|
|
|
end
|
2007-12-18 23:21:02 -05:00
|
|
|
|
2016-08-06 13:15:47 -04:00
|
|
|
property "Rack version" do
|
2009-03-14 11:37:20 -04:00
|
|
|
::Rack.release
|
|
|
|
end
|
|
|
|
|
2016-08-06 13:15:47 -04:00
|
|
|
property "JavaScript Runtime" do
|
2011-06-27 05:59:23 -04:00
|
|
|
ExecJS.runtime.name
|
|
|
|
end
|
|
|
|
|
2016-08-06 13:15:47 -04:00
|
|
|
property "Middleware" do
|
2010-07-26 17:35:19 -04:00
|
|
|
Rails.configuration.middleware.map(&:inspect)
|
2009-04-02 12:54:52 -04:00
|
|
|
end
|
|
|
|
|
2005-11-05 09:30:47 -05:00
|
|
|
# The application's location on the filesystem.
|
2016-08-06 13:15:47 -04:00
|
|
|
property "Application root" do
|
2009-10-16 15:49:39 -04:00
|
|
|
File.expand_path(Rails.root)
|
2005-11-05 13:04:52 -05:00
|
|
|
end
|
|
|
|
|
2005-11-04 23:44:33 -05:00
|
|
|
# The current Rails environment (development, test, or production).
|
2016-08-06 13:15:47 -04:00
|
|
|
property "Environment" do
|
2010-01-12 08:11:47 -05:00
|
|
|
Rails.env
|
2005-11-04 23:44:33 -05:00
|
|
|
end
|
2007-12-18 23:21:02 -05:00
|
|
|
|
2005-11-04 23:44:33 -05:00
|
|
|
# The name of the database adapter for the current environment.
|
2016-08-06 13:15:47 -04:00
|
|
|
property "Database adapter" do
|
|
|
|
ActiveRecord::Base.configurations[Rails.env]["adapter"]
|
2005-11-04 23:44:33 -05:00
|
|
|
end
|
2006-03-05 16:38:51 -05:00
|
|
|
|
2016-08-06 13:15:47 -04:00
|
|
|
property "Database schema version" do
|
2018-01-10 10:25:13 -05:00
|
|
|
ActiveRecord::Base.connection.migration_context.current_version rescue nil
|
2006-03-05 16:38:51 -05:00
|
|
|
end
|
2005-11-04 23:44:33 -05:00
|
|
|
end
|
|
|
|
end
|