Skip to content

Commit 3f26afb

Browse files
committed
Expose Rails.application.assets_manifest
1 parent c397676 commit 3f26afb

File tree

3 files changed

+64
-29
lines changed

3 files changed

+64
-29
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
### 2.3.0
2+
3+
* Expose Rails.application.assets_manifest
4+
5+
*Joshua Peek*
6+
17
### 2.2.0
28

39
* Support Sprockets 2.8 through 3.x.

lib/sprockets/railtie.rb

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ def assets
3232
end
3333
end
3434
attr_writer :assets
35+
36+
# Returns Sprockets::Manifest for app config.
37+
attr_accessor :assets_manifest
3538
end
3639
end
3740

@@ -66,8 +69,6 @@ def configure(&block)
6669
config.after_initialize do |app|
6770
config = app.config
6871

69-
manifest_assets_path = File.join(config.paths['public'].first, config.assets.prefix)
70-
7172
# Configuration options that should invalidate
7273
# the Sprockets cache when changed.
7374
app.assets.version = [
@@ -83,28 +84,6 @@ def configure(&block)
8384
app.assets.append_path path
8485
end
8586

86-
ActiveSupport.on_load(:action_view) do
87-
include Sprockets::Rails::Helper
88-
89-
# Copy relevant config to AV context
90-
self.debug_assets = config.assets.debug
91-
self.digest_assets = config.assets.digest
92-
self.assets_prefix = config.assets.prefix
93-
94-
# Copy over to Sprockets as well
95-
context = app.assets.context_class
96-
context.assets_prefix = config.assets.prefix
97-
context.digest_assets = config.assets.digest
98-
context.config = config.action_controller
99-
100-
if config.assets.compile
101-
self.assets_environment = app.assets
102-
self.assets_manifest = Sprockets::Manifest.new(app.assets, manifest_assets_path, config.assets.manifest)
103-
else
104-
self.assets_manifest = Sprockets::Manifest.new(manifest_assets_path, config.assets.manifest)
105-
end
106-
end
107-
10887
app.assets.js_compressor = config.assets.js_compressor
10988
app.assets.css_compressor = config.assets.css_compressor
11089

@@ -121,6 +100,30 @@ def configure(&block)
121100
app.assets = app.assets.index
122101
end
123102

103+
manifest_assets_path = File.join(config.paths['public'].first, config.assets.prefix)
104+
if config.assets.compile
105+
app.assets_manifest = Sprockets::Manifest.new(app.assets, manifest_assets_path, config.assets.manifest)
106+
else
107+
app.assets_manifest = Sprockets::Manifest.new(manifest_assets_path, config.assets.manifest)
108+
end
109+
110+
ActiveSupport.on_load(:action_view) do
111+
include Sprockets::Rails::Helper
112+
113+
# Copy relevant config to AV context
114+
self.debug_assets = config.assets.debug
115+
self.digest_assets = config.assets.digest
116+
self.assets_prefix = config.assets.prefix
117+
118+
# Copy over to Sprockets as well
119+
context = app.assets.context_class
120+
context.assets_prefix = config.assets.prefix
121+
context.digest_assets = config.assets.digest
122+
context.config = config.action_controller
123+
124+
self.assets_environment = app.assets if config.assets.compile
125+
self.assets_manifest = app.assets_manifest
126+
end
124127

125128
Sprockets::Rails::Helper.precompile ||= app.config.assets.precompile
126129
Sprockets::Rails::Helper.assets ||= app.assets

test/test_railtie.rb

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,16 @@ def test_app_asset_available_when_compile
6666
assert env = app.assets
6767
end
6868

69+
def test_app_asset_manifest_available_when_compile
70+
assert_equal true, app.config.assets.compile
71+
72+
app.initialize!
73+
74+
assert manifest = app.assets_manifest
75+
assert_equal app.assets, manifest.environment
76+
assert_equal File.join(ROOT, "public/assets"), manifest.directory
77+
end
78+
6979
def test_app_asset_available_when_no_compile
7080
app.configure do
7181
config.assets.compile = false
@@ -78,6 +88,20 @@ def test_app_asset_available_when_no_compile
7888
assert env = app.assets
7989
end
8090

91+
def test_app_asset_manifest_available_when_no_compile
92+
app.configure do
93+
config.assets.compile = false
94+
end
95+
96+
assert_equal false, app.config.assets.compile
97+
98+
app.initialize!
99+
100+
assert manifest = app.assets_manifest
101+
refute manifest.environment
102+
assert_equal File.join(ROOT, "public/assets"), manifest.directory
103+
end
104+
81105
def test_copies_paths
82106
app.configure do
83107
config.assets.paths << "javascripts"
@@ -176,7 +200,7 @@ def test_action_view_helper
176200
assert_equal false, ActionView::Base.digest_assets
177201
assert_equal "/assets", ActionView::Base.assets_prefix
178202
assert_equal app.assets, ActionView::Base.assets_environment
179-
assert_match %r{public/assets/manifest-.*.json}, ActionView::Base.assets_manifest.path
203+
assert_equal app.assets_manifest, ActionView::Base.assets_manifest
180204

181205
@view = ActionView::Base.new
182206
assert_equal "/javascripts/xmlhr.js", @view.javascript_path("xmlhr")
@@ -198,8 +222,9 @@ def test_manifest_path
198222
end
199223
app.initialize!
200224

201-
assert_match %r{config/foo/bar\.json$}, ActionView::Base.assets_manifest.path
202-
assert_match %r{public/assets$}, ActionView::Base.assets_manifest.dir
225+
assert manifest = app.assets_manifest
226+
assert_match %r{config/foo/bar\.json$}, manifest.path
227+
assert_match %r{public/assets$}, manifest.dir
203228
end
204229

205230
def test_manifest_path_respects_rails_public_path
@@ -209,8 +234,9 @@ def test_manifest_path_respects_rails_public_path
209234
end
210235
app.initialize!
211236

212-
assert_match %r{test_public/assets/manifest-.*\.json$}, ActionView::Base.assets_manifest.path
213-
assert_match %r{test_public/assets$}, ActionView::Base.assets_manifest.dir
237+
assert manifest = app.assets_manifest
238+
assert_match %r{test_public/assets/manifest-.*\.json$}, manifest.path
239+
assert_match %r{test_public/assets$}, manifest.dir
214240
end
215241
end
216242
end

0 commit comments

Comments
 (0)