Skip to content

Commit 7c48f1b

Browse files
committed
Merge pull request #220 from rails/disable-app-assets-on-compile-false
Disable Rails.application.assets when compile=false
2 parents 5e8687c + d7c7ee1 commit 7c48f1b

File tree

2 files changed

+43
-42
lines changed

2 files changed

+43
-42
lines changed

lib/sprockets/railtie.rb

Lines changed: 41 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,7 @@ class Configuration
2020
remove_possible_method :assets=
2121

2222
# Returns Sprockets::Environment for app config.
23-
def assets
24-
@assets ||= Sprockets::Environment.new(root.to_s) do |env|
25-
env.version = ::Rails.env
26-
env.cache = Sprockets::Cache::FileStore.new("#{root}/tmp/cache")
27-
28-
env.context_class.class_eval do
29-
include ::Sprockets::Rails::Context
30-
end
31-
end
32-
end
33-
attr_writer :assets
23+
attr_accessor :assets
3424

3525
# Returns Sprockets::Manifest for app config.
3626
attr_accessor :assets_manifest
@@ -77,47 +67,70 @@ def configure(&block)
7767
Sprockets::Rails::Task.new(app)
7868
end
7969

80-
config.after_initialize do |app|
70+
def self.build_environment(app)
8171
config = app.config
72+
env = Sprockets::Environment.new(app.root.to_s)
73+
74+
# Copy config.assets.paths to Sprockets
75+
config.assets.paths.each do |path|
76+
env.append_path path
77+
end
78+
79+
env.js_compressor = config.assets.js_compressor
80+
env.css_compressor = config.assets.css_compressor
81+
82+
env.context_class.class_eval do
83+
include ::Sprockets::Rails::Context
84+
self.assets_prefix = config.assets.prefix
85+
self.digest_assets = config.assets.digest
86+
self.config = config.action_controller
87+
end
8288

8389
# Configuration options that should invalidate
8490
# the Sprockets cache when changed.
85-
app.assets.version = [
91+
env.version = [
8692
::Rails.env,
87-
app.assets.version,
93+
::Rails.env, # TODO: Remove duplicate key
8894
config.assets.version,
8995
config.action_controller.relative_url_root,
9096
(config.action_controller.asset_host unless config.action_controller.asset_host.respond_to?(:call)),
9197
Sprockets::Rails::VERSION
9298
].compact.join('-')
9399

94-
# Copy config.assets.paths to Sprockets
95-
config.assets.paths.each do |path|
96-
app.assets.append_path path
97-
end
98-
99-
app.assets.js_compressor = config.assets.js_compressor
100-
app.assets.css_compressor = config.assets.css_compressor
100+
env.cache = Sprockets::Cache::FileStore.new("#{app.root}/tmp/cache")
101101

102102
# Run app.assets.configure blocks
103103
config.assets._blocks.each do |block|
104-
block.call app.assets
104+
block.call(env)
105105
end
106106

107107
# No more configuration changes at this point.
108108
# With cache classes on, Sprockets won't check the FS when files
109109
# change. Preferable in production when the FS only changes on
110110
# deploys when the app restarts.
111111
if config.cache_classes
112-
app.assets = app.assets.cached
112+
env = env.cached
113113
end
114114

115-
manifest_assets_path = File.join(config.paths['public'].first, config.assets.prefix)
115+
env
116+
end
117+
118+
def self.build_manifest(app)
119+
config = app.config
120+
path = File.join(config.paths['public'].first, config.assets.prefix)
121+
Sprockets::Manifest.new(app.assets, path, config.assets.manifest)
122+
end
123+
124+
config.after_initialize do |app|
125+
config = app.config
126+
116127
if config.assets.compile
117-
app.assets_manifest = Sprockets::Manifest.new(app.assets, manifest_assets_path, config.assets.manifest)
118-
else
119-
app.assets_manifest = Sprockets::Manifest.new(manifest_assets_path, config.assets.manifest)
128+
app.assets = self.build_environment(app)
129+
app.routes.prepend do
130+
mount app.assets => config.assets.prefix
131+
end
120132
end
133+
app.assets_manifest = build_manifest(app)
121134

122135
ActiveSupport.on_load(:action_view) do
123136
include Sprockets::Rails::Helper
@@ -128,21 +141,9 @@ def configure(&block)
128141
self.assets_prefix = config.assets.prefix
129142
self.assets_precompile = config.assets.precompile
130143

131-
# Copy over to Sprockets as well
132-
context = app.assets.context_class
133-
context.assets_prefix = config.assets.prefix
134-
context.digest_assets = config.assets.digest
135-
context.config = config.action_controller
136-
137-
self.assets_environment = app.assets if config.assets.compile
144+
self.assets_environment = app.assets
138145
self.assets_manifest = app.assets_manifest
139146
end
140-
141-
if config.assets.compile
142-
app.routes.prepend do
143-
mount app.assets => config.assets.prefix
144-
end
145-
end
146147
end
147148
end
148149
end

test/test_railtie.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def test_app_asset_manifest_available_when_compile
7676
assert_equal File.join(ROOT, "public/assets"), manifest.dir
7777
end
7878

79-
def test_app_asset_available_when_no_compile
79+
def test_app_asset_not_available_when_no_compile
8080
app.configure do
8181
config.assets.compile = false
8282
end
@@ -85,7 +85,7 @@ def test_app_asset_available_when_no_compile
8585

8686
app.initialize!
8787

88-
assert app.assets
88+
refute app.assets
8989
end
9090

9191
def test_app_asset_manifest_available_when_no_compile

0 commit comments

Comments
 (0)