diff --git a/lib/sprockets/railtie.rb b/lib/sprockets/railtie.rb index 3ff11003..56e6a22c 100644 --- a/lib/sprockets/railtie.rb +++ b/lib/sprockets/railtie.rb @@ -68,8 +68,8 @@ class Railtie < ::Rails::Railtie include Sprockets::Rails::Utils class ManifestNeededError < StandardError - def initialize - msg = "Expected to find a manifest file in `app/assets/config/manifest.js`\n" + + def initialize(manifest_path) + msg = "Expected to find a manifest file in `#{manifest_path}`\n" + "But did not, please create this file and use it to link any assets that need\n" + "to be rendered by your app:\n\n" + "Example:\n" + @@ -99,11 +99,22 @@ def configure(&block) config.assets.prefix = "/assets" config.assets.manifest = nil config.assets.quiet = false + config.assets.config_manifest = nil + + initializer :set_config_manifest_path do |app| + config_manifest = if app.config.assets.config_manifest.present? + Pathname.new(app.config.assets.config_manifest) + else + ::Rails.root.join("app/assets/config/manifest.js") + end + raise ManifestNeededError, config_manifest unless config_manifest.exist? + + app.config.assets.config_manifest = config_manifest.to_s + end initializer :set_default_precompile do |app| if using_sprockets4? - raise ManifestNeededError unless ::Rails.root.join("app/assets/config/manifest.js").exist? - app.config.assets.precompile += %w( manifest.js ) + app.config.assets.precompile += [app.config.assets.config_manifest] else app.config.assets.precompile += [LOOSE_APP_ASSETS, /(?:\/|\\|\A)application\.(css|js)$/] end diff --git a/test/test_railtie.rb b/test/test_railtie.rb index 38708904..314cdf6f 100644 --- a/test/test_railtie.rb +++ b/test/test_railtie.rb @@ -12,6 +12,15 @@ def silence_stderr $stderr.reopen orig_stderr end +def create_config_manifest(dir = "app/assets/config") + Dir.chdir(app.root) do + FileUtils.mkdir_p(dir) + File.open("#{ dir }/manifest.js", "w") do |f| + f << "" + end + end +end + class TestBoot < Minitest::Test include ActiveSupport::Testing::Isolation @@ -39,13 +48,7 @@ def setup @app.config.active_support.deprecation = :notify ActionView::Base # load ActionView - Dir.chdir(app.root) do - dir = "app/assets/config" - FileUtils.mkdir_p(dir) - File.open("#{ dir }/manifest.js", "w") do |f| - f << "" - end - end + create_config_manifest end def test_initialize @@ -295,6 +298,31 @@ def test_sprockets_context_helper assert_nil env.context_class.config.asset_host end + def test_default_config_manifest_path + app.initialize! + assert_match %r{app/assets/config.manifest.js$}, app.config.assets.config_manifest + end + + def test_custom_config_manifest_path + create_config_manifest("my/custom/path") + app.configure do + config.assets.config_manifest = 'my/custom/path/manifest.js' + end + app.initialize! + + assert_match %r{my/custom/path/manifest.js$}, app.config.assets.config_manifest + end + + def test_config_manifest_raises_when_does_not_exist + app.configure do + config.assets.config_manifest = 'not/found/manifest.js' + end + + assert_raises Sprockets::Railtie::ManifestNeededError do + app.initialize! + end + end + def test_manifest_path app.configure do config.assets.manifest = Rails.root.join('config','foo','bar.json')