-
Notifications
You must be signed in to change notification settings - Fork 398
/
railtie.rb
141 lines (115 loc) · 4.72 KB
/
railtie.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# frozen_string_literal: true
require 'fileutils'
module Rack::MiniProfilerRails
# call direct if needed to do a defer init
def self.initialize!(app)
raise "MiniProfilerRails initialized twice. Set `require: false' for rack-mini-profiler in your Gemfile" if defined?(@already_initialized) && @already_initialized
c = Rack::MiniProfiler.config
# By default, only show the MiniProfiler in development mode.
# To use the MiniProfiler in production, call Rack::MiniProfiler.authorize_request
# from a hook in your ApplicationController
#
# Example:
# before_action { Rack::MiniProfiler.authorize_request if current_user.is_admin? }
#
# NOTE: this must be set here with = and not ||=
# The out of the box default is "true"
c.pre_authorize_cb = lambda { |env|
!Rails.env.test?
}
c.skip_paths ||= []
if serves_static_assets?(app)
c.skip_paths << app.config.assets.prefix
end
unless Rails.env.development? || Rails.env.test?
c.authorization_mode = :whitelist
end
if Rails.logger
c.logger = Rails.logger
end
# The file store is just so much less flaky
# If the user has not changed from the default memory store then switch to the file store, otherwise keep what the user set
if c.storage == Rack::MiniProfiler::MemoryStore && c.storage_options.nil?
base_path = Rails.application.config.paths['tmp'].first rescue "#{Rails.root}/tmp"
tmp = base_path + '/miniprofiler'
c.storage_options = {:path => tmp}
c.storage = Rack::MiniProfiler::FileStore
end
# Quiet the SQL stack traces
c.backtrace_remove = Rails.root.to_s + "/"
c.backtrace_includes = [/^\/?(app|config|lib|test)/]
c.skip_schema_queries = (Rails.env.development? || Rails.env.test?)
# Install the Middleware
app.middleware.insert(0, Rack::MiniProfiler)
# Attach to various Rails methods
ActiveSupport.on_load(:action_controller) do
::Rack::MiniProfiler.profile_method(ActionController::Base, :process) {|action| "Executing action: #{action}"}
end
ActiveSupport.on_load(:action_view) do
::Rack::MiniProfiler.profile_method(ActionView::Template, :render) {|x,y| "Rendering: #{@virtual_path}"}
end
@already_initialized = true
end
def self.serves_static_assets?(app)
config = app.config
if !config.respond_to?(:assets) || !config.assets.respond_to?(:prefix)
return false
end
if ::Rails.version >= "5.0.0"
::Rails.configuration.public_file_server.enabled
elsif ::Rails.version >= "4.2.0"
::Rails.configuration.serve_static_files
else
::Rails.configuration.serve_static_assets
end
end
class Railtie < ::Rails::Railtie
initializer "rack_mini_profiler.configure_rails_initialization" do |app|
Rack::MiniProfilerRails.initialize!(app)
end
# Suppress compression when Rack::Deflater is lower in the middleware
# stack than Rack::MiniProfiler
config.after_initialize do |app|
middlewares = app.middleware.middlewares
if Rack::MiniProfiler.config.suppress_encoding.nil? &&
middlewares.include?(Rack::Deflater) &&
middlewares.index(Rack::Deflater) > middlewares.index(Rack::MiniProfiler)
Rack::MiniProfiler.config.suppress_encoding = true
end
end
# TODO: Implement something better here
# config.after_initialize do
#
# class ::ActionView::Helpers::AssetTagHelper::JavascriptIncludeTag
# alias_method :asset_tag_orig, :asset_tag
# def asset_tag(source,options)
# current = Rack::MiniProfiler.current
# return asset_tag_orig(source,options) unless current
# wrapped = ""
# unless current.mpt_init
# current.mpt_init = true
# wrapped << Rack::MiniProfiler::ClientTimerStruct.init_instrumentation
# end
# name = source.split('/')[-1]
# wrapped << Rack::MiniProfiler::ClientTimerStruct.instrument(name, asset_tag_orig(source,options)).html_safe
# wrapped
# end
# end
# class ::ActionView::Helpers::AssetTagHelper::StylesheetIncludeTag
# alias_method :asset_tag_orig, :asset_tag
# def asset_tag(source,options)
# current = Rack::MiniProfiler.current
# return asset_tag_orig(source,options) unless current
# wrapped = ""
# unless current.mpt_init
# current.mpt_init = true
# wrapped << Rack::MiniProfiler::ClientTimerStruct.init_instrumentation
# end
# name = source.split('/')[-1]
# wrapped << Rack::MiniProfiler::ClientTimerStruct.instrument(name, asset_tag_orig(source,options)).html_safe
# wrapped
# end
# end
# end
end
end