Class: DevToolbar::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/dev_toolbar/middleware.rb

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Middleware

Returns a new instance of Middleware.



3
4
5
# File 'lib/dev_toolbar/middleware.rb', line 3

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



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
# File 'lib/dev_toolbar/middleware.rb', line 7

def call(env)
  status, headers, response = @app.call(env)

  if Rails.env.development? && headers["Content-Type"]&.include?("text/html")
    response_body = response.body
    toolbar_html = "      <div id=\"dev-toolbar\">\n        <div id=\"dev-toolbar-button\">\n          <a id=\"dev-toolbar-toggle\">\u{1F6E0}\uFE0F</a>\n        </div>\n        <div id=\"dev-toolbar-links\" class=\"hidden\">\n          \#{toolbar_links}\n        </div>\n      </div>\n      <style>\n        #dev-toolbar {\n          position: fixed;\n          right: 0;\n          top: 50vh;\n          transform: translateY(-50%);\n          background-color: #f0f0f0;\n          border: 1px solid #ccc;\n          z-index: 1000;\n          display: flex;\n          flex-direction: column;\n          align-items: center;\n          font-family: -apple-system, BlinkMacSystemFont, avenir next, avenir, segoe ui, helvetica neue, helvetica, Cantarell, Ubuntu, roboto, noto, arial, sans-serif;\n          color: #808080;\n        }\n    \n        #dev-toolbar-toggle {\n          font-size: 2em;\n          border: none;\n          cursor: pointer;\n          line-height: 1.5;\n          padding: 0 10px;\n          text-decoration: none;\n        }\n    \n        #dev-toolbar-links {\n          display: flex;\n          flex-direction: column;\n        }\n\n        .dev-toolbar-link {\n          padding: 5px 10px;\n          border-bottom: 1px #f0f0f0 solid;\n          color: #808080;\n          text-decoration: none;\n          background-color: white;\n        }\n    \n        #dev-toolbar-links.hidden {\n          display: none;\n        }\n      </style>\n      <script>\n        document.getElementById('dev-toolbar-toggle').addEventListener('click', function() {\n          var links = document.getElementById('dev-toolbar-links');\n          links.classList.toggle('hidden');\n        });\n      </script>\n    HTML\n\n    response_body.sub!('</body>', \"\#{toolbar_html}</body>\")\n    headers[\"Content-Length\"] = response_body.bytesize.to_s\n\n    response = [response_body]\n  end\n\n  [status, headers, response]\nend\n"