Skip to content

Commit 32a58f5

Browse files
committed
Only include minimal amount in sprockets context helper
1 parent a8d7cf7 commit 32a58f5

File tree

4 files changed

+40
-13
lines changed

4 files changed

+40
-13
lines changed

lib/sprockets/rails/context.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
require 'action_view'
2+
require 'sprockets'
3+
4+
module Sprockets
5+
module Rails
6+
module Context
7+
include ActionView::Helpers::AssetUrlHelper
8+
include ActionView::Helpers::AssetTagHelper
9+
10+
def self.included(klass)
11+
klass.class_eval do
12+
class_attribute :config, :assets_prefix, :digest_assets
13+
end
14+
end
15+
16+
def compute_asset_path(path, options = {})
17+
begin
18+
asset_uri = locate(path)
19+
rescue FileNotFound
20+
# TODO: eh, we should be able to use a form of locate that returns
21+
# nil instead of raising an exception.
22+
end
23+
24+
if asset_uri
25+
asset = link_asset(path)
26+
digest_path = asset.digest_path
27+
path = digest_path if digest_assets
28+
File.join(assets_prefix || "/", path)
29+
else
30+
super
31+
end
32+
end
33+
end
34+
end
35+
end

lib/sprockets/rails/helper.rb

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,7 @@ def initialize(bad_path, good_path, prefix)
4545
:assets_prefix, :digest_assets, :debug_assets]
4646

4747
def self.included(klass)
48-
if klass < Sprockets::Context
49-
klass.class_eval do
50-
alias_method :assets_environment, :environment
51-
def assets_manifest; end
52-
class_attribute :config, :assets_prefix, :digest_assets, :debug_assets
53-
end
54-
else
55-
klass.class_attribute(*VIEW_ACCESSORS)
56-
end
48+
klass.class_attribute(*VIEW_ACCESSORS)
5749
end
5850

5951
def self.extended(obj)
@@ -64,8 +56,6 @@ def self.extended(obj)
6456

6557
def compute_asset_path(path, options = {})
6658
if digest_path = asset_digest_path(path)
67-
# Check if we are inside Sprockets context
68-
link_asset(path) if defined?(link_asset)
6959
path = digest_path if digest_assets
7060
path += "?body=1" if options[:debug]
7161
File.join(assets_prefix || "/", path)

lib/sprockets/railtie.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
require 'action_controller/railtie'
44
require 'active_support/core_ext/module/remove_method'
55
require 'sprockets'
6+
require 'sprockets/rails/context'
67
require 'sprockets/rails/helper'
78
require 'sprockets/rails/version'
89

@@ -27,7 +28,7 @@ def assets
2728
env.cache = Sprockets::Cache::FileStore.new(path)
2829

2930
env.context_class.class_eval do
30-
include ::Sprockets::Rails::Helper
31+
include ::Sprockets::Rails::Context
3132
end
3233
end
3334
end

test/test_helper.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
require 'action_view'
44
require 'sprockets'
5+
require 'sprockets/rails/context'
56
require 'sprockets/rails/helper'
67

78
ActiveSupport::TestCase.test_order = :random if ActiveSupport::TestCase.respond_to?(:test_order=)
@@ -13,7 +14,7 @@ def setup
1314
assets = @assets = Sprockets::Environment.new
1415
@assets.append_path FIXTURES_PATH
1516
@assets.context_class.class_eval do
16-
include ::Sprockets::Rails::Helper
17+
include ::Sprockets::Rails::Context
1718
end
1819

1920
@view = ActionView::Base.new

0 commit comments

Comments
 (0)