-
Notifications
You must be signed in to change notification settings - Fork 247
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
config.assets.quiet logger silencing with middleware
- Loading branch information
1 parent
b4f9006
commit 91da721
Showing
5 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
module Sprockets | ||
module Rails | ||
class QuietAssets | ||
def initialize(app) | ||
@app = app | ||
@assets_regex = %r(\A/{0,2}#{::Rails.application.config.assets.prefix}) | ||
end | ||
|
||
def call(env) | ||
if env['PATH_INFO'] =~ @assets_regex | ||
::Rails.logger.silence { @app.call(env) } | ||
else | ||
@app.call(env) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
require 'active_support' | ||
require 'active_support/testing/isolation' | ||
require 'active_support/log_subscriber/test_helper' | ||
require 'minitest/autorun' | ||
|
||
require 'sprockets/railtie' | ||
require 'rails' | ||
|
||
Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test) | ||
|
||
class TestQuietAssets < Minitest::Test | ||
include ActiveSupport::Testing::Isolation | ||
|
||
def setup | ||
@app = Class.new(Rails::Application) | ||
@app.config.eager_load = false | ||
@app.config.logger = ActiveSupport::Logger.new("/dev/null") | ||
@app.initialize! | ||
|
||
Rails.logger.level = Logger::DEBUG | ||
end | ||
|
||
def test_silences_with_default_prefix | ||
assert_equal Logger::ERROR, middleware.call("PATH_INFO" => "/assets/stylesheets/application.css") | ||
end | ||
|
||
def test_silencess_with_custom_prefix | ||
Rails.application.config.assets.prefix = "path/to" | ||
assert_equal Logger::ERROR, middleware.call("PATH_INFO" => "/path/to/thing") | ||
end | ||
|
||
def test_does_not_silence_without_match | ||
assert_equal Logger::DEBUG, middleware.call("PATH_INFO" => "/path/to/thing") | ||
end | ||
|
||
private | ||
|
||
def middleware | ||
@middleware ||= Sprockets::Rails::QuietAssets.new(->(env) { Rails.logger.level }) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters