Skip to content

Commit

Permalink
Merge branch 'manifest-api' into 2.x
Browse files Browse the repository at this point in the history
  • Loading branch information
josh committed Nov 29, 2014
2 parents c397676 + c72ea87 commit 90e3f5a
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 29 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
### 2.3.0

* Expose Rails.application.assets_manifest

*Joshua Peek*

### 2.2.0

* Support Sprockets 2.8 through 3.x.
Expand Down
51 changes: 27 additions & 24 deletions lib/sprockets/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ def assets
end
end
attr_writer :assets

# Returns Sprockets::Manifest for app config.
attr_accessor :assets_manifest
end
end

Expand Down Expand Up @@ -66,8 +69,6 @@ def configure(&block)
config.after_initialize do |app|
config = app.config

manifest_assets_path = File.join(config.paths['public'].first, config.assets.prefix)

# Configuration options that should invalidate
# the Sprockets cache when changed.
app.assets.version = [
Expand All @@ -83,28 +84,6 @@ def configure(&block)
app.assets.append_path path
end

ActiveSupport.on_load(:action_view) do
include Sprockets::Rails::Helper

# Copy relevant config to AV context
self.debug_assets = config.assets.debug
self.digest_assets = config.assets.digest
self.assets_prefix = config.assets.prefix

# Copy over to Sprockets as well
context = app.assets.context_class
context.assets_prefix = config.assets.prefix
context.digest_assets = config.assets.digest
context.config = config.action_controller

if config.assets.compile
self.assets_environment = app.assets
self.assets_manifest = Sprockets::Manifest.new(app.assets, manifest_assets_path, config.assets.manifest)
else
self.assets_manifest = Sprockets::Manifest.new(manifest_assets_path, config.assets.manifest)
end
end

app.assets.js_compressor = config.assets.js_compressor
app.assets.css_compressor = config.assets.css_compressor

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

manifest_assets_path = File.join(config.paths['public'].first, config.assets.prefix)
if config.assets.compile
app.assets_manifest = Sprockets::Manifest.new(app.assets, manifest_assets_path, config.assets.manifest)
else
app.assets_manifest = Sprockets::Manifest.new(manifest_assets_path, config.assets.manifest)
end

ActiveSupport.on_load(:action_view) do
include Sprockets::Rails::Helper

# Copy relevant config to AV context
self.debug_assets = config.assets.debug
self.digest_assets = config.assets.digest
self.assets_prefix = config.assets.prefix

# Copy over to Sprockets as well
context = app.assets.context_class
context.assets_prefix = config.assets.prefix
context.digest_assets = config.assets.digest
context.config = config.action_controller

self.assets_environment = app.assets if config.assets.compile
self.assets_manifest = app.assets_manifest
end

Sprockets::Rails::Helper.precompile ||= app.config.assets.precompile
Sprockets::Rails::Helper.assets ||= app.assets
Expand Down
36 changes: 31 additions & 5 deletions test/test_railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,16 @@ def test_app_asset_available_when_compile
assert env = app.assets
end

def test_app_asset_manifest_available_when_compile
assert_equal true, app.config.assets.compile

app.initialize!

assert manifest = app.assets_manifest
assert_equal app.assets, manifest.environment
assert_equal File.join(ROOT, "public/assets"), manifest.dir
end

def test_app_asset_available_when_no_compile
app.configure do
config.assets.compile = false
Expand All @@ -78,6 +88,20 @@ def test_app_asset_available_when_no_compile
assert env = app.assets
end

def test_app_asset_manifest_available_when_no_compile
app.configure do
config.assets.compile = false
end

assert_equal false, app.config.assets.compile

app.initialize!

assert manifest = app.assets_manifest
refute manifest.environment
assert_equal File.join(ROOT, "public/assets"), manifest.dir
end

def test_copies_paths
app.configure do
config.assets.paths << "javascripts"
Expand Down Expand Up @@ -176,7 +200,7 @@ def test_action_view_helper
assert_equal false, ActionView::Base.digest_assets
assert_equal "/assets", ActionView::Base.assets_prefix
assert_equal app.assets, ActionView::Base.assets_environment
assert_match %r{public/assets/manifest-.*.json}, ActionView::Base.assets_manifest.path
assert_equal app.assets_manifest, ActionView::Base.assets_manifest

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

assert_match %r{config/foo/bar\.json$}, ActionView::Base.assets_manifest.path
assert_match %r{public/assets$}, ActionView::Base.assets_manifest.dir
assert manifest = app.assets_manifest
assert_match %r{config/foo/bar\.json$}, manifest.path
assert_match %r{public/assets$}, manifest.dir
end

def test_manifest_path_respects_rails_public_path
Expand All @@ -209,8 +234,9 @@ def test_manifest_path_respects_rails_public_path
end
app.initialize!

assert_match %r{test_public/assets/manifest-.*\.json$}, ActionView::Base.assets_manifest.path
assert_match %r{test_public/assets$}, ActionView::Base.assets_manifest.dir
assert manifest = app.assets_manifest
assert_match %r{test_public/assets/manifest-.*\.json$}, manifest.path
assert_match %r{test_public/assets$}, manifest.dir
end
end
end

0 comments on commit 90e3f5a

Please sign in to comment.