aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoshua Peek <josh@joshpeek.com>2011-07-27 15:09:42 -0500
committerJoshua Peek <josh@joshpeek.com>2011-07-27 15:29:36 -0500
commit8248052ee74465abfae5b202270e96c9fd93e785 (patch)
tree4c704402c32e0e3263bf85304c4cec3e5ed5e6f9
parent5eeae68657e8d6db58f33b5c8629b94878e03990 (diff)
downloadrails-8248052ee74465abfae5b202270e96c9fd93e785.tar.gz
rails-8248052ee74465abfae5b202270e96c9fd93e785.tar.bz2
rails-8248052ee74465abfae5b202270e96c9fd93e785.zip
Make Rails.application.assets available in initializers
-rw-r--r--actionpack/lib/sprockets/railtie.rb80
1 files changed, 36 insertions, 44 deletions
diff --git a/actionpack/lib/sprockets/railtie.rb b/actionpack/lib/sprockets/railtie.rb
index 83799d2b4d..c8438e6043 100644
--- a/actionpack/lib/sprockets/railtie.rb
+++ b/actionpack/lib/sprockets/railtie.rb
@@ -11,15 +11,20 @@ module Sprockets
load "sprockets/assets.rake"
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, and so that other Railties have an opportunity to
- # register compressors.
- config.after_initialize do |app|
- assets = app.config.assets
- next unless assets.enabled
+ initializer "sprockets.environment" do |app|
+ config = app.config
+ next unless config.assets.enabled
- app.assets = asset_environment(app)
+ require 'sprockets'
+
+ app.assets = Sprockets::Environment.new(app.root.to_s) do |env|
+ env.static_root = File.join(app.root.join('public'), config.assets.prefix)
+ env.logger = ::Rails.logger
+
+ if config.assets.cache_store != false
+ env.cache = ActiveSupport::Cache.lookup_store(config.assets.cache_store) || ::Rails.cache
+ end
+ end
ActiveSupport.on_load(:action_view) do
include ::Sprockets::Helpers::RailsHelper
@@ -28,53 +33,40 @@ module Sprockets
include ::Sprockets::Helpers::RailsHelper
end
end
-
- app.routes.prepend 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)
- require "sprockets"
-
- assets = app.config.assets
-
- env = Sprockets::Environment.new(app.root.to_s)
+ # We need to configure this after initialization to ensure we collect
+ # paths from all engines. This hook is invoked exactly before routes
+ # are compiled, and so that other Railties have an opportunity to
+ # register compressors.
+ config.after_initialize do |app|
+ next unless app.assets
+ config = app.config
- env.static_root = File.join(app.root.join("public"), assets.prefix)
+ config.assets.paths.each { |path| app.assets.append_path(path) }
- if env.respond_to?(:append_path)
- assets.paths.each { |path| env.append_path(path) }
- else
- env.paths.concat assets.paths
+ if config.assets.compress
+ # temporarily hardcode default JS compressor to uglify. Soon, it will work
+ # the same as SCSS, where a default plugin sets the default.
+ unless config.assets.js_compressor == false
+ app.assets.js_compressor = LazyCompressor.new { expand_js_compressor(config.assets.js_compressor || :uglifier) }
end
- env.logger = ::Rails.logger
-
- if env.respond_to?(:cache) && assets.cache_store != false
- env.cache = ActiveSupport::Cache.lookup_store(assets.cache_store) || ::Rails.cache
+ unless config.assets.css_compressor == false
+ app.assets.css_compressor = LazyCompressor.new { expand_css_compressor(config.assets.css_compressor) }
end
+ end
- if assets.compress
- # temporarily hardcode default JS compressor to uglify. Soon, it will work
- # the same as SCSS, where a default plugin sets the default.
- unless assets.js_compressor == false
- env.js_compressor = LazyCompressor.new { expand_js_compressor(assets.js_compressor || :uglifier) }
- end
-
- unless assets.css_compressor == false
- env.css_compressor = LazyCompressor.new { expand_css_compressor(assets.css_compressor) }
- end
- end
+ app.routes.prepend do
+ mount app.assets => config.assets.prefix
+ end
- env
+ if config.action_controller.perform_caching
+ app.assets = app.assets.index
end
+ end
+ protected
def expand_js_compressor(sym)
case sym
when :closure