Permalink
Browse files

Move app initializers to sprockets railtie.

  • Loading branch information...
1 parent 16b9547 commit 8f75c3abcde4f2ff64e855178027e1bd93976de9 @josevalim josevalim committed Apr 17, 2011
@@ -1,9 +1,40 @@
require "sprockets"
+# TODO: Move this to sprockets-rails
+# If so, we can move the require to a Gemfile and remove assets.enabled
class Sprockets::Railtie < Rails::Railtie
+ # Configure ActionController to use sprockets.
initializer "sprockets.set_configs", :after => "action_controller.set_configs" do |app|
ActiveSupport.on_load(:action_controller) do
self.use_sprockets = app.config.assets.enabled
end
end
+
+ # We need to configure this after initialization to ensure we collect
+ # paths from all engines. This hook is invoked exactly before routes
+ # are compiled.
+ config.after_initialize do |app|
+ assets = app.config.assets
+ next unless assets.enabled
+
+ app.assets = asset_environment(app)
+ app.routes.append do
+ mount app.assets => assets.prefix
+ end
+
+ if config.action_controller.perform_caching
+ app.assets = app.assets.index
+ end
+ end
+
+ protected
+
+ def asset_environment(app)
+ assets = app.config.assets
+ env = Sprockets::Environment.new(app.root.to_s)
+ env.static_root = File.join(app.root.join("public"), assets.prefix)
+ env.paths.concat assets.paths
+ env.logger = Rails.logger
+ env
+ end
end
@@ -139,15 +139,6 @@ def config
alias :build_middleware_stack :app
- def build_asset_environment
- require 'sprockets'
- env = Sprockets::Environment.new(root.to_s)
- env.static_root = File.join(root.join("public"), config.assets.prefix)
- env.paths.concat config.assets.paths
- env.logger = Rails.logger
- @assets = env
- end
-
def default_middleware_stack
ActionDispatch::MiddlewareStack.new.tap do |middleware|
if rack_cache = config.action_controller.perform_caching && config.action_dispatch.rack_cache
@@ -33,22 +33,6 @@ module Finisher
end
end
- initializer :add_sprockets_route do |app|
- assets = config.assets
- if assets.enabled
- build_asset_environment
- app.routes.append do
- mount app.assets => assets.prefix
- end
- end
- end
-
- initializer :index_sprockets_environment do |app|
- if config.assets.enabled && config.action_controller.perform_caching
- app.assets = app.assets.index
- end
- end
-
initializer :build_middleware_stack do
build_middleware_stack
end
@@ -69,6 +53,8 @@ module Finisher
end
# Force routes to be loaded just at the end and add it to to_prepare callbacks
+ # This needs to be after the finisher hook to ensure routes added in the hook
+ # are still loaded.
initializer :set_routes_reloader do |app|
reloader = lambda { app.routes_reloader.execute_if_updated }
reloader.call

0 comments on commit 8f75c3a

Please sign in to comment.