Skip to content

Commit c56e8fa

Browse files
committed
Define custom cache resolvers for AC config
1 parent 86fb891 commit c56e8fa

File tree

3 files changed

+18
-37
lines changed

3 files changed

+18
-37
lines changed

lib/sprockets/rails/context.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ def self.included(klass)
1414
end
1515

1616
def compute_asset_path(path, options = {})
17+
@dependencies << 'actioncontroller-asset-url-config'
18+
1719
begin
1820
asset_uri = resolve(path, compat: false)
1921
rescue FileNotFound
@@ -32,4 +34,10 @@ def compute_asset_path(path, options = {})
3234
end
3335
end
3436
end
37+
38+
register_dependency_resolver 'actioncontroller-asset-url-config' do |env|
39+
config = env.context_class.config
40+
[config.relative_url_root,
41+
(config.asset_host unless config.asset_host.respond_to?(:call))]
42+
end
3543
end

lib/sprockets/railtie.rb

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -88,16 +88,15 @@ def configure(&block)
8888
)
8989
end
9090

91+
Sprockets.register_dependency_resolver 'rails-env' do
92+
::Rails.env
93+
end
94+
config.assets.configure do |env|
95+
env.depend_on 'environment-version'
96+
end
97+
9198
config.assets.configure do |env|
92-
# Configuration options that should invalidate
93-
# the Sprockets cache when changed.
94-
env.version = [
95-
::Rails.env,
96-
config.assets.version,
97-
config.action_controller.relative_url_root,
98-
(config.action_controller.asset_host unless config.action_controller.asset_host.respond_to?(:call)),
99-
Sprockets::Rails::VERSION
100-
].compact.join('-')
99+
env.version = config.assets.version
101100
end
102101

103102
rake_tasks do |app|

test/test_railtie.rb

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def test_defaults
5959
assert_kind_of Sprockets::Environment, env
6060

6161
assert_equal ROOT, env.root
62-
assert_equal "test--#{Sprockets::Rails::VERSION}", env.version
62+
assert_equal "", env.version
6363
assert env.cache
6464
assert_equal [], env.paths
6565
assert_nil env.js_compressor
@@ -145,33 +145,7 @@ def test_version
145145
app.initialize!
146146

147147
assert env = app.assets
148-
assert_equal "test-v2-#{Sprockets::Rails::VERSION}", env.version
149-
end
150-
151-
def test_version_fragments_with_string_asset_host
152-
app.configure do
153-
config.assets.version = 'v2'
154-
config.action_controller.asset_host = 'http://some-cdn.com'
155-
config.action_controller.relative_url_root = 'some-path'
156-
end
157-
app.initialize!
158-
159-
assert env = app.assets
160-
assert_equal "test-v2-some-path-http://some-cdn.com-#{Sprockets::Rails::VERSION}", env.version
161-
end
162-
163-
def test_version_fragments_with_proc_asset_host
164-
app.configure do
165-
config.assets.version = 'v2'
166-
config.action_controller.asset_host = ->(path, request) {
167-
'http://some-cdn.com'
168-
}
169-
config.action_controller.relative_url_root = 'some-path'
170-
end
171-
app.initialize!
172-
173-
assert env = app.assets
174-
assert_equal "test-v2-some-path-#{Sprockets::Rails::VERSION}", env.version
148+
assert_equal "v2", env.version
175149
end
176150

177151
def test_configure

0 commit comments

Comments
 (0)