Skip to content

Commit 8f75c3a

Browse files
committed
Move app initializers to sprockets railtie.
1 parent 16b9547 commit 8f75c3a

File tree

3 files changed

+33
-25
lines changed

3 files changed

+33
-25
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,40 @@
11
require "sprockets"
22

3+
# TODO: Move this to sprockets-rails
4+
# If so, we can move the require to a Gemfile and remove assets.enabled
35
class Sprockets::Railtie < Rails::Railtie
6+
# Configure ActionController to use sprockets.
47
initializer "sprockets.set_configs", :after => "action_controller.set_configs" do |app|
58
ActiveSupport.on_load(:action_controller) do
69
self.use_sprockets = app.config.assets.enabled
710
end
811
end
12+
13+
# We need to configure this after initialization to ensure we collect
14+
# paths from all engines. This hook is invoked exactly before routes
15+
# are compiled.
16+
config.after_initialize do |app|
17+
assets = app.config.assets
18+
next unless assets.enabled
19+
20+
app.assets = asset_environment(app)
21+
app.routes.append do
22+
mount app.assets => assets.prefix
23+
end
24+
25+
if config.action_controller.perform_caching
26+
app.assets = app.assets.index
27+
end
28+
end
29+
30+
protected
31+
32+
def asset_environment(app)
33+
assets = app.config.assets
34+
env = Sprockets::Environment.new(app.root.to_s)
35+
env.static_root = File.join(app.root.join("public"), assets.prefix)
36+
env.paths.concat assets.paths
37+
env.logger = Rails.logger
38+
env
39+
end
940
end

railties/lib/rails/application.rb

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -139,15 +139,6 @@ def config
139139

140140
alias :build_middleware_stack :app
141141

142-
def build_asset_environment
143-
require 'sprockets'
144-
env = Sprockets::Environment.new(root.to_s)
145-
env.static_root = File.join(root.join("public"), config.assets.prefix)
146-
env.paths.concat config.assets.paths
147-
env.logger = Rails.logger
148-
@assets = env
149-
end
150-
151142
def default_middleware_stack
152143
ActionDispatch::MiddlewareStack.new.tap do |middleware|
153144
if rack_cache = config.action_controller.perform_caching && config.action_dispatch.rack_cache

railties/lib/rails/application/finisher.rb

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,6 @@ module Finisher
3333
end
3434
end
3535

36-
initializer :add_sprockets_route do |app|
37-
assets = config.assets
38-
if assets.enabled
39-
build_asset_environment
40-
app.routes.append do
41-
mount app.assets => assets.prefix
42-
end
43-
end
44-
end
45-
46-
initializer :index_sprockets_environment do |app|
47-
if config.assets.enabled && config.action_controller.perform_caching
48-
app.assets = app.assets.index
49-
end
50-
end
51-
5236
initializer :build_middleware_stack do
5337
build_middleware_stack
5438
end
@@ -69,6 +53,8 @@ module Finisher
6953
end
7054

7155
# Force routes to be loaded just at the end and add it to to_prepare callbacks
56+
# This needs to be after the finisher hook to ensure routes added in the hook
57+
# are still loaded.
7258
initializer :set_routes_reloader do |app|
7359
reloader = lambda { app.routes_reloader.execute_if_updated }
7460
reloader.call

0 commit comments

Comments
 (0)