在我的 Rails 4 应用程序中,我创建了这两个辅助函数:
def link_to_switch_off_miniprofiler
link_to "OFF", "?pp=disable"
end
def link_to_switch_on_miniprofiler
link_to "ON", "?pp=enable"
end
有什么方法可以检测 MiniProfiler 当前是否启用或禁用,以便我可以将两个功能合二为一?
感谢您的帮助。
对于我的 Rails 5.2 应用程序来说,这仍然是一个当前问题。我最终根据
app/helpers/mini_profiler_helper.rb
的源代码添加了以下助手 (Rack::MiniProfiler
):
module MiniProfilerHelper
def profiling_enabled?
return false unless defined? Rack::MiniProfiler
!cookies[Rack::MiniProfiler::ClientSettings::COOKIE_NAME]&.split(',')&.include?('dp=t')
end
end
在我的
Gemfile
中,我将其限制为开发模式(bundle add GEM
建议的版本限制):
group :development do
gem 'rack-mini-profiler', '~> 3.1'
gem 'memory_profiler', '~> 1.0'
gem 'stackprof', '~> 0.2.26'
end
我的
config/initializers/mini_profiler.rb
包含:
Rack::MiniProfiler.config.position = 'bottom-left' if defined? Rack::MiniProfiler
这允许我将以下内容添加到“帮助”下拉菜单的底部(以精简格式):
- Rails.env.development?
li.divider role="separator"
li
- if profiling_enabled?
= link_to '/?pp=help', data: {turbolinks: false}
' Mini Profiler Help
- else
= link_to '/?pp=enable', data: {turbolinks: false}
' Enable Mini Profiler
li
= link_to '/rails/info/routes'
' Routes
注意:如果禁用分析,则不会显示帮助页面,因此您需要一个单独的链接来首先启用它。启用后,您可以通过帮助页面中的链接禁用它。我禁用 Turbolinks,以便在启用分析时重新加载分析器的 js。