FEATURE: Allow assets to be precompiled with Sprockets (#456)

FEATURE: Allow assets to be precompiled with Sprockets (#456)

  • Allow assets to be precompiled with Sprockets

  • Don’t create rails engine unless user opts in

  • Update ASSET_VERSION

  • Add documentation

diff --git a/README.md b/README.md
index 0349ded..2aaa728 100644
--- a/README.md
+++ b/README.md
@@ -329,6 +329,15 @@ _Note:_ The GUID (`data-version` and the `?v=` parameter on the `src`) will chan
 #### Using MiniProfiler's built in route for apps without HTML responses
 MiniProfiler also ships with a `/rack-mini-profiler/requests` route that displays the speed badge on a blank HTML page. This can be useful when profiling an application that does not render HTML.
 
+#### Register MiniProfiler's assets in the Rails assets pipeline
+MiniProfiler can be configured so it registers its assets in the assets pipeline. To do that, you'll need to provide a lambda (or proc) to the `assets_url` config (see the below section). The callback will receive 3 arguments which are: `name` represents asset name (currently it's either `rack-mini-profiling.js` or `rack-mini-profiling.css`), `assets_version` is a 32 characters long hash of MiniProfiler's assets, and `env` which is the `env` object of the request. MiniProfiler expects the `assets_url` callback to return a URL from which the asset can be loaded (the return value will be used as a `href`/`src` attribute in the DOM). If the `assets_url` callback is not set (the default) or it returns a non-truthy value, MiniProfiler will fallback to loading assets from its own middleware (`/mini-profiler-resources/*`). The following callback should work for most applications:
+
+`‍``ruby
+Rack::MiniProfiler.config.assets_url = ->(name, version, env) {
+  ActionController::Base.helpers.asset_path(name)
+}
+`‍``
+
 ### Configuration Options
 
 You can set configuration options using the configuration accessor on `Rack::MiniProfiler`.
@@ -360,6 +369,7 @@ max_traces_to_show|20|Maximum number of mini profiler timing blocks to show on o
 html_container|`body`|The HTML container (as a jQuery selector) to inject the mini_profiler UI into
 show_total_sql_count|`false`|Displays the total number of SQL executions.
 enable_advanced_debugging_tools|`false`|Enables sensitive debugging tools that can be used via the UI. In production we recommend keeping this disabled as memory and environment debugging tools can expose contents of memory that may contain passwords.
+assets_url|`nil`|See the "Register MiniProfiler's assets in the Rails assets pipeline" section above.
 
 ### Using MiniProfiler with `Rack::Deflate` middleware
 
diff --git a/lib/html/includes.css b/lib/html/includes.css
index 62a19a0..3b28358 100644
--- a/lib/html/includes.css
+++ b/lib/html/includes.css
@@ -207,7 +207,8 @@
     top: 0px; }
     .profiler-results.profiler-top.profiler-left {
       left: 0px; }
-      .profiler-results.profiler-top.profiler-left.profiler-no-controls .profiler-totals, .profiler-results.profiler-top.profiler-left.profiler-no-controls .profiler-result:last-child .profiler-button,
+      .profiler-results.profiler-top.profiler-left.profiler-no-controls .profiler-totals,
+      .profiler-results.profiler-top.profiler-left.profiler-no-controls .profiler-result:last-child .profiler-button,
       .profiler-results.profiler-top.profiler-left .profiler-controls {
         -webkit-border-bottom-right-radius: 10px;
         -moz-border-radius-bottomright: 10px;
@@ -217,7 +218,8 @@
         border-right: 1px solid #888; }
     .profiler-results.profiler-top.profiler-right {
       right: 0px; }
-      .profiler-results.profiler-top.profiler-right.profiler-no-controls .profiler-totals, .profiler-results.profiler-top.profiler-right.profiler-no-controls .profiler-result:last-child .profiler-button,
+      .profiler-results.profiler-top.profiler-right.profiler-no-controls .profiler-totals,
+      .profiler-results.profiler-top.profiler-right.profiler-no-controls .profiler-result:last-child .profiler-button,
       .profiler-results.profiler-top.profiler-right .profiler-controls {
         -webkit-border-bottom-left-radius: 10px;
         -moz-border-radius-bottomleft: 10px;
@@ -229,7 +231,8 @@
     bottom: 0px; }
     .profiler-results.profiler-bottom.profiler-left {
       left: 0px; }
-      .profiler-results.profiler-bottom.profiler-left.profiler-no-controls .profiler-totals, .profiler-results.profiler-bottom.profiler-left.profiler-no-controls .profiler-result:first-child .profiler-button,
+      .profiler-results.profiler-bottom.profiler-left.profiler-no-controls .profiler-totals,
+      .profiler-results.profiler-bottom.profiler-left.profiler-no-controls .profiler-result:first-child .profiler-button,
       .profiler-results.profiler-bottom.profiler-left .profiler-controls {
         -webkit-border-top-right-radius: 10px;
         -moz-border-radius-topright: 10px;
@@ -239,7 +242,8 @@
         border-right: 1px solid #888; }
     .profiler-results.profiler-bottom.profiler-right {
       right: 0px; }
-      .profiler-results.profiler-bottom.profiler-right.profiler-no-controls .profiler-totals, .profiler-results.profiler-bottom.profiler-right.profiler-no-controls .profiler-result:first-child .profiler-button,
+      .profiler-results.profiler-bottom.profiler-right.profiler-no-controls .profiler-totals,
+      .profiler-results.profiler-bottom.profiler-right.profiler-no-controls .profiler-result:first-child .profiler-button,
       .profiler-results.profiler-bottom.profiler-right .profiler-controls {
         -webkit-border-bottom-top-radius: 10px;
         -moz-border-radius-topleft: 10px;
@@ -345,6 +349,7 @@
   @media print {
     .profiler-results {
       display: none; } }
+
 .profiler-queries-bg {
   z-index: 2147483642;
   display: none;
diff --git a/lib/html/includes.js b/lib/html/includes.js
index e5930d0..a9cc120 100644
--- a/lib/html/includes.js
+++ b/lib/html/includes.js
@@ -980,6 +980,7 @@ var MiniProfiler = (function() {
           script.getAttribute("data-start-hidden") === "true" ||
           sessionStorage["rack-mini-profiler-start-hidden"] === "true";
         var htmlContainer = script.getAttribute("data-html-container");
+        var cssUrl = script.getAttribute("data-css-url");
         return {
           ids: ids,
           path: path,
@@ -996,7 +997,8 @@ var MiniProfiler = (function() {
           toggleShortcut: toggleShortcut,
           startHidden: startHidden,
           collapseResults: collapseResults,
-          htmlContainer: htmlContainer
+          htmlContainer: htmlContainer,
+          cssUrl: cssUrl
         };
       })();
 
@@ -1056,7 +1058,7 @@ var MiniProfiler = (function() {
 
       var init = function init() {
         if (options.authorized) {
-          var url = options.path + "includes.css?v=" + options.version;
+          var url = options.cssUrl;
 
           if (document.createStyleSheet) {
             document.createStyleSheet(url);
@@ -1398,4 +1400,5 @@ var MiniProfiler = (function() {
   };
 })();
 
+window.MiniProfiler = MiniProfiler;
 MiniProfiler.init();
diff --git a/lib/html/profile_handler.js b/lib/html/profile_handler.js
index 5948085..d147c26 100644
--- a/lib/html/profile_handler.js
+++ b/lib/html/profile_handler.js
@@ -1 +1 @@
-<script async type="text/javascript" id="mini-profiler" src="{path}includes.js?v={version}" data-version="{version}" data-path="{path}" data-current-id="{currentId}" data-ids="{ids}" data-horizontal-position="{horizontalPosition}" data-vertical-position="{verticalPosition}" data-trivial="{showTrivial}" data-children="{showChildren}" data-max-traces="{maxTracesToShow}" data-controls="{showControls}" data-total-sql-count="{showTotalSqlCount}" data-authorized="{authorized}" data-toggle-shortcut="{toggleShortcut}" data-start-hidden="{startHidden}" data-collapse-results="{collapseResults}" data-html-container="{htmlContainer}"></script>

[... diff too long, it was truncated ...]

GitHub sha: 79ec6c40

This commit appears in #456 which was merged by OsamaSayegh.